consumeAsFlow

fun <T> ReceiveChannel<T>.consumeAsFlow(): Flow<T>

Represents the given receive channel as a hot flow and consumes the channel on the first collection from this flow. The resulting flow can be collected just once and throws IllegalStateException when trying to collect it more than once.

See also receiveAsFlow which supports multiple collectors of the resulting flow.

Cancellation semantics

  • Flow collector is cancelled when the original channel is closed with an exception.

  • Flow collector completes normally when the original channel is closed normally.

  • If the flow collector fails with an exception, the source channel is cancelled.

Operator fusion

Adjacent applications of flowOn, buffer, conflate, and produceIn to the result of consumeAsFlow are fused. In particular, produceIn returns the original channel (but throws IllegalStateException on repeated calls). Calls to flowOn have generally no effect, unless buffer is used to explicitly request buffering.

Sources

common source
Link copied to clipboard