consume

suspend fun <T : Any> Flow<Consumable<T>>.consume(block: suspend (T) -> Unit)

Collects results of type T and passes every result, which was consumed successfully, to block. Results, already consumed by another collector or expired, are skipped.

The collection runs until the flow completes or the surrounding coroutine is cancelled. Exceptions thrown by block propagate to the collecting coroutine.

Example:

LaunchedEffect(Unit) {
controller.observeResults<String>().consume { result ->
showMessage(result)
}
}

inline fun <T : Any> ResultSource.consume(scope: CoroutineScope, noinline block: suspend (T) -> Unit): Job

Launches collection of results of type T in scope and passes every consumed result to block.

The returned Job runs until it is cancelled or scope is cancelled: a result flow never completes on its own. Pass the scope that owns the returning destination, usually a ViewModel scope, so observation stops when the destination is cleared. Exceptions thrown by block propagate to scope and cancel it according to structured concurrency; handle them in block if the scope should survive.

Example:

controller.consume<MessageResult>(viewModelScope) { result ->
showMessage(result)
}