Package-level declarations

Select expression to perform multiple suspending operations simultaneously until one of them succeeds.

Types

Link copied to clipboard
typealias OnCancellationConstructor = (select: SelectInstance<*>, param: Any?, internalResult: Any?) -> (Throwable) -> Unit

This function specifies how the internal result, provided via SelectInstance.trySelect or SelectInstance.selectInRegistrationPhase, should be processed in case of this select cancellation while dispatching. Unfortunately, we cannot pass this function only in SelectInstance.trySelect, as SelectInstance.selectInRegistrationPhase can be called when the coroutine is already cancelled.

Link copied to clipboard
typealias ProcessResultFunction = (clauseObject: Any, param: Any?, clauseResult: Any?) -> Any?

This function specifies how the internal result, provided via SelectInstance.selectInRegistrationPhase or SelectInstance.trySelect should be processed. For example, both ReceiveChannel.onReceive and ReceiveChannel.onReceiveCatching clauses perform exactly the same synchronization logic, but differ when the channel has been discovered in the closed or cancelled state.

Link copied to clipboard
typealias RegistrationFunction = (clauseObject: Any, select: SelectInstance<*>, param: Any?) -> Unit

The registration function specifies how the select instance should be registered into the specified clause object. In case of channels, the registration logic coincides with the plain send/receive operation with the only difference that the select instance is stored as a waiter instead of continuation.

Link copied to clipboard
interface SelectBuilder<in R>

Scope for select invocation.

Link copied to clipboard
interface SelectClause

Each select clause is specified with:

Link copied to clipboard

Clause for select expression without additional parameters that does not select any value.

Link copied to clipboard
interface SelectClause1<out Q> : SelectClause

Clause for select expression without additional parameters that selects value of type Q.

Link copied to clipboard
interface SelectClause2<in P, out Q> : SelectClause

Clause for select expression with additional parameter of type P that selects value of type Q.

Functions

Link copied to clipboard
fun <R> SelectBuilder<R>.onTimeout(timeMillis: Long, block: suspend () -> R)

Clause that selects the given block after a specified timeout passes. If timeout is negative or zero, block is selected immediately.

fun <R> SelectBuilder<R>.onTimeout(timeout: Duration, block: suspend () -> R)

Clause that selects the given block after the specified timeout passes. If timeout is negative or zero, block is selected immediately.

Link copied to clipboard
inline suspend fun <R> select(crossinline builder: SelectBuilder<R>.() -> Unit): R

Waits for the result of multiple suspending functions simultaneously, which are specified using clauses in the builder scope of this select invocation. The caller is suspended until one of the clauses is either selected or fails.

Link copied to clipboard
inline suspend fun <R> selectUnbiased(crossinline builder: SelectBuilder<R>.() -> Unit): R

Waits for the result of multiple suspending functions simultaneously like select, but in an unbiased way when multiple clauses are selectable at the same time.

Link copied to clipboard
inline suspend fun whileSelect(crossinline builder: SelectBuilder<Boolean>.() -> Unit)

Loops while select expression returns true.