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