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
18 
19 import kotlin.coroutines.RestrictsSuspension
20 
21 /**
22  * FRP operations that are available while a transaction is active.
23  *
24  * These operations do not accumulate state, which makes [FrpTransactionScope] weaker than
25  * [FrpStateScope], but allows them to be used in more places.
26  */
27 @ExperimentalFrpApi
28 @RestrictsSuspension
29 interface FrpTransactionScope : FrpScope {
30 
31     /**
32      * Returns the current value of this [Transactional] as a [FrpDeferredValue].
33      *
34      * @see sample
35      */
sampleDeferrednull36     @ExperimentalFrpApi fun <A> Transactional<A>.sampleDeferred(): FrpDeferredValue<A>
37 
38     /**
39      * Returns the current value of this [TState] as a [FrpDeferredValue].
40      *
41      * @see sample
42      */
43     @ExperimentalFrpApi fun <A> TState<A>.sampleDeferred(): FrpDeferredValue<A>
44 
45     /** TODO */
46     @ExperimentalFrpApi
47     fun <A> deferredTransactionScope(
48         block: suspend FrpTransactionScope.() -> A
49     ): FrpDeferredValue<A>
50 
51     /** A [TFlow] that emits once, within this transaction, and then never again. */
52     @ExperimentalFrpApi val now: TFlow<Unit>
53 
54     /**
55      * Returns the current value held by this [TState]. Guaranteed to be consistent within the same
56      * transaction.
57      */
58     @ExperimentalFrpApi suspend fun <A> TState<A>.sample(): A = sampleDeferred().get()
59 
60     /**
61      * Returns the current value held by this [Transactional]. Guaranteed to be consistent within
62      * the same transaction.
63      */
64     @ExperimentalFrpApi suspend fun <A> Transactional<A>.sample(): A = sampleDeferred().get()
65 }
66