Skip to content

Commit ee88fbe

Browse files
committed
startUndispatchedCoroutine utility functions
1 parent 80105e3 commit ee88fbe

File tree

1 file changed

+39
-0
lines changed
  • kotlinx-coroutines-core/src/main/kotlin/kotlinx/coroutines/experimental/intrinsics

1 file changed

+39
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

0 commit comments

Comments
 (0)