ProducerScope

Scope for the produce, callbackFlow and channelFlow builders.

Properties

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

Extensions

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

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

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

Link copied to clipboard

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)

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.

Link copied to clipboard

Ensures that current scope is active.

Link copied to clipboard

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

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

Link copied to clipboard

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

Creates a context for a new coroutine. It installs Dispatchers.Default when no other dispatcher or ContinuationInterceptor is specified and adds optional support for debugging facilities (when turned on) and copyable-thread-local facilities on JVM. See DEBUG_PROPERTY_NAME for description of debugging facilities on JVM.

Link copied to clipboard

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

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

Link copied to clipboard
fun <T> CoroutineScope.promise(    context: CoroutineContext = EmptyCoroutineContext,     start: CoroutineStart = CoroutineStart.DEFAULT,     block: suspend CoroutineScope.() -> T): Promise<T>

Starts new coroutine and returns its result as an implementation of Promise.

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.