summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Steve Elliott <steell@google.com> 2022-09-08 20:54:16 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2022-09-08 20:54:16 +0000
commita117bbe3f218e493cdd212793f88c0ecb39744cd (patch)
treefdb694b6ecec8724600d961cc65def9686f6b07b
parent6451a04d78dfb7b7d0c8210b66f37004238b524c (diff)
parentbdc27839b22382337d8ff5020a8a6200d37d39e9 (diff)
Merge "Make pairwise() only subscribe to upstream once" into tm-qpr-dev
-rw-r--r--packages/SystemUI/src/com/android/systemui/util/kotlin/Flow.kt23
1 files changed, 12 insertions, 11 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/util/kotlin/Flow.kt b/packages/SystemUI/src/com/android/systemui/util/kotlin/Flow.kt
index 729fdfeb2ffb..84305cc46ac3 100644
--- a/packages/SystemUI/src/com/android/systemui/util/kotlin/Flow.kt
+++ b/packages/SystemUI/src/com/android/systemui/util/kotlin/Flow.kt
@@ -17,10 +17,10 @@
package com.android.systemui.util.kotlin
import kotlinx.coroutines.flow.Flow
+import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.distinctUntilChanged
-import kotlinx.coroutines.flow.drop
+import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.onStart
-import kotlinx.coroutines.flow.zip
/**
* Returns a new [Flow] that combines the two most recent emissions from [this] using [transform].
@@ -29,15 +29,16 @@ import kotlinx.coroutines.flow.zip
*
* Useful for code that needs to compare the current value to the previous value.
*/
-fun <T, R> Flow<T>.pairwiseBy(transform: suspend (old: T, new: T) -> R): Flow<R> {
- // same as current flow, but with the very first event skipped
- val nextEvents = drop(1)
- // zip current flow and nextEvents; transform will receive a pair of old and new value. This
- // works because zip will suppress emissions until both flows have emitted something; since in
- // this case both flows are emitting at the same rate, but the current flow just has one extra
- // thing emitted at the start, the effect is that zip will cache the most recent value while
- // waiting for the next emission from nextEvents.
- return zip(nextEvents, transform)
+fun <T, R> Flow<T>.pairwiseBy(transform: suspend (old: T, new: T) -> R): Flow<R> = flow {
+ val noVal = Any()
+ var previousValue: Any? = noVal
+ collect { newVal ->
+ if (previousValue != noVal) {
+ @Suppress("UNCHECKED_CAST")
+ emit(transform(previousValue as T, newVal))
+ }
+ previousValue = newVal
+ }
}
/**