Package kotlinx.coroutines.channels

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

Types

ActorScope
Link copied to clipboard
interface ActorScope<E> : CoroutineScope, ReceiveChannel<E>
Scope for actor coroutine builder.
BroadcastChannel
Link copied to clipboard
common
interface BroadcastChannel<E> : SendChannel<E>
Broadcast channel is a non-blocking primitive for communication between the sender and multiple receivers that subscribe for the elements using openSubscription function and unsubscribe using ReceiveChannel.cancel function.
BufferOverflow
Link copied to clipboard
common
enum BufferOverflow : Enum<BufferOverflow>
A strategy for buffer overflow handling in channels and flows that controls what is going to be sacrificed on buffer overflow:
  • SUSPEND &mdash; the upstream that is sending or is emitting a value is suspended while the buffer is full.

  • DROP_OLDEST &mdash; drop the oldest value in the buffer on overflow, add the new value to the buffer, do not suspend.

  • DROP_LATEST &mdash; drop the latest value that is being added to the buffer right now on buffer overflow (so that buffer contents stay the same), do not suspend.

Channel
Link copied to clipboard
common
interface Channel<E> : SendChannel<E> , ReceiveChannel<E>
Channel is a non-blocking primitive for communication between a sender (via SendChannel) and a receiver (via ReceiveChannel).
ChannelIterator
Link copied to clipboard
common
interface ChannelIterator<out E>
Iterator for ReceiveChannel.
ChannelResult
Link copied to clipboard
common
value class ChannelResult<out T>
A discriminated union of channel operation result.
ClosedReceiveChannelException
Link copied to clipboard
common
class ClosedReceiveChannelException(message: String?) : NoSuchElementException
Indicates an attempt to receive from a isClosedForReceive channel that was closed without a cause.
ClosedSendChannelException
Link copied to clipboard
common
class ClosedSendChannelException(message: String?) : IllegalStateException
Indicates an attempt to send to a isClosedForSend channel that was closed without a cause.
ConflatedBroadcastChannel
Link copied to clipboard
common
class ConflatedBroadcastChannel<E> : BroadcastChannel<E>
Broadcasts the most recently sent element (aka value) to all openSubscription subscribers.
ProducerScope
Link copied to clipboard
common
interface ProducerScope<in E> : CoroutineScope, SendChannel<E>
Scope for the produce coroutine builder.
ReceiveChannel
Link copied to clipboard
common
interface ReceiveChannel<out E>
Receiver's interface to Channel.
SendChannel
Link copied to clipboard
common
interface SendChannel<in E>
Sender's interface to Channel.
TickerMode
Link copied to clipboard
enum TickerMode : Enum<TickerMode>
Mode for ticker function.

Functions

actor
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.
awaitClose
Link copied to clipboard
common
suspend fun ProducerScope<*>.awaitClose(block: () -> Unit = {})
Suspends the current coroutine until the channel is either closed or cancelled and invokes the given block before resuming the coroutine.
broadcast
Link copied to clipboard
common
fun <E> ReceiveChannel<E>.broadcast(capacity: Int = 1, start: CoroutineStart = CoroutineStart.LAZY): BroadcastChannel<E>
Broadcasts all elements of the channel.
fun <E> CoroutineScope.broadcast(context: CoroutineContext = EmptyCoroutineContext, capacity: Int = 1, start: CoroutineStart = CoroutineStart.LAZY, onCompletion: CompletionHandler? = null, block: suspend ProducerScope<E>.() -> Unit): BroadcastChannel<E>
Launches new coroutine to produce a stream of values by sending them to a broadcast channel and returns a reference to the coroutine as a BroadcastChannel.
BroadcastChannel
Link copied to clipboard
common
fun <E> BroadcastChannel(capacity: Int): BroadcastChannel<E>
Creates a broadcast channel with the specified buffer capacity.
Channel
Link copied to clipboard
common
fun <E> Channel(capacity: Int = RENDEZVOUS): Channel<E>
fun <E> Channel(capacity: Int = RENDEZVOUS, onBufferOverflow: BufferOverflow = BufferOverflow.SUSPEND, onUndeliveredElement: (E) -> Unit? = null): Channel<E>
Creates a channel with the specified buffer capacity (or without a buffer by default).
consume
Link copied to clipboard
common
inline fun <E, R> BroadcastChannel<E>.consume(block: ReceiveChannel<E>.() -> R): R
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
Makes sure that the given block consumes all elements from the given channel by always invoking cancel after the execution of the block.
consumeEach
Link copied to clipboard
common
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 after the execution of the block.
getOrElse
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.getOrElse(onFailure: (exception: Throwable?) -> T): T
Returns the encapsulated value if this instance represents success or the result of onFailure function for the encapsulated Throwable exception if it is failed or closed result.
onClosed
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onClosed(action: (exception: Throwable?) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated Throwable exception if this instance represents failure due to channel being closed.
onFailure
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onFailure(action: (exception: Throwable?) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated Throwable exception if this instance represents failure.
onReceiveOrNull
Link copied to clipboard
common
fun <E : Any> ReceiveChannel<E>.onReceiveOrNull(): SelectClause1<E?>
This function is deprecated in the favour of ReceiveChannel.onReceiveCatching
onSuccess
Link copied to clipboard
common
inline fun <T> ChannelResult<T>.onSuccess(action: (T) -> Unit): ChannelResult<T>
Performs the given action on the encapsulated value if this instance represents success.
produce
Link copied to clipboard
common
fun <E> CoroutineScope.produce(context: CoroutineContext = EmptyCoroutineContext, capacity: Int = 0, 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.
sendBlocking
Link copied to clipboard
fun <E> SendChannel<E>.sendBlocking(element: E)
Deprecated blocking variant of send.
ticker
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.
toList
Link copied to clipboard
common
suspend fun <E> ReceiveChannel<E>.toList(): List<E>
Returns a List containing all elements.
trySendBlocking
Link copied to clipboard
fun <E> SendChannel<E>.trySendBlocking(element: E): ChannelResult<Unit>
Adds element into 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.