ProducerScope

@ExperimentalCoroutinesApi interface ProducerScope<in E> : 
    CoroutineScope,
    SendChannel<E>
(source)

Scope for produce coroutine builder.

Note: This is an experimental api. Behaviour of producers that work as children in a parent scope with respect to cancellation and error handling may change in the future.

Properties

channel

abstract val channel: SendChannel<E>

A reference to the channel that this coroutine sends elements to. It is provided for convenience, so that the code in the coroutine can refer to the channel as channel as apposed to this. All the SendChannel functions on this interface delegate to the channel instance returned by this function.

Inherited Properties

coroutineContext

abstract val coroutineContext: CoroutineContext

Context of this scope.

isClosedForSend

abstract val isClosedForSend: Boolean

Returns true if this channel was closed by invocation of close and thus the send and offer attempts throws exception.

isFull

abstract val isFull: Boolean

Returns true if the channel is full (out of capacity) and the send attempt will suspend. This function returns false for isClosedForSend channel.

onSend

abstract val onSend: SelectClause2<E, SendChannel<E>>

Clause for select expression of send suspending function that selects when the element that is specified as parameter is sent to the channel. When the clause is selected the reference to this channel is passed into the corresponding block.

Inherited Functions

close

abstract fun close(cause: Throwable? = null): Boolean

Closes this channel. This is an idempotent operation – repeated invocations of this function have no effect and return false. Conceptually, its sends a special “close token” over this channel.

invokeOnClose

abstract fun invokeOnClose(
    handler: (cause: Throwable?) -> Unit
): Unit

Registers handler which is synchronously invoked once the channel is closed or receiving side of this channel is cancelled. Only one handler can be attached to the channel during channel’s lifetime. Handler is invoked when isClosedForSend starts to return true. If channel is already closed, handler is invoked immediately.

offer

abstract fun offer(element: E): Boolean

Adds element into this queue if it is possible to do so immediately without violating capacity restrictions and returns true. Otherwise, it returns false immediately or throws exception if the channel isClosedForSend (see close for details).

send

abstract suspend fun send(element: E): Unit

Adds element into to this channel, suspending the caller while this channel isFull, or throws exception if the channel isClosedForSend (see close for details).

Extension Properties

isActive

val CoroutineScope.isActive: Boolean

Returns true when current Job is still active (has not completed and was not cancelled yet).

Extension Functions

actor

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.

async

fun <T> CoroutineScope.async(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> T
): Deferred<T>

Creates new coroutine and returns its future result as an implementation of Deferred. The running coroutine is cancelled when the resulting deferred is cancelled.

broadcast

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. The resulting object can be used to subscribe to elements produced by this coroutine.

launch

fun CoroutineScope.launch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend CoroutineScope.() -> Unit
): Job

Launches new coroutine without blocking current thread and returns a reference to the coroutine as a Job. The coroutine is cancelled when the resulting job is cancelled.

plus

operator fun CoroutineScope.plus(
    context: CoroutineContext
): CoroutineScope

Adds the specified coroutine context to this scope, overriding existing elements in the current scope’s context with the corresponding keys.

produce

fun <E> CoroutineScope.produce(
    context: CoroutineContext = EmptyCoroutineContext,
    capacity: Int = 0,
    block: suspend ProducerScope<E>.() -> Unit
): ReceiveChannel<E>

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

sendBlocking

fun <E> SendChannel<E>.sendBlocking(element: E): Unit

Adds element into to this channel, blocking the caller while this channel Channel.isFull, or throws exception if the channel Channel.isClosedForSend (see Channel.close for details).