Flow: withLatestFrom operator
See original GitHub issueSee #1315 for one of the use-cases. Note, that is operator can be implemented in a quite a straightforward way using stable Flow
APIs. The simple implementation is given below:
fun <A, B: Any, R> Flow<A>.withLatestFrom(other: Flow<B>, transform: suspend (A, B) -> R): Flow<R> = flow {
coroutineScope {
val latestB = AtomicReference<B?>()
val outerScope = this
launch {
try {
other.collect { latestB.set(it) }
} catch(e: CancellationException) {
outerScope.cancel(e) // cancel outer scope on cancellation exception, too
}
}
collect { a: A ->
latestB.get()?.let { b -> emit(transform(a, b)) }
}
}
}
TODO: We need to figure out a name for this operation that is consistent with combine
and xxxLatest
naming convention. Even better, it should be a part of the customizable “combine family” of operations where each source flow can either react to the the incoming values or just reference the most recent value. It is also related to #1354 and #1082 which would introduce the concept of StateFlow
(a flow with the most recent value).
Issue Analytics
- State:
- Created 4 years ago
- Reactions:22
- Comments:23 (7 by maintainers)
Top Results From Across the Web
Is there a withLatestFrom Equivalent with Coroutines Flow?
RxJava has a couple of extremely useful operators, withLatestFrom , throttleFirst , etc. and I can't seem to find an equivalent in coroutines ......
Read more >Composing functions and operators, zip combineLatest and ...
In this articleWe will review Zip combineLatest and withLatestFrom operator for RxJava and how to achieve same using Flow. zip. Zip combine the ......
Read more >transformLatest
Returns a flow that produces element by transform function every time the original flow emits a value. When the original flow emits a...
Read more >withLatestFrom - Learn RxJS
import { withLatestFrom, map } from 'rxjs/operators';. import { interval } from 'rxjs';. . //emit every 5s. const source = interval(5000);. //emit every...
Read more >Implement Kotlin Flow race/amb operator - ProAndroidDev
Hello Kotlin Developers, let's implement race/amb operator for Flow. This operator is similar to race of rxjs or amb of RxJava. You can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Seeing as #1315 seems to have stalled since 2019. Seeing as StateFlow has been released, is there anything blocking the implementation of
withLatestFrom
? similarly to @uburoiubu I too find this operator very useful for reactive UI programming and would gladly help on its implementation if needed.I have added it in https://github.com/hoc081098/FlowExt library for anyone who needs it