Richard Gomez
11/17/2021, 1:19 AM@GET
suspend fun get(): Uni<String> = uni { getFoo() } // run coroutine and return the result as a "Uni", which is similar to Reactor's Mono.
The existing 3rd-party helpers in kotlinx.coroutines' rely on AbstractCoroutine
, which warns that it shouldn't be used outside of the core lib. (e.g. mono builder)Richard Gomez
11/17/2021, 1:22 AMAbstractCoroutine
2. Use AbstractCoroutine
and pray it doesn't break
3. Make an issue on the kotlinx.coroutines repo and hope they make an official set of Mutiny helpers?Justin Tullgren
11/17/2021, 2:21 AMJustin Tullgren
11/17/2021, 2:21 AMCompletableFuture
you can use the java8 integration libraryJustin Tullgren
11/17/2021, 2:22 AMJustin Tullgren
11/17/2021, 2:23 AMJustin Tullgren
11/17/2021, 2:26 AMsuspend fun get(): Uni<String> = suspendCancellableCoroutine { continuation ->
request
.ifNoItem().after(ofSecond(1))
.fail(() -> new Exception("💥"))
.onFailure().recoverWithItem(fail -> continuation.resumeWithException(fail))
.subscribe()
.with(item -> continuation.resumeWith(item))
}
Justin Tullgren
11/17/2021, 2:26 AMyschimke
11/17/2021, 8:42 AMyschimke
11/17/2021, 8:43 AMyschimke
11/17/2021, 8:44 AMRichard Gomez
11/17/2021, 1:32 PMyschimke
11/17/2021, 1:35 PMRichard Gomez
11/17/2021, 1:36 PMRichard Gomez
11/17/2021, 1:39 PMlaunch
never actually runs.
fun <T> CoroutineScope.uni(block: suspend CoroutineScope.() -> T): Uni<T> {
Uni.createFrom().emitter<T> { em ->
// Need to call launch because "Suspension functions can be called only within coroutine body".
launch {
try {
em.completed(block())
} catch(ex: Exception) {
// I know that swallowing CancellationException is bad
em.fail(t)
}
}
}
}
The existing bindings seem to have the same challenge, and solve it with AbstractCoroutine
.