Roman Elizarov | ee88fbe | 2017-02-22 09:53:49 +0300 | [diff] [blame] | 1 | package kotlinx.coroutines.experimental.intrinsics |
| 2 | |
| 3 | import kotlin.coroutines.experimental.Continuation |
| 4 | import kotlin.coroutines.experimental.intrinsics.COROUTINE_SUSPENDED |
| 5 | import kotlin.coroutines.experimental.suspendCoroutine |
| 6 | |
| 7 | /** |
| 8 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. |
| 9 | * |
| 10 | * @suppress **This is unstable API and it is subject to change.** |
| 11 | */ |
| 12 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
| 13 | internal fun <R> (suspend () -> R).startUndispatchedCoroutine(completion: Continuation<R>) { |
| 14 | val value = try { |
| 15 | (this as kotlin.jvm.functions.Function1<Continuation<R>, Any?>).invoke(completion) |
| 16 | } catch (e: Throwable) { |
| 17 | completion.resumeWithException(e) |
| 18 | return |
| 19 | } |
| 20 | if (value !== COROUTINE_SUSPENDED) |
| 21 | completion.resume(value as R) |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Use this function to restart coroutine directly from inside of [suspendCoroutine]. |
| 26 | * |
| 27 | * @suppress **This is unstable API and it is subject to change.** |
| 28 | */ |
| 29 | @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN", "UNCHECKED_CAST") |
| 30 | internal fun <E, R> (suspend (E) -> R).startUndispatchedCoroutine(element: E, completion: Continuation<R>) { |
| 31 | val value = try { |
| 32 | (this as kotlin.jvm.functions.Function2<E, Continuation<R>, Any?>).invoke(element, completion) |
| 33 | } catch (e: Throwable) { |
| 34 | completion.resumeWithException(e) |
| 35 | return |
| 36 | } |
| 37 | if (value !== COROUTINE_SUSPENDED) |
| 38 | completion.resume(value as R) |
| 39 | } |