combineLatest(Iterable<Flow>) operator
See original GitHub issuePlease add a combineLatest
operator which accepts a list of Flows. Here is an example of my use case:
fun <T> combineLatest(flows: Iterable<Flow<T>>): Flow<List<T>> = TODO()
// A list of connected devices.
val devices: List<Device> = getDevices()
// A list of Flows which receive the state of the device every time it changes.
val flows: List<Flow<DeviceState>> = devices.map { device ->
device.observeState() // Flow<DeviceState>
}
// Combine the latest state of the devices into a single Flow.
val flow: Flow<List<DeviceState>> = combineLatest(flows)
// Somewhere else in the code
flow.collect { states ->
for (state in states) {
// TODO: do something with the states.
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
combine - Kotlin
Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow. Sources. common source....
Read more >CombineLatest operator - ReactiveX
RxPHP implements this operator as combineLatest . Merges the specified observable sequences into one observable sequence by using the selector function whenever ...
Read more >Flowable (RxJava Javadoc 2.1.10)
The Flowable class that implements the Reactive-Streams Pattern and offers factory methods, intermediate operators and the ability to consume reactive ...
Read more >Flux (reactor-core 3.5.1)
Concatenate all sources provided in an Iterable , forwarding elements emitted by the sources downstream. Concatenate all sources emitted as an onNext signal ......
Read more >An Early look at Kotlin Coroutine's Flow - ProAndroidDev
asFlow()Iterable<T>. ... Flow is not currently as operator rich as RxJava but as at when this post was written, there are 27 operators...
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
I find
flow.combineLatest(other)
only useful for that single overload. If there are more than 2 sources I do strongly prefer a factory method.For example if we take this one: https://github.com/Kotlin/kotlinx.coroutines/blob/f77533cb7840afd0afa5fa4a97da6fd22f84f975/kotlinx-coroutines-core/common/src/flow/operators/Zip.kt#L120-L134
This makes no sense for me. It’s not that I want to combine source
A
with the latest ofB,C,D,E
. Instead all these sources are from the user perspective equally important. If we look at this signature, it implies as if the receiver flow is different from the flows passed as arguments.Just to add on, I find the first example below easier to read than the second.
flow1
has no additional significance overflow2
, so I find seeing them written in parallel easier to read because they emit in parallel