-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from leviysoft/feature/coroutine-compat
Coroutines compatibility module
- Loading branch information
Showing
12 changed files
with
144 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
76 changes: 76 additions & 0 deletions
76
coroutines/src/main/kotlin/com/github/leviysoft/sk/coroutines/CoroutineScalaConversions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
@file:OptIn(InternalCoroutinesApi::class, ExperimentalCoroutinesApi::class) | ||
|
||
package com.github.leviysoft.sk.coroutines | ||
|
||
import kotlinx.coroutines.* | ||
|
||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.coroutines.EmptyCoroutineContext | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.Future | ||
import scala.concurrent.Promise | ||
import scala.util.Failure | ||
import scala.util.Success | ||
import kotlin.coroutines.resume | ||
import kotlin.coroutines.resumeWithException | ||
|
||
fun <T> CoroutineScope.scalaFuture( | ||
context: CoroutineContext = EmptyCoroutineContext, | ||
start: CoroutineStart = CoroutineStart.DEFAULT, | ||
block: suspend CoroutineScope.() -> T | ||
) : Future<T> { | ||
require(!start.isLazy) { "$start start is not supported" } | ||
val newContext = this.newCoroutineContext(context) | ||
val promise = Promise.apply<T>() | ||
val coroutine = PromiseCoroutine(newContext, promise) | ||
coroutine.start(start, coroutine, block) | ||
|
||
return promise.future() | ||
} | ||
|
||
fun <T> Deferred<T>.asScalaFuture(): Future<T> { | ||
val promise: Promise<T> = Promise.apply() | ||
|
||
invokeOnCompletion { | ||
try { | ||
promise.success(getCompleted()) | ||
} catch (t: Throwable) { | ||
promise.failure(t) | ||
} | ||
} | ||
|
||
return promise.future() | ||
} | ||
|
||
fun Job.asScalaFuture(): Future<scala.runtime.BoxedUnit> { | ||
val promise: Promise<scala.runtime.BoxedUnit> = Promise.apply() | ||
invokeOnCompletion { cause -> | ||
if (cause === null) promise.success(scala.runtime.BoxedUnit.UNIT) | ||
else promise.failure(cause) | ||
} | ||
return promise.future() | ||
} | ||
|
||
fun <T> Future<T>.asDeferred(executor: ExecutionContext): Deferred<T> { | ||
val result = CompletableDeferred<T>() | ||
this.onComplete({ res -> | ||
when(res) { | ||
is Success -> result.complete(res.value()) | ||
is Failure -> result.completeExceptionally(res.exception()) | ||
else -> throw IllegalStateException("Unreachable") | ||
} | ||
}, executor) | ||
return result | ||
} | ||
|
||
suspend fun <T> Future<T>.await(executor: ExecutionContext): T { | ||
return suspendCancellableCoroutine { cont: CancellableContinuation<T> -> | ||
this.onComplete({ res -> | ||
when(res) { | ||
is Success -> cont.resume(res.value()) | ||
is Failure -> cont.resumeWithException(res.exception()) | ||
else -> throw IllegalStateException("Unreachable") | ||
} | ||
}, executor) | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
coroutines/src/main/kotlin/com/github/leviysoft/sk/coroutines/PromiseCoroutine.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.github.leviysoft.sk.coroutines | ||
|
||
import kotlinx.coroutines.AbstractCoroutine | ||
import kotlinx.coroutines.InternalCoroutinesApi | ||
import scala.concurrent.Promise | ||
import kotlin.coroutines.CoroutineContext | ||
|
||
@OptIn(InternalCoroutinesApi::class) | ||
internal class PromiseCoroutine<T>(context: CoroutineContext, private val promise: Promise<T>): AbstractCoroutine<T>(context, initParentJob = true, active = true) { | ||
override fun onCompleted(value: T) { | ||
this.promise.success(value) | ||
} | ||
|
||
override fun onCancelled(cause: Throwable, handled: Boolean) { | ||
this.promise.failure(cause) | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
coroutines/src/test/kotlin/com/github/leviysoft/sk/coroutines/FutureConversionTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.github.leviysoft.sk.coroutines | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.`Future$` | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class FutureConversionTests { | ||
@Test | ||
fun awaitFutureTest() { | ||
val future = `Future$`.`MODULE$`.successful(42) | ||
|
||
val result = runBlocking { | ||
future.await(ExecutionContext.global()) | ||
} | ||
|
||
assertEquals(42, result) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters