ReceiveChannel

interface ReceiveChannel<out E>(source)

Receiver's interface to a Channel.

Combined, SendChannel and ReceiveChannel define the complete Channel interface.

Inheritors

Properties

Link copied to clipboard

Returns true if the sending side of this channel was closed and all previously sent items were already received (which also happens for cancelled channels).

Link copied to clipboard

Returns true if the channel contains no elements and isn't 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.

Link copied to clipboard

Clause for the select expression of the receiveCatching suspending function that selects with a ChannelResult when an element is retrieved or the channel gets closed.

Functions

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

Closes the channel for new elements and removes all existing ones.

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

Executes the block and then cancels the channel.

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 afterward.

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 exception passed to close if there was one.

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

Retrieves an element, removing it from the channel.

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 an element, removing it from the channel.

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

Returns a List containing all the elements sent to this channel, preserving their order.

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

Attempts to retrieve an element without waiting, removing it from the channel.