Support new coroutine convention in JVM backend
This commit is contained in:
committed by
Stanislav Erokhin
parent
5a6b4a3224
commit
6649f64e9f
+19
-25
@@ -43,32 +43,26 @@ fun box(): String {
|
||||
|
||||
// LIBRARY CODE
|
||||
|
||||
fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> {
|
||||
val controller = FutureController<T>()
|
||||
c(controller).resume(Unit)
|
||||
return controller.future
|
||||
}
|
||||
|
||||
class FutureController<T> {
|
||||
fun <T> async(c: @Suspend() (() -> T)): CompletableFuture<T> {
|
||||
val future = CompletableFuture<T>()
|
||||
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
|
||||
f.whenComplete { value, throwable ->
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
c.startCoroutine(object : Continuation<T> {
|
||||
override fun resume(data: T) {
|
||||
future.complete(data)
|
||||
}
|
||||
|
||||
Suspend
|
||||
}
|
||||
|
||||
operator fun handleResult(value: T, c: Continuation<Nothing>) {
|
||||
future.complete(value)
|
||||
}
|
||||
|
||||
fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
future.completeExceptionally(t)
|
||||
}
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
future.completeExceptionally(exception)
|
||||
}
|
||||
})
|
||||
return future
|
||||
}
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
|
||||
f.whenComplete { value, throwable ->
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
}
|
||||
SUSPENDED
|
||||
}
|
||||
|
||||
+19
-24
@@ -40,31 +40,26 @@ fun box(): String {
|
||||
return "No exception"
|
||||
}
|
||||
|
||||
fun <T> async(coroutine c: FutureController<T>.() -> Continuation<Unit>): CompletableFuture<T> {
|
||||
val controller = FutureController<T>()
|
||||
c(controller).resume(Unit)
|
||||
return controller.future
|
||||
}
|
||||
|
||||
class FutureController<T> {
|
||||
fun <T> async(c: @Suspend() (() -> T)): CompletableFuture<T> {
|
||||
val future = CompletableFuture<T>()
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
|
||||
f.whenComplete { value, throwable ->
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
c.startContinuation(object : Continuation<T> {
|
||||
override fun resume(data: T) {
|
||||
future.complete(data)
|
||||
}
|
||||
|
||||
Suspend
|
||||
}
|
||||
|
||||
operator fun handleResult(value: T, c: Continuation<Nothing>) {
|
||||
future.complete(value)
|
||||
}
|
||||
|
||||
operator fun handleException(t: Throwable, c: Continuation<Nothing>) {
|
||||
future.completeExceptionally(t)
|
||||
}
|
||||
override fun resumeWithException(exception: Throwable) {
|
||||
future.completeExceptionally(exception)
|
||||
}
|
||||
})
|
||||
return future
|
||||
}
|
||||
|
||||
suspend fun <V> await(f: CompletableFuture<V>) = suspendWithCurrentContinuation<V> { machine ->
|
||||
f.whenComplete { value, throwable ->
|
||||
if (throwable == null)
|
||||
machine.resume(value)
|
||||
else
|
||||
machine.resumeWithException(throwable)
|
||||
}
|
||||
SUSPENDED
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user