Skip to content

Consider making collectWhile public (instead of internal) #4555

@LouisCAD

Description

@LouisCAD

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?): R

Implementing 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions