Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.experimental |
| 6 | |
Roman Elizarov | 23fb728 | 2018-01-24 23:09:42 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.experimental.intrinsics.* |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 8 | import kotlinx.coroutines.experimental.selects.* |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 9 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 10 | |
| 11 | /** |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 12 | * Deferred value is a non-blocking cancellable future. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 13 | * |
| 14 | * It is created with [async] coroutine builder or via constructor of [CompletableDeferred] class. |
Roman Elizarov | 41c5c8b | 2017-01-25 13:37:15 +0300 | [diff] [blame] | 15 | * It is in [active][isActive] state while the value is being computed. |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 16 | * |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 17 | * Deferred value has the following states: |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 18 | * |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 19 | * | **State** | [isActive] | [isCompleted] | [isCompletedExceptionally] | [isCancelled] | |
| 20 | * | --------------------------------------- | ---------- | ------------- | -------------------------- | ------------- | |
| 21 | * | _New_ (optional initial state) | `false` | `false` | `false` | `false` | |
| 22 | * | _Active_ (default initial state) | `true` | `false` | `false` | `false` | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 23 | * | _Completing_ (optional transient state) | `true` | `false` | `false` | `false` | |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 24 | * | _Cancelling_ (optional transient state) | `false` | `false` | `false` | `true` | |
| 25 | * | _Cancelled_ (final state) | `false` | `true` | `true` | `true` | |
| 26 | * | _Resolved_ (final state) | `false` | `true` | `false` | `false` | |
| 27 | * | _Failed_ (final state) | `false` | `true` | `true` | `false` | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 28 | * |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 29 | * Usually, a deferred value is created in _active_ state (it is created and started). |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 30 | * However, [async] coroutine builder has an optional `start` parameter that creates a deferred value in _new_ state |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 31 | * when this parameter is set to [CoroutineStart.LAZY]. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 32 | * Such a deferred can be be made _active_ by invoking [start], [join], or [await]. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 33 | * |
| 34 | * A deferred can be _cancelled_ at any time with [cancel] function that forces it to transition to |
Roman Elizarov | 4d626de | 2018-01-11 22:57:28 +0300 | [diff] [blame] | 35 | * _cancelling_ state immediately. Deferred that is not backed by a coroutine (see [CompletableDeferred]) and does not have |
| 36 | * [children] becomes _cancelled_ on [cancel] immediately. |
| 37 | * Otherwise, deferred becomes _cancelled_ when it finishes executing its code and |
| 38 | * when all its children [complete][isCompleted]. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 39 | * |
| 40 | * ``` |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 41 | * wait children |
| 42 | * +-----+ start +--------+ complete +-------------+ finish +-----------+ |
| 43 | * | New | ---------------> | Active | ----------> | Completing | ---+-> | Resolved | |
| 44 | * +-----+ +--------+ +-------------+ | |(completed)| |
| 45 | * | | | | +-----------+ |
| 46 | * | cancel | cancel | cancel | |
| 47 | * V V | | +-----------+ |
| 48 | * +-----------+ finish +------------+ | +-> | Failed | |
| 49 | * | Cancelled | <--------- | Cancelling | <---------------+ |(completed)| |
| 50 | * |(completed)| +------------+ +-----------+ |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 51 | * +-----------+ |
| 52 | * ``` |
| 53 | * |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 54 | * A deferred value is a [Job]. A job in the |
| 55 | * [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html) |
| 56 | * of [async] builder represents the coroutine itself. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 57 | * A deferred value is active while the coroutine is working and cancellation aborts the coroutine when |
| 58 | * the coroutine is suspended on a _cancellable_ suspension point by throwing [CancellationException] |
| 59 | * or the cancellation cause inside the coroutine. |
| 60 | * |
| 61 | * A deferred value can have a _parent_ job. A deferred value with a parent is cancelled when its parent is |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 62 | * cancelled or completes. Parent waits for all its [children] to complete in _completing_ or |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 63 | * _cancelling_ state. _Completing_ state is purely internal. For an outside observer a _completing_ |
| 64 | * deferred is still active, while internally it is waiting for its children. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 65 | * |
| 66 | * All functions on this interface and on all interfaces derived from it are **thread-safe** and can |
| 67 | * be safely invoked from concurrent coroutines without external synchronization. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 68 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 69 | public interface Deferred<out T> : Job { |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 70 | /** |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 71 | * Returns `true` if computation of this deferred value has _completed exceptionally_ -- it had |
| 72 | * either _failed_ with exception during computation or was [cancelled][cancel]. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 73 | * |
| 74 | * It implies that [isActive] is `false` and [isCompleted] is `true`. |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 75 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 76 | public val isCompletedExceptionally: Boolean |
Roman Elizarov | b7c46de | 2017-02-08 12:35:24 +0300 | [diff] [blame] | 77 | |
| 78 | /** |
Roman Elizarov | be4cae3 | 2017-02-15 17:57:02 +0300 | [diff] [blame] | 79 | * Awaits for completion of this value without blocking a thread and resumes when deferred computation is complete, |
| 80 | * returning the resulting value or throwing the corresponding exception if the deferred had completed exceptionally. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 81 | * |
Roman Elizarov | be4cae3 | 2017-02-15 17:57:02 +0300 | [diff] [blame] | 82 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 83 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
Roman Elizarov | c581454 | 2017-01-19 10:19:06 +0300 | [diff] [blame] | 84 | * immediately resumes with [CancellationException]. |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 85 | * |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 86 | * This function can be used in [select] invocation with [onAwait] clause. |
Roman Elizarov | d84dbc2 | 2017-02-22 14:56:58 +0300 | [diff] [blame] | 87 | * Use [isCompleted] to check for completion of this deferred value without waiting. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 88 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 89 | public suspend fun await(): T |
Roman Elizarov | c581454 | 2017-01-19 10:19:06 +0300 | [diff] [blame] | 90 | |
| 91 | /** |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 92 | * Clause for [select] expression of [await] suspending function that selects with the deferred value when it is |
| 93 | * resolved. The [select] invocation fails if the deferred value completes exceptionally (either fails or |
| 94 | * it cancelled). |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 95 | */ |
Roman Elizarov | db0e22d | 2017-08-29 18:15:57 +0300 | [diff] [blame] | 96 | public val onAwait: SelectClause1<T> |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 97 | |
| 98 | /** |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 99 | * Returns *completed* result or throws [IllegalStateException] if this deferred value has not |
| 100 | * [completed][isCompleted] yet. It throws the corresponding exception if this deferred has |
| 101 | * [completed exceptionally][isCompletedExceptionally]. |
| 102 | * |
Roman Elizarov | e780347 | 2017-02-16 09:52:31 +0300 | [diff] [blame] | 103 | * This function is designed to be used from [invokeOnCompletion] handlers, when there is an absolute certainty that |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 104 | * the value is already complete. See also [getCompletionExceptionOrNull]. |
Roman Elizarov | c581454 | 2017-01-19 10:19:06 +0300 | [diff] [blame] | 105 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 106 | public fun getCompleted(): T |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 107 | |
| 108 | /** |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 109 | * Returns *completion exception* result if this deferred [completed exceptionally][isCompletedExceptionally], |
| 110 | * `null` if it is completed normally, or throws [IllegalStateException] if this deferred value has not |
| 111 | * [completed][isCompleted] yet. |
| 112 | * |
| 113 | * This function is designed to be used from [invokeOnCompletion] handlers, when there is an absolute certainty that |
| 114 | * the value is already complete. See also [getCompleted]. |
| 115 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 116 | public fun getCompletionExceptionOrNull(): Throwable? |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 117 | |
| 118 | /** |
Roman Elizarov | fc7a9a2 | 2017-02-13 11:54:01 +0300 | [diff] [blame] | 119 | * @suppress **Deprecated**: Use `isActive`. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 120 | */ |
| 121 | @Deprecated(message = "Use `isActive`", replaceWith = ReplaceWith("isActive")) |
| 122 | public val isComputing: Boolean get() = isActive |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 126 | * Creates new coroutine and returns its future result as an implementation of [Deferred]. |
Roman Elizarov | 44ba4b1 | 2017-01-25 11:37:54 +0300 | [diff] [blame] | 127 | * |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 128 | * The running coroutine is cancelled when the resulting object is [cancelled][Job.cancel]. |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 129 | * |
| 130 | * The [context] for the new coroutine can be explicitly specified. |
| 131 | * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`. |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 132 | * The [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html) |
| 133 | * of the parent coroutine may be used, |
Roman Elizarov | 44ba4b1 | 2017-01-25 11:37:54 +0300 | [diff] [blame] | 134 | * in which case the [Job] of the resulting coroutine is a child of the job of the parent coroutine. |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 135 | * The parent job may be also explicitly specified using [parent] parameter. |
| 136 | * |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 137 | * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 138 | * |
Roman Elizarov | 7b10c94 | 2017-05-16 21:02:51 +0300 | [diff] [blame] | 139 | * By default, the coroutine is immediately scheduled for execution. |
| 140 | * Other options can be specified via `start` parameter. See [CoroutineStart] for details. |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 141 | * An optional [start] parameter can be set to [CoroutineStart.LAZY] to start coroutine _lazily_. In this case,, |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 142 | * the resulting [Deferred] is created in _new_ state. It can be explicitly started with [start][Job.start] |
Vsevolod Tolstopyatov | f0ef14e | 2018-04-11 19:44:08 +0300 | [diff] [blame] | 143 | * function and will be started implicitly on the first invocation of [join][Job.join], [await][Deferred.await] or [awaitAll]. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 144 | * |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 145 | * @param context context of the coroutine. The default value is [DefaultDispatcher]. |
| 146 | * @param start coroutine start option. The default value is [CoroutineStart.DEFAULT]. |
Roman Elizarov | fe8ba6b | 2018-03-13 17:34:29 +0300 | [diff] [blame] | 147 | * @param parent explicitly specifies the parent job, overrides job from the [context] (if any). |
| 148 | * @param onCompletion optional completion handler for the coroutine (see [Job.invokeOnCompletion]). |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 149 | * @param block the coroutine code. |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 150 | */ |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 151 | public fun <T> async( |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 152 | context: CoroutineContext = DefaultDispatcher, |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 153 | start: CoroutineStart = CoroutineStart.DEFAULT, |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 154 | parent: Job? = null, |
Roman Elizarov | fe8ba6b | 2018-03-13 17:34:29 +0300 | [diff] [blame] | 155 | onCompletion: CompletionHandler? = null, |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 156 | block: suspend CoroutineScope.() -> T |
| 157 | ): Deferred<T> { |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 158 | val newContext = newCoroutineContext(context, parent) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 159 | val coroutine = if (start.isLazy) |
| 160 | LazyDeferredCoroutine(newContext, block) else |
| 161 | DeferredCoroutine<T>(newContext, active = true) |
Roman Elizarov | fe8ba6b | 2018-03-13 17:34:29 +0300 | [diff] [blame] | 162 | if (onCompletion != null) coroutine.invokeOnCompletion(handler = onCompletion) |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 163 | coroutine.start(start, coroutine, block) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 164 | return coroutine |
| 165 | } |
| 166 | |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 167 | /** @suppress **Deprecated**: Binary compatibility */ |
| 168 | @Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN) |
| 169 | public fun <T> async( |
| 170 | context: CoroutineContext = DefaultDispatcher, |
| 171 | start: CoroutineStart = CoroutineStart.DEFAULT, |
Roman Elizarov | fe8ba6b | 2018-03-13 17:34:29 +0300 | [diff] [blame] | 172 | parent: Job? = null, |
| 173 | block: suspend CoroutineScope.() -> T |
| 174 | ): Deferred<T> = async(context, start, parent, block = block) |
| 175 | |
| 176 | /** @suppress **Deprecated**: Binary compatibility */ |
| 177 | @Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN) |
| 178 | public fun <T> async( |
| 179 | context: CoroutineContext = DefaultDispatcher, |
| 180 | start: CoroutineStart = CoroutineStart.DEFAULT, |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 181 | block: suspend CoroutineScope.() -> T |
| 182 | ): Deferred<T> = |
| 183 | async(context, start, block = block) |
| 184 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 185 | /** |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 186 | * @suppress **Deprecated**: Use `start = CoroutineStart.XXX` parameter |
| 187 | */ |
| 188 | @Deprecated(message = "Use `start = CoroutineStart.XXX` parameter", |
| 189 | replaceWith = ReplaceWith("async(context, if (start) CoroutineStart.DEFAULT else CoroutineStart.LAZY, block)")) |
| 190 | public fun <T> async(context: CoroutineContext, start: Boolean, block: suspend CoroutineScope.() -> T): Deferred<T> = |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 191 | async(context, if (start) CoroutineStart.DEFAULT else CoroutineStart.LAZY, block = block) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 192 | |
| 193 | /** |
Roman Elizarov | fc7a9a2 | 2017-02-13 11:54:01 +0300 | [diff] [blame] | 194 | * @suppress **Deprecated**: `defer` was renamed to `async`. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 195 | */ |
| 196 | @Deprecated(message = "`defer` was renamed to `async`", level = DeprecationLevel.WARNING, |
Roman Elizarov | aa461cf | 2018-04-11 13:20:29 +0300 | [diff] [blame] | 197 | replaceWith = ReplaceWith("async(context, block = block)")) |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 198 | public fun <T> defer(context: CoroutineContext, block: suspend CoroutineScope.() -> T): Deferred<T> = |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 199 | async(context, block = block) |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 200 | |
Roman Elizarov | 7a3afb5 | 2017-06-19 19:27:08 +0300 | [diff] [blame] | 201 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 202 | private open class DeferredCoroutine<T>( |
Roman Elizarov | 2b12d58 | 2017-06-22 20:12:19 +0300 | [diff] [blame] | 203 | parentContext: CoroutineContext, |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 204 | active: Boolean |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 205 | ) : AbstractCoroutine<T>(parentContext, active), Deferred<T>, SelectClause1<T> { |
Roman Elizarov | 7a3afb5 | 2017-06-19 19:27:08 +0300 | [diff] [blame] | 206 | override fun getCompleted(): T = getCompletedInternal() as T |
Roman Elizarov | e3f2884 | 2017-12-21 19:15:40 +0300 | [diff] [blame] | 207 | override suspend fun await(): T = awaitInternal() as T |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 208 | override val onAwait: SelectClause1<T> get() = this |
| 209 | override fun <R> registerSelectClause1(select: SelectInstance<R>, block: suspend (T) -> R) = |
| 210 | registerSelectClause1Internal(select, block) |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 211 | } |
| 212 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 213 | private class LazyDeferredCoroutine<T>( |
Roman Elizarov | 1216e91 | 2017-02-22 09:57:06 +0300 | [diff] [blame] | 214 | parentContext: CoroutineContext, |
| 215 | private val block: suspend CoroutineScope.() -> T |
| 216 | ) : DeferredCoroutine<T>(parentContext, active = false) { |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 217 | override fun onStart() { |
Roman Elizarov | a74eb5f | 2017-05-11 20:15:18 +0300 | [diff] [blame] | 218 | block.startCoroutineCancellable(this, this) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 219 | } |
Roman Elizarov | 3754f95 | 2017-01-18 20:47:54 +0300 | [diff] [blame] | 220 | } |