Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0cb42cdc authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Ibb14d99a,Ia5852ccd,I064b1371 into main

* changes:
  [kairos] fix MutableTFlow backpressure
  [kairos] Add TState.transitions utility
  [kairos] add Flow.scanTo utilities
parents 6e2d093d 60f5dd1b
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package com.android.systemui.kairos

import com.android.systemui.kairos.util.These
import com.android.systemui.kairos.util.WithPrev
import com.android.systemui.kairos.util.just
import com.android.systemui.kairos.util.none
import kotlinx.coroutines.flow.Flow
@@ -238,3 +239,14 @@ val <A> FrpStatefulMode<A>.compiledStateful: FrpStateful<TState<A>>
 */
fun <A> FrpBuildScope.rebuildOn(rebuildSignal: TFlow<*>, spec: FrpSpec<A>): TState<A> =
    rebuildSignal.map { spec }.holdLatestSpec(spec)

/**
 * Like [stateChanges] but also includes the old value of this [TState].
 *
 * Shorthand for:
 * ``` kotlin
 *     stateChanges.map { WithPrev(previousValue = sample(), newValue = it) }
 * ```
 */
val <A> TState<A>.transitions: TFlow<WithPrev<A, A>>
    get() = stateChanges.map { WithPrev(previousValue = sample(), newValue = it) }
+21 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.dropWhile
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.launch

/** A function that modifies the FrpNetwork. */
@@ -595,6 +596,26 @@ interface FrpBuildScope : FrpStateScope {
    @ExperimentalFrpApi
    fun <A> Flow<A>.toTState(initialValue: A): TState<A> = toTFlow().hold(initialValue)

    /**
     * Shorthand for:
     * ```kotlin
     * flow.scan(initialValue, operation).toTFlow().hold(initialValue)
     * ```
     */
    @ExperimentalFrpApi
    fun <A, B> Flow<A>.scanToTState(initialValue: B, operation: (B, A) -> B): TState<B> =
        scan(initialValue, operation).toTFlow().hold(initialValue)

    /**
     * Shorthand for:
     * ```kotlin
     * flow.scan(initialValue) { a, f -> f(a) }.toTFlow().hold(initialValue)
     * ```
     */
    @ExperimentalFrpApi
    fun <A> Flow<(A) -> A>.scanToTState(initialValue: A): TState<A> =
        scanToTState(initialValue) { a, f -> f(a) }

    /**
     * Invokes [block] whenever this [TFlow] emits a value. [block] receives an [FrpBuildScope] that
     * can be used to make further modifications to the FRP network, and/or perform side-effects via
+4 −6
Original line number Diff line number Diff line
@@ -520,16 +520,14 @@ internal constructor(internal val network: Network, internal val impl: InputNode
    @ExperimentalFrpApi
    suspend fun emit(value: T) {
        coroutineScope {
            var jobOrNull: Job? = null
            val newEmit =
                async(start = CoroutineStart.LAZY) {
                    jobOrNull?.join()
                    network.transaction { impl.visit(this, value) }.await()
                }
            val jobOrNull = storage.getAndSet(newEmit)
            if (jobOrNull?.isActive != true) {
            jobOrNull = storage.getAndSet(newEmit)
            newEmit.await()
            } else {
                jobOrNull.join()
            }
        }
    }