launchConsumeEach terminal operator (name needed)
See original GitHub issueConflatedBroadcastChannel
is a good fit for dataflow programming. It represents an asynchronously updatable value and its consuming operation “does the right thing” of delivering the most recent value and all the subsequent update. In the domain of dataflow programming the common operation is to process each value in a separate coroutine. For example, in Android:
val x = ConflatedBroadcastChannel() // a model for some value x
launch(UI) {
x.consumeEach { someView.text = it.toString }
}
This launch(context) { consumeEach { ... } }
pattern is very common, so some dedicated terminal operator seems to be in order for it. Possible names are:
launchConsumeEach
is an obvious, but verbose choice, see https://github.com/dmytrodanylyk/coroutines-arch/blob/master/library/src/main/java/com/kotlin/arch/ChannelsExt.kt by @dmytrodanylyk
The challenge that we have is that all terminal operators currently defined on ReceiveChannel
are suspending functions that work in the context of invoker. All non-suspending operator that receive context
as parameter perform some kind of transform and return another representation. This immediately suggests that launchConsumeEach
become such an intermediate operator and its result should be a Job
. But can we find a better name for it? Some verb in style of the other operators we have?
Issue Analytics
- State:
- Created 5 years ago
- Reactions:3
- Comments:18 (18 by maintainers)
Top GitHub Comments
We’ve been designing the similar operator for #254 cold stream and the idea we’ve decided to go with will be usable for channels, too If you have a
channel
it would look like this:The other way to use this API would be like this:
A correction: The released replacement for the proposed
channel.launchConsumeEach(scope) { block }
ischannel.asFlow().onEach { block }.launchIn(scope)