1 /*
2  * 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.FrpBuildScope
20 import com.android.systemui.kairos.FrpStateScope
21 import com.android.systemui.kairos.FrpTransactionScope
22 import com.android.systemui.kairos.TFlow
23 import com.android.systemui.kairos.internal.util.HeteroMap
24 import com.android.systemui.kairos.internal.util.Key
25 import com.android.systemui.kairos.util.Maybe
26 
27 internal interface InitScope {
28     val networkId: Any
29 }
30 
31 internal interface EvalScope : NetworkScope, DeferScope {
32     val frpScope: FrpTransactionScope
33 
runInTransactionScopenull34     suspend fun <R> runInTransactionScope(block: suspend FrpTransactionScope.() -> R): R
35 }
36 
37 internal interface StateScope : EvalScope {
38     override val frpScope: FrpStateScope
39 
40     suspend fun <R> runInStateScope(block: suspend FrpStateScope.() -> R): R
41 
42     val endSignal: TFlow<Any>
43 
44     fun childStateScope(newEnd: TFlow<Any>): StateScope
45 }
46 
47 internal interface BuildScope : StateScope {
48     override val frpScope: FrpBuildScope
49 
runInBuildScopenull50     suspend fun <R> runInBuildScope(block: suspend FrpBuildScope.() -> R): R
51 }
52 
53 internal interface NetworkScope : InitScope {
54 
55     val epoch: Long
56     val network: Network
57 
58     val compactor: Scheduler
59     val scheduler: Scheduler
60 
61     val transactionStore: HeteroMap
62 
63     fun scheduleOutput(output: Output<*>)
64 
65     fun scheduleMuxMover(muxMover: MuxDeferredNode<*, *>)
66 
67     fun schedule(state: TStateSource<*>)
68 
69     suspend fun schedule(node: MuxNode<*, *, *>)
70 
71     fun scheduleDeactivation(node: PushNode<*>)
72 
73     fun scheduleDeactivation(output: Output<*>)
74 }
75 
setResultnull76 internal fun <A> NetworkScope.setResult(node: Key<A>, result: A) {
77     transactionStore[node] = result
78 }
79 
getCurrentValuenull80 internal fun <A> NetworkScope.getCurrentValue(key: Key<A>): Maybe<A> = transactionStore[key]
81 
82 internal fun NetworkScope.hasCurrentValue(key: Key<*>): Boolean = transactionStore.contains(key)
83