Something like channel.receiveWhen { predicate }. Might be useful if there are multiple worker coroutines, each one processing its own subset of elements.
Something like channel.receiveUnless{ predicate } would be also interesting.
How much is different to:
myChannel.openSubscription().use { subscription ->
subscription .filter { predicate } .receive()
}
The most general -unresolved- question is: should a regular Channel be both one-to-one and one-to-many?
@fvasco The key difference here is that an element sent to Channel is never lost. It must be received. However, an element sent to BroadcastChannel will be lost if there are no subscribers. It is especially important when creating back-end pipelines to process event, perform protocol conversion, etc.
I see this being a
fun ReceiveChannel<T>.split(predicate: (T) -> Boolean): Pair<ReceiveChannel<T>, ReceiveChannel<T>>, where true goes to the first channel and false goes to the second one. This way one doesn't have to implement some form of peek().
is it still in plans or this is an abandoned issue? Or maybe there is another way to do it?
It is abandoned for a reason. Thanks for bringing attention to it. It is conceptually really hard to implement. If you need something like it, then launch a separate coroutine to perform filtering, so that it sends the correspondingly filtered items to the appropriate downsteam channels.
Got it, thanks for the quick response
Most helpful comment
@fvasco The key difference here is that an element sent to
Channelis never lost. It must be received. However, an element sent toBroadcastChannelwill be lost if there are no subscribers. It is especially important when creating back-end pipelines to process event, perform protocol conversion, etc.