• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.kairos.internal
18 
19 import com.android.systemui.kairos.internal.store.Single
20 import com.android.systemui.kairos.internal.store.SingletonMapK
21 import com.android.systemui.kairos.util.Maybe
22 import com.android.systemui.kairos.util.Maybe.Present
23 
24 internal inline fun <A> filterPresentImpl(
25     crossinline getPulse: EvalScope.() -> EventsImpl<Maybe<A>>
26 ): EventsImpl<A> =
27     DemuxImpl(
28             mapImpl(getPulse) { maybeResult, _ ->
29                 if (maybeResult is Present) {
30                     Single(maybeResult.value)
31                 } else {
32                     Single<A>()
33                 }
34             },
35             numKeys = 1,
36             storeFactory = SingletonMapK.Factory(),
37         )
38         .eventsForKey(Unit)
39 
filterImplnull40 internal inline fun <A> filterImpl(
41     crossinline getPulse: EvalScope.() -> EventsImpl<A>,
42     crossinline f: EvalScope.(A) -> Boolean,
43 ): EventsImpl<A> {
44     val mapped =
45         mapImpl(getPulse) { it, _ -> if (f(it)) Maybe.present(it) else Maybe.absent }.cached()
46     return filterPresentImpl { mapped }
47 }
48