ProducerScope

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

Scope for the produce coroutine builder.

Note: This is an experimental api. Behavior 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 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 opposed to this. All the SendChannel functions on this interface delegate to the channel instance returned by this property.

Inherited Properties

coroutineContext

abstract val coroutineContext: CoroutineContext

The context of this scope. Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope. Accessing this property in general code is not recommended for any purposes except accessing the Job instance for advanced usages.

isClosedForSend

abstract val isClosedForSend: Boolean

Returns true if this channel was closed by an invocation of close. This means that calling send or offer will result in an exception.

onSend

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

Clause for the select expression of the send suspending function that selects when the element that is specified as the 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 — subsequent 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 a handler which is synchronously invoked once the channel is closed or the receiving side of this channel is cancelled. Only one handler can be attached to a channel during its lifetime. The handler is invoked when isClosedForSend starts to return true. If the channel is closed already, the handler is invoked immediately.

offer

abstract fun offer(element: E): Boolean

Immediately adds the specified element to this channel, if this doesn’t violate its capacity restrictions, and returns true. Otherwise, just returns false. This is a synchronous variant of send which backs off in situations when send suspends.

send

abstract suspend fun send(element: E): Unit

Sends the specified element to this channel, suspending the caller while the buffer of this channel is full or if it does not exist, or throws an exception if the channel is closed for send (see close for details).

Extension Properties

isActive

val CoroutineScope.isActive: Boolean

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

Extension Functions

async

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

Creates a coroutine and returns its future result as an implementation of Deferred. The running coroutine is cancelled when the resulting deferred is cancelled. The resulting coroutine has a key difference compared with similar primitives in other languages and frameworks: it cancels the parent job (or outer scope) on failure to enforce structured concurrency paradigm. To change that behaviour, supervising parent (SupervisorJob or supervisorScope) can be used.

awaitClose

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

Suspends the current coroutine until the channel is either closed or cancelled and invokes the given block before resuming the coroutine. This suspending function is cancellable.

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.

cancel

fun CoroutineScope.cancel(
    cause: CancellationException? = null
): Unit

Cancels this scope, including its job and all its children with an optional cancellation cause. A cause can be used to specify an error message or to provide other details on a cancellation reason for debugging purposes. Throws IllegalStateException if the scope does not have a job in it.

fun CoroutineScope.cancel(
    message: String,
    cause: Throwable? = null
): Unit

Cancels this scope, including its job and all its children with a specified diagnostic error message. A cause can be specified to provide additional details on a cancellation reason for debugging purposes. Throws IllegalStateException if the scope does not have a job in it.

ensureActive

fun CoroutineScope.ensureActive(): Unit

Ensures that current scope is active. Throws IllegalStateException if the context does not have a job in it.

launch

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

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

newCoroutineContext

expect fun CoroutineScope.newCoroutineContext(
    context: CoroutineContext
): CoroutineContext

Creates a context for the new coroutine. It installs Dispatchers.Default when no other dispatcher or ContinuationInterceptor is specified, and adds optional support for debugging facilities (when turned on).

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