ReceiveChannel

interface ReceiveChannel<out E>

Receiver's interface to Channel.

Functions

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

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

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

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

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

Properties

isClosedForReceive
Link copied to clipboard
abstract val isClosedForReceive: Boolean

Returns true if this channel was closed by invocation of close on the SendChannel side and all previously sent items were already received. This means that calling receive will result in a ClosedReceiveChannelException. If the channel was closed because of an exception, it is considered closed, too, but is called a failed channel. All suspending attempts to receive an element from a failed channel throw the original close cause exception.

isEmpty
Link copied to clipboard
abstract val isEmpty: Boolean

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.

onReceive
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).

onReceiveCatching
Link copied to clipboard
abstract val onReceiveCatching: SelectClause1<ChannelResult<E>>

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.

Inheritors

Channel
Link copied to clipboard
ActorScope
Link copied to clipboard

Extensions

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

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

consumeAsFlow
Link copied to clipboard
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.

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

onReceiveOrNull
Link copied to clipboard
fun <E : Any> ReceiveChannel<E>.onReceiveOrNull(): SelectClause1<E?>

This function is deprecated in the favour of ReceiveChannel.onReceiveCatching

receiveAsFlow
Link copied to clipboard
fun <T> ReceiveChannel<T>.receiveAsFlow(): Flow<T>

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.

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

Returns a List containing all elements.

Sources

common source
Link copied to clipboard