transformLatest

@ExperimentalCoroutinesApi fun <T, R> Flow<T>.transformLatest(
    transform: suspend FlowCollector<R>.(value: T) -> Unit
): Flow<R>
(source)

Returns a flow that produces element by transform function every time the original flow emits a value. When the original flow emits a new value, the previous transform block is cancelled, thus the name transformLatest.

For example, the following flow:

flow {
    emit("a")
    delay(100)
    emit("b")
}.transformLatest { value ->
    emit(value)
    delay(200)
    emit(value + "_last")
}

produces a b b_last.

This operator is buffered by default and size of its output buffer can be changed by applying subsequent buffer operator.