question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Flow: withLatestFrom operator

See original GitHub issue

See #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:open
  • Created 4 years ago
  • Reactions:22
  • Comments:23 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
Progdrasilcommented, Dec 17, 2020

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.

2reactions
hoc081098commented, Jul 20, 2021

I have added it in https://github.com/hoc081098/FlowExt library for anyone who needs it

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found