-
Couldn't load subscription status.
- Fork 1.9k
Description
Use case
Hello,
I need to implement a terminal flow truncating operator, and having access to collectWhile would be nice.
The operator I'm trying to do is niche, it's similar to firstNotNullOfOrNull in some way, but also similar to onEach or transform.
What I want to do is to have an extension for FlowCollector<T> that emits everything from a source Flow<T>, and once the passed block returns a non null value, the source flow is truncated (after the last T value is emitted), and the result of the block is returned:
suspend fun <T, R : Any> FlowCollector<T>.emitUntilNonNull(source: Flow<T>, block: (T) -> R?): RImplementing this without collectWhile, and without the ability to return/escape from collect is more cumbersome and a bit less efficient as I need to use a combination of transformWhile and collect instead of just collectWhile.