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

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

Merge changes Ib74803cc,I56464da3 into main

* changes:
  [kairos] add unsafe sample debug util
  [kairos] specify variance for TStateSelector
parents 0b440afc 00ad67ff
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -418,7 +418,7 @@ fun <A> TState<A>.selector(numDistinctValues: Int? = null): TStateSelector<A> =
 * @see selector
 */
@ExperimentalFrpApi
class TStateSelector<A>
class TStateSelector<in A>
internal constructor(
    private val upstream: TState<A>,
    private val groupedChanges: GroupedTFlow<A, Boolean>,
+23 −0
Original line number Diff line number Diff line
@@ -31,6 +31,8 @@ import com.android.systemui.kairos.internal.TStateSource
import com.android.systemui.kairos.util.Just
import com.android.systemui.kairos.util.Maybe
import com.android.systemui.kairos.util.None
import com.android.systemui.kairos.util.flatMap
import com.android.systemui.kairos.util.map
import com.android.systemui.kairos.util.none
import com.android.systemui.kairos.util.orElseGet

@@ -178,3 +180,24 @@ private fun <A> TStateImpl<A>.getUnsafe(): Maybe<A> =
        is TStateSource -> getStorageUnsafe()
        is DerivedMapCheap<*, *> -> none
    }

private fun <A> TStateImpl<A>.getUnsafeWithEpoch(): Maybe<Pair<A, Long>> =
    when (this) {
        is TStateDerived -> getCachedUnsafe().map { it to invalidatedEpoch }
        is TStateSource -> getStorageUnsafe().map { it to writeEpoch }
        is DerivedMapCheap<*, *> -> none
    }

/**
 * Returns the current value held in this [TState], or [none] if the [TState] has not been
 * initialized.
 *
 * The returned [Long] is the *epoch* at which the internal cache was last updated. This can be used
 * to identify values which are out-of-date.
 */
fun <A> TState<A>.sampleUnsafe(): Maybe<Pair<A, Long>> =
    when (this) {
        is MutableTState -> tState.init.getUnsafe().flatMap { it.getUnsafeWithEpoch() }
        is TStateInit -> init.getUnsafe().flatMap { it.getUnsafeWithEpoch() }
        is TStateLoop -> this.init.getUnsafe().flatMap { it.getUnsafeWithEpoch() }
    }