Skip to content

Commit 71df60e

Browse files
authored
Deprecate awaitSingleOr*, specialize some await* functions for Mono and Maybe (#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 #2591 Fixes #1587
1 parent cefb84f commit 71df60e

File tree

22 files changed

+871
-225
lines changed

22 files changed

+871
-225
lines changed

reactive/kotlinx-coroutines-jdk9/src/Await.kt

+38-34
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,82 @@
44

55
package kotlinx.coroutines.jdk9
66

7+
import kotlinx.coroutines.*
78
import java.util.concurrent.*
89
import org.reactivestreams.FlowAdapters
910
import kotlinx.coroutines.reactive.*
1011

1112
/**
12-
* Awaits for the first value from the given publisher without blocking a thread and
13-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
13+
* Awaits the first value from the given publisher without blocking the thread and returns the resulting value, or, if
14+
* the publisher has produced an error, throws the corresponding exception.
1415
*
1516
* This suspending function is cancellable.
16-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
17-
* immediately resumes with [CancellationException].
17+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
18+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
1819
*
19-
* @throws NoSuchElementException if publisher does not emit any value
20+
* @throws NoSuchElementException if the publisher does not emit any value
2021
*/
21-
public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T = FlowAdapters.toPublisher(this).awaitFirst()
22+
public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
23+
FlowAdapters.toPublisher(this).awaitFirst()
2224

2325
/**
24-
* Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a
25-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
26+
* Awaits the first value from the given publisher, or returns the [default] value if none is emitted, without blocking
27+
* the thread, and returns the resulting value, or, if this publisher has produced an error, throws the corresponding
28+
* exception.
2629
*
2730
* This suspending function is cancellable.
28-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
29-
* immediately resumes with [CancellationException].
31+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
32+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
3033
*/
3134
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
32-
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
35+
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
3336

3437
/**
35-
* Awaits for the first value from the given observable or `null` value if none is emitted without blocking a
36-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
38+
* Awaits the first value from the given publisher, or returns `null` if none is emitted, without blocking the thread,
39+
* and returns the resulting value, or, if this publisher has produced an error, throws the corresponding exception.
3740
*
3841
* This suspending function is cancellable.
39-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
40-
* immediately resumes with [CancellationException].
42+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
43+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
4144
*/
4245
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
43-
FlowAdapters.toPublisher(this).awaitFirstOrNull()
46+
FlowAdapters.toPublisher(this).awaitFirstOrNull()
4447

4548
/**
46-
* Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a
47-
* thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
49+
* Awaits the first value from the given publisher, or calls [defaultValue] to get a value if none is emitted, without
50+
* blocking the thread, and returns the resulting value, or, if this publisher has produced an error, throws the
51+
* corresponding exception.
4852
*
4953
* This suspending function is cancellable.
50-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
51-
* immediately resumes with [CancellationException].
54+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
55+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
5256
*/
5357
public suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T =
54-
FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue)
58+
FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue)
5559

5660
/**
57-
* Awaits for the last value from the given publisher without blocking a thread and
58-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
61+
* Awaits the last value from the given publisher without blocking the thread and
62+
* returns the resulting value, or, if this publisher has produced an error, throws the corresponding exception.
5963
*
6064
* This suspending function is cancellable.
61-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
62-
* immediately resumes with [CancellationException].
65+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
66+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
6367
*
64-
* @throws NoSuchElementException if publisher does not emit any value
68+
* @throws NoSuchElementException if the publisher does not emit any value
6569
*/
6670
public suspend fun <T> Flow.Publisher<T>.awaitLast(): T =
67-
FlowAdapters.toPublisher(this).awaitLast()
71+
FlowAdapters.toPublisher(this).awaitLast()
6872

6973
/**
70-
* Awaits for the single value from the given publisher without blocking a thread and
71-
* returns the resulting value or throws the corresponding exception if this publisher had produced error.
74+
* Awaits the single value from the given publisher without blocking the thread and returns the resulting value, or,
75+
* if this publisher has produced an error, throws the corresponding exception.
7276
*
7377
* This suspending function is cancellable.
74-
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
75-
* immediately resumes with [CancellationException].
78+
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
79+
* function immediately cancels its [Flow.Subscription] and resumes with [CancellationException].
7680
*
77-
* @throws NoSuchElementException if publisher does not emit any value
78-
* @throws IllegalArgumentException if publisher emits more than one value
81+
* @throws NoSuchElementException if the publisher does not emit any value
82+
* @throws IllegalArgumentException if the publisher emits more than one value
7983
*/
8084
public suspend fun <T> Flow.Publisher<T>.awaitSingle(): T =
81-
FlowAdapters.toPublisher(this).awaitSingle()
85+
FlowAdapters.toPublisher(this).awaitSingle()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.jdk9
6+
7+
import kotlinx.coroutines.*
8+
import org.junit.*
9+
import java.util.concurrent.Flow as JFlow
10+
11+
class AwaitTest: TestBase() {
12+
13+
/** Tests that calls to [awaitFirst] (and, thus, to the rest of these functions) throw [CancellationException] and
14+
* unsubscribe from the publisher when their [Job] is cancelled. */
15+
@Test
16+
fun testAwaitCancellation() = runTest {
17+
expect(1)
18+
val publisher = JFlow.Publisher<Int> { s ->
19+
s.onSubscribe(object : JFlow.Subscription {
20+
override fun request(n: Long) {
21+
expect(3)
22+
}
23+
24+
override fun cancel() {
25+
expect(5)
26+
}
27+
})
28+
}
29+
val job = launch(start = CoroutineStart.UNDISPATCHED) {
30+
try {
31+
expect(2)
32+
publisher.awaitFirst()
33+
} catch (e: CancellationException) {
34+
expect(6)
35+
throw e
36+
}
37+
}
38+
expect(4)
39+
job.cancelAndJoin()
40+
finish(7)
41+
}
42+
43+
}

0 commit comments

Comments
 (0)