summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/debug/Debug.kt23
1 files changed, 23 insertions, 0 deletions
diff --git a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/debug/Debug.kt b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/debug/Debug.kt
index 4f302a14ff00..0674a2e75659 100644
--- a/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/debug/Debug.kt
+++ b/packages/SystemUI/utils/kairos/src/com/android/systemui/kairos/debug/Debug.kt
@@ -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() }
+ }