xref: /aosp_15_r20/frameworks/base/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/internal/TFlowImpl.kt (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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.util.Maybe
20 
21 /* Initialized TFlow */
22 internal fun interface TFlowImpl<out A> {
23     suspend fun activate(evalScope: EvalScope, downstream: Schedulable): ActivationResult<A>?
24 }
25 
26 internal data class ActivationResult<out A>(
27     val connection: NodeConnection<A>,
28     val needsEval: Boolean,
29 )
30 
TFlowCheapnull31 internal inline fun <A> TFlowCheap(crossinline cheap: CheapNodeSubscribe<A>) =
32     TFlowImpl { scope, ds ->
33         scope.cheap(ds)
34     }
35 
36 internal typealias CheapNodeSubscribe<A> =
37     suspend EvalScope.(downstream: Schedulable) -> ActivationResult<A>?
38 
39 internal data class NodeConnection<out A>(
40     val directUpstream: PullNode<A>,
41     val schedulerUpstream: PushNode<*>,
42 )
43 
hasCurrentValuenull44 internal suspend fun <A> NodeConnection<A>.hasCurrentValue(
45     transactionStore: TransactionStore
46 ): Boolean = schedulerUpstream.hasCurrentValue(transactionStore)
47 
48 internal suspend fun <A> NodeConnection<A>.removeDownstreamAndDeactivateIfNeeded(
49     downstream: Schedulable
50 ) = schedulerUpstream.removeDownstreamAndDeactivateIfNeeded(downstream)
51 
52 internal suspend fun <A> NodeConnection<A>.scheduleDeactivationIfNeeded(evalScope: EvalScope) =
53     schedulerUpstream.scheduleDeactivationIfNeeded(evalScope)
54 
55 internal suspend fun <A> NodeConnection<A>.removeDownstream(downstream: Schedulable) =
56     schedulerUpstream.removeDownstream(downstream)
57 
58 internal suspend fun <A> NodeConnection<A>.getPushEvent(evalScope: EvalScope): Maybe<A> =
59     directUpstream.getPushEvent(evalScope)
60 
61 internal val <A> NodeConnection<A>.depthTracker: DepthTracker
62     get() = schedulerUpstream.depthTracker
63