|
4 | 4 |
|
5 | 5 | package kotlinx.coroutines.jdk9 |
6 | 6 |
|
| 7 | +import kotlinx.coroutines.* |
7 | 8 | import java.util.concurrent.* |
8 | 9 | import org.reactivestreams.FlowAdapters |
9 | 10 | import kotlinx.coroutines.reactive.* |
10 | 11 |
|
11 | 12 | /** |
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. |
14 | 15 | * |
15 | 16 | * 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]. |
18 | 19 | * |
19 | | - * @throws NoSuchElementException if publisher does not emit any value |
| 20 | + * @throws NoSuchElementException if the publisher does not emit any value |
20 | 21 | */ |
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() |
22 | 24 |
|
23 | 25 | /** |
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. |
26 | 29 | * |
27 | 30 | * 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]. |
30 | 33 | */ |
31 | 34 | public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T = |
32 | | - FlowAdapters.toPublisher(this).awaitFirstOrDefault(default) |
| 35 | + FlowAdapters.toPublisher(this).awaitFirstOrDefault(default) |
33 | 36 |
|
34 | 37 | /** |
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. |
37 | 40 | * |
38 | 41 | * 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]. |
41 | 44 | */ |
42 | 45 | public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? = |
43 | | - FlowAdapters.toPublisher(this).awaitFirstOrNull() |
| 46 | + FlowAdapters.toPublisher(this).awaitFirstOrNull() |
44 | 47 |
|
45 | 48 | /** |
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. |
48 | 52 | * |
49 | 53 | * 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]. |
52 | 56 | */ |
53 | 57 | public suspend fun <T> Flow.Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T = |
54 | | - FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue) |
| 58 | + FlowAdapters.toPublisher(this).awaitFirstOrElse(defaultValue) |
55 | 59 |
|
56 | 60 | /** |
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. |
59 | 63 | * |
60 | 64 | * 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]. |
63 | 67 | * |
64 | | - * @throws NoSuchElementException if publisher does not emit any value |
| 68 | + * @throws NoSuchElementException if the publisher does not emit any value |
65 | 69 | */ |
66 | 70 | public suspend fun <T> Flow.Publisher<T>.awaitLast(): T = |
67 | | - FlowAdapters.toPublisher(this).awaitLast() |
| 71 | + FlowAdapters.toPublisher(this).awaitLast() |
68 | 72 |
|
69 | 73 | /** |
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. |
72 | 76 | * |
73 | 77 | * 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]. |
76 | 80 | * |
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 |
79 | 83 | */ |
80 | 84 | public suspend fun <T> Flow.Publisher<T>.awaitSingle(): T = |
81 | | - FlowAdapters.toPublisher(this).awaitSingle() |
| 85 | + FlowAdapters.toPublisher(this).awaitSingle() |
0 commit comments