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

Commit 00ad67ff authored by Steve Elliott's avatar Steve Elliott
Browse files

[kairos] add unsafe sample debug util

Flag: EXEMPT unused
Test: atest kairos-tests
Change-Id: Ib74803cce73c287efce76ecc41efddafab799352
parent 804a064a
Loading
Loading
Loading
Loading
+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() }
    }