AbstractCoroutine

abstract class AbstractCoroutine<in T> : 
    JobSupport,
    Job,
    Continuation<T>,
    CoroutineScope
(source)

Abstract base class for implementation of coroutines in coroutine builders.

This class implements completion Continuation, Job, and CoroutineScope interfaces. It stores the result of continuation in the state of the job. This coroutine waits for children coroutines to finish before completing and is cancelled through an intermediate cancelling state.

The following methods are available for override:

Parameters

parentContext - context of the parent coroutine.

active - when true (by default) coroutine is created in active state, when false in new state. See Job for details.

Constructors

<init>

AbstractCoroutine(
    parentContext: CoroutineContext,
    active: Boolean = true)

Abstract base class for implementation of coroutines in coroutine builders.

Properties

coroutineContext

val coroutineContext: CoroutineContext

Returns the context of this coroutine.

Functions

cancel

open fun cancel(cause: Throwable?): Boolean

Cancels this job with an optional cancellation cause. The result is true if this job was cancelled as a result of this invocation and false otherwise (if it was already completed or if it is NonCancellable). Repeated invocations of this function have no effect and always produce false.

invokeOnCompletion

open fun invokeOnCompletion(
    onCancelling: Boolean,
    invokeImmediately: Boolean,
    handler: CompletionHandler
): DisposableHandle

Registers handler that is synchronously invoked once on cancellation or completion of this job. When job is already cancelling or complete, then the handler is immediately invoked with a job’s cancellation cause or null unless invokeImmediately is set to false. Otherwise, handler will be invoked once when this job is cancelled or complete.

onCancellation

open fun onCancellation(cause: Throwable?): Unit

This function is invoked once when this coroutine is cancelled or is completed, similarly to invokeOnCompletion with onCancelling set to true.

onCompleted

open fun onCompleted(value: T): Unit

This function is invoked once when job is completed normally with the specified value.

onCompletedExceptionally

open fun onCompletedExceptionally(exception: Throwable): Unit

This function is invoked once when job is completed exceptionally with the specified exception.

onStart

open fun onStart(): Unit

This function is invoked once when non-active coroutine (constructed with active set to `false) is started.

resume

fun resume(value: T): Unit

Completes execution of this coroutine normally with the specified value.

resumeWithException

fun resumeWithException(exception: Throwable): Unit

Completes execution of this with coroutine exceptionally with the specified exception.

start

fun start(
    start: CoroutineStart,
    block: suspend () -> T
): Unit
fun <R> start(
    start: CoroutineStart,
    receiver: R,
    block: suspend R.() -> T
): Unit

Starts this coroutine with the given code block and start strategy. This function shall be invoked at most once on this coroutine.

Extension Functions

cancelFutureOnCompletion

fun Job.cancelFutureOnCompletion(
    future: Future<*>
): DisposableHandle

Cancels a specified future when this job is complete.

startUndispatchedOrReturn

fun <T> AbstractCoroutine<T>.startUndispatchedOrReturn(
    block: suspend () -> T
): Any?
fun <T, R> AbstractCoroutine<T>.startUndispatchedOrReturn(
    receiver: R,
    block: suspend R.() -> T
): Any?

Starts this coroutine with the given code block in the same context and returns result when it completes without suspnesion. This function shall be invoked at most once on this coroutine.