From 1ea4433f3dab88a552eefab11dc4e75138f1ca75 Mon Sep 17 00:00:00 2001 From: Steve Elliott Date: Fri, 6 Dec 2024 16:13:23 -0500 Subject: [kairos] add 5-way combine() overload Flag: EXEMPT unused Test: atest kairos-tests Change-Id: Ia3ce305702419f5a498c3a8341879b5aadae6e55 --- .../src/com/android/systemui/kairos/TState.kt | 51 ++++++++++++++++++++++ .../android/systemui/kairos/internal/TStateImpl.kt | 22 ++++++++++ 2 files changed, 73 insertions(+) diff --git a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/TState.kt b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/TState.kt index a4c695657f8d..46b0f075af22 100644 --- a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/TState.kt +++ b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/TState.kt @@ -312,6 +312,57 @@ fun combine( ) } +/** + * Returns a [TState] whose value is generated with [transform] by combining the current values of + * each given [TState]. + * + * @see TState.combineWith + */ +@ExperimentalFrpApi +fun combine( + stateA: TState, + stateB: TState, + stateC: TState, + stateD: TState, + stateE: TState, + transform: suspend FrpScope.(A, B, C, D, E) -> Z, +): TState { + val operatorName = "combine" + val name = operatorName + return TStateInit( + init(name) { + coroutineScope { + val dl1: Deferred> = async { + stateA.init.connect(evalScope = this@init) + } + val dl2: Deferred> = async { + stateB.init.connect(evalScope = this@init) + } + val dl3: Deferred> = async { + stateC.init.connect(evalScope = this@init) + } + val dl4: Deferred> = async { + stateD.init.connect(evalScope = this@init) + } + val dl5: Deferred> = async { + stateE.init.connect(evalScope = this@init) + } + zipStates( + name, + operatorName, + dl1.await(), + dl2.await(), + dl3.await(), + dl4.await(), + dl5.await(), + ) { a, b, c, d, e -> + NoScope.runInFrpScope { transform(a, b, c, d, e) } + } + } + } + ) +} + /** Returns a [TState] by applying [transform] to the value held by the original [TState]. */ @ExperimentalFrpApi fun TState.flatMap(transform: suspend FrpScope.(A) -> TState): TState { diff --git a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/internal/TStateImpl.kt b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/internal/TStateImpl.kt index 5cec05c8ef2d..c68b4c366776 100644 --- a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/internal/TStateImpl.kt +++ b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/internal/TStateImpl.kt @@ -314,6 +314,28 @@ internal fun zipStates( @Suppress("UNCHECKED_CAST") transform(a as A, b as B, c as C, d as D) } +internal fun zipStates( + name: String?, + operatorName: String, + l1: TStateImpl, + l2: TStateImpl, + l3: TStateImpl, + l4: TStateImpl, + l5: TStateImpl, + transform: suspend EvalScope.(A, B, C, D, E) -> Z, +): TStateImpl = + zipStates(null, operatorName, mapOf(0 to l1, 1 to l2, 2 to l3, 3 to l4, 4 to l5)).map( + name, + operatorName, + ) { + val a = it.getValue(0) + val b = it.getValue(1) + val c = it.getValue(2) + val d = it.getValue(3) + val e = it.getValue(4) + @Suppress("UNCHECKED_CAST") transform(a as A, b as B, c as C, d as D, e as E) + } + internal fun zipStates( name: String?, operatorName: String, -- cgit v1.2.3-59-g8ed1b