-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add await() extension on Rector Mono type #1587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I would like to work on this issue. About the implementation of |
They both need to be implemented together, but they need careful design on their behaviour in various edge-cases. It is not clear at this moment what should happen when |
Since this issue hasn't progressed for a year, I've written PoC-level code. Need more cases then this? suspend inline fun <reified T> Mono<T>.await(): T {
if (null is T) {
// for Mono<T?>
return awaitFirstOrNull() as T
}
return when (T::class) {
// for Mono<Unit> or Mono<Void>
Unit::class, Void::class -> {
@Suppress("UNCHECKED_CAST", "ReactiveStreamsSubscriberImplementation")
suspendCancellableCoroutine { cont ->
cont as CancellableContinuation<T?>
subscribe(object : Subscriber<T?> {
override fun onSubscribe(s: Subscription) {
cont.invokeOnCancellation {
s.cancel()
}
s.request(1)
}
override fun onNext(t: T?) {
error("Mono<Void> or Mono<Unit> cannot emit value")
}
override fun onError(t: Throwable) {
cont.resumeWithException(t)
}
override fun onComplete() {
cont.resume(null)
}
})
}
}
// for Mono<T>
else -> awaitSingle() as T
}
}
object MonoAwaitTest : Spek({
test("mono await") {
runBlocking {
val m1: Mono<Int> = Mono.just(100)
val r1: Int = m1.await()
r1.should.be.equal(100)
val m2: Mono<String> = Mono.just("value")
val r2: String = m2.await()
r2.should.be.equal("value")
val m3: Mono<String?> = Mono.justOrEmpty("String?")
val r3: String? = m3.await()
r3.should.be.equal("String?")
val m4: Mono<String?> = Mono.justOrEmpty(null)
val r4: String? = m4.await()
r4.should.be.equal(null)
val m5: Mono<Unit> = Mono.empty()
val r5: Unit = m5.await()
r5.should.be.equal(null)
val m6: Mono<Void> = Mono.empty()
val r6: Void = m6.await()
r6.should.be.equal(null)
}
}
})
|
It seems from the documentation (https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html) that |
…nd Maybe (Kotlin#2628) * Deprecated `awaitSingleOr*` on arbitrary Publishers * Added specialized `awaitSingle` and `awaitSingleOrNull` methods on `Maybe<T>` and `Mono<T>` * Deprecated `Maybe<T>.await()` in favor of `Maybe<T>.awaitSingleOrNull()` * Added specializations of most of the `await*` methods for `Mono<T>` and deprecated them, as the only useful methods on `Mono<T>` are `awaitSingle` and `awaitSingleOrNull` * Reworded some documentation for `await*` methods Fixes Kotlin#2591 Fixes Kotlin#1587
It should simply be an inline alias to
Publisher.awaitSingle()
. But special care shall be taken aboutMono<Void>
types that never emit any value. They needawaitCompletion()
which is missing altogether now.The text was updated successfully, but these errors were encountered: