ReceiveChannel

interface ReceiveChannel<out E>(source)

Receiver's interface to Channel.

Inheritors

Properties

Link copied to clipboard

Returns true if this channel was closed by invocation of close on the SendChannel side and all previously sent items were already received, or if the receiving side was cancelled.

Link copied to clipboard

Returns true if the channel is empty (contains no elements), which means that an attempt to receive will suspend. This function returns false if the channel is closed for receive.

Link copied to clipboard
abstract val onReceive: SelectClause1<E>

Clause for the select expression of the receive suspending function that selects with the element received from the channel. The select invocation fails with an exception if the channel is closed for receive (see close for details).

Link copied to clipboard

Clause for the select expression of the onReceiveCatching suspending function that selects with the ChannelResult with a value that is received from the channel or with a close cause if the channel is closed for receive.

Functions

Link copied to clipboard
fun <E> ReceiveChannel<E>.broadcast(capacity: Int = 1, start: CoroutineStart = CoroutineStart.LAZY): BroadcastChannel<E>

Broadcasts all elements of the channel. This function consumes all elements of the original ReceiveChannel.

Link copied to clipboard
abstract fun cancel(cause: CancellationException? = null)

Cancels reception of remaining elements from this channel with an optional cause. This function closes the channel and removes all buffered sent elements from it.

Link copied to clipboard
inline fun <E, R> ReceiveChannel<E>.consume(block: ReceiveChannel<E>.() -> R): R

Makes sure that the given block consumes all elements from the given channel by always invoking cancel after the execution of the block.

Link copied to clipboard

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.

Link copied to clipboard
inline suspend fun <E> ReceiveChannel<E>.consumeEach(action: (E) -> Unit)

Performs the given action for each received element and cancels the channel after the execution of the block. If you need to iterate over the channel without consuming it, a regular for loop should be used instead.

Link copied to clipboard
abstract operator fun iterator(): ChannelIterator<E>

Returns a new iterator to receive elements from this channel using a for loop. Iteration completes normally when the channel is closed for receive without a cause and throws the original close cause exception if the channel has failed.

Link copied to clipboard
abstract suspend fun receive(): E

Retrieves and removes an element from this channel if it's not empty, or suspends the caller while the channel is empty, or throws a ClosedReceiveChannelException if the channel is closed for receive. If the channel was closed because of an exception, it is called a failed channel and this function will throw the original close cause exception.

Link copied to clipboard

Represents the given receive channel as a hot flow and receives from the channel in fan-out fashion every time this flow is collected. One element will be emitted to one collector only.

Link copied to clipboard
abstract suspend fun receiveCatching(): ChannelResult<E>

Retrieves and removes an element from this channel if it's not empty, or suspends the caller while this channel is empty. This method returns ChannelResult with the value of an element successfully retrieved from the channel or the close cause if the channel was closed. Closed cause may be null if the channel was closed normally. The result cannot be failed without being closed.

Link copied to clipboard
suspend fun <E> ReceiveChannel<E>.toList(): List<E>

Returns a List containing all elements.

Link copied to clipboard
abstract fun tryReceive(): ChannelResult<E>

Retrieves and removes an element from this channel if it's not empty, returning a successful result, returns failed result if the channel is empty, and closed result if the channel is closed.