Package-level declarations

Channels — non-blocking primitives for communicating a stream of elements between coroutines.

Channels — non-blocking primitives for communicating a stream of elements between coroutines.

Channels — non-blocking primitives for communicating a stream of elements between coroutines.

Types

Link copied to clipboard

Scope for actor coroutine builder.

Link copied to clipboard

A strategy for buffer overflow handling in channels and flows that controls what is going to be sacrificed on buffer overflow:

Link copied to clipboard

Channel is a non-blocking primitive for communication between a sender (via SendChannel) and a receiver (via ReceiveChannel). Conceptually, a channel is similar to java.util.concurrent.BlockingQueue, but it has suspending operations instead of blocking ones and can be closed.

Link copied to clipboard
interface ChannelIterator<out E>

Iterator for a ReceiveChannel. Instances of this interface are not thread-safe and shall not be used from concurrent coroutines.

Link copied to clipboard
value class ChannelResult<out T>

A discriminated union representing a channel operation result. It encapsulates the knowledge of whether the operation succeeded, failed with an option to retry, or failed because the channel was closed.

Link copied to clipboard

Indicates an attempt to receive from a closed-for-receiving channel that was closed without a cause.

Link copied to clipboard

Indicates an attempt to send to a closed-for-sending channel that was closed without a cause.

Link copied to clipboard

Scope for the produce, callbackFlow and channelFlow builders.

Link copied to clipboard
interface ReceiveChannel<out E>

Receiver's interface to a Channel.

Link copied to clipboard
interface SendChannel<in E>

Sender's interface to a Channel.

Link copied to clipboard

Functions

Link copied to clipboard
fun <E> CoroutineScope.actor(context: CoroutineContext = EmptyCoroutineContext, capacity: Int = 0, start: CoroutineStart = CoroutineStart.DEFAULT, onCompletion: CompletionHandler? = null, block: suspend ActorScope<E>.() -> Unit): SendChannel<E>

Launches new coroutine that is receiving messages from its mailbox channel and returns a reference to its mailbox channel as a SendChannel. The resulting object can be used to send messages to this coroutine.

Link copied to clipboard
suspend fun ProducerScope<*>.awaitClose(block: () -> Unit = {})

Suspends the current coroutine until the channel is either closed or cancelled.

Link copied to clipboard
fun <E> Channel(capacity: Int = RENDEZVOUS, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, onUndeliveredElement: (E) -> Unit? = null): Channel<E>

Creates a channel. See the Channel interface documentation for details.

Link copied to clipboard

Opens subscription to this BroadcastChannel and makes sure that the given block consumes all elements from it by always invoking cancel after the execution of the block.

inline fun <E, R> ReceiveChannel<E>.consume(block: ReceiveChannel<E>.() -> R): R

Executes the block and then cancels the channel.

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

Subscribes to this BroadcastChannel and performs the specified action for each received element.

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
inline fun <T> ChannelResult<T>.getOrElse(onFailure: (exception: Throwable?) -> T): T

Returns the encapsulated value if the operation succeeded, or the result of onFailure function for ChannelResult.exceptionOrNull otherwise.

Link copied to clipboard
inline fun <T> ChannelResult<T>.onClosed(action: (exception: Throwable?) -> Unit): ChannelResult<T>

Performs the given action if the operation failed because the channel was closed for that operation. The result of ChannelResult.exceptionOrNull is passed to the action parameter.

Link copied to clipboard
inline fun <T> ChannelResult<T>.onFailure(action: (exception: Throwable?) -> Unit): ChannelResult<T>

Performs the given action if the operation failed. The result of ChannelResult.exceptionOrNull is passed to the action parameter.

Link copied to clipboard
inline fun <T> ChannelResult<T>.onSuccess(action: (value: T) -> Unit): ChannelResult<T>

Performs the given action on the encapsulated value if the operation succeeded. Returns the original ChannelResult unchanged.

Link copied to clipboard
fun <E> CoroutineScope.produce(context: CoroutineContext = EmptyCoroutineContext, capacity: Int = Channel.RENDEZVOUS, block: suspend ProducerScope<E>.() -> Unit): ReceiveChannel<E>

Launches a new coroutine to produce a stream of values by sending them to a channel and returns a reference to the coroutine as a ReceiveChannel. This resulting object can be used to receive elements produced by this coroutine.

Link copied to clipboard
fun ticker(delayMillis: Long, initialDelayMillis: Long = delayMillis, context: CoroutineContext = EmptyCoroutineContext, mode: TickerMode = TickerMode.FIXED_PERIOD): ReceiveChannel<Unit>

Creates a channel that produces the first item after the given initial delay and subsequent items with the given delay between them.

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

Adds element to this channel, blocking the caller while this channel is full, and returning either successful result when the element was added, or failed result representing closed channel with a corresponding exception.