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

Commit 6943fa9e authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] add Flow.scanTo utilities

Flag: EXEMPT unused
Test: atest kairos-tests
Change-Id: I064b1371f648f916918bb01579591c9f7d828711
parent 7ae1f019
Loading
Loading
Loading
Loading
+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