transformWhile
fun <T, R> Flow<T>.transformWhile(transform: suspend FlowCollector<R>.(value: T) -> Boolean): Flow<R>(source)
Applies transform function to each value of the given flow while this function returns true
.
The receiver of the transformWhile
is FlowCollector and thus transformWhile
is a flexible function that may transform emitted element, skip it or emit it multiple times.
This operator generalizes takeWhile and can be used as a building block for other operators. For example, a flow of download progress messages can be completed when the download is done but emit this last message (unlike takeWhile
):
fun Flow<DownloadProgress>.completeWhenDone(): Flow<DownloadProgress> =
transformWhile { progress ->
emit(progress) // always emit progress
!progress.isDone() // continue while download is not done
}
Content copied to clipboard