@@ -15,11 +15,12 @@ import kotlin.coroutines.*
15
15
// ------------------------ CompletableSource ------------------------
16
16
17
17
/* *
18
- * Awaits for completion of this completable without blocking a thread.
19
- * Returns `Unit` or throws the corresponding exception if this completable had produced error.
18
+ * Awaits for completion of this completable without blocking the thread.
19
+ * Returns `Unit`, or throws the corresponding exception if this completable produces an error.
20
20
*
21
21
* This suspending function is cancellable. If the [Job] of the invoking coroutine is cancelled or completed while this
22
- * suspending function is suspended, this function immediately resumes with [CancellationException].
22
+ * suspending function is suspended, this function immediately resumes with [CancellationException] and disposes of its
23
+ * subscription.
23
24
*/
24
25
public suspend fun CompletableSource.await (): Unit = suspendCancellableCoroutine { cont ->
25
26
subscribe(object : CompletableObserver {
@@ -31,6 +32,37 @@ public suspend fun CompletableSource.await(): Unit = suspendCancellableCoroutine
31
32
32
33
// ------------------------ MaybeSource ------------------------
33
34
35
+ /* *
36
+ * Awaits for completion of the [MaybeSource] without blocking the thread.
37
+ * Returns the resulting value, or `null` if no value is produced, or throws the corresponding exception if this
38
+ * [MaybeSource] produces an error.
39
+ *
40
+ * This suspending function is cancellable.
41
+ * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this
42
+ * function immediately resumes with [CancellationException] and disposes of its subscription.
43
+ */
44
+ @Suppress(" UNCHECKED_CAST" )
45
+ public suspend fun <T > MaybeSource<T>.awaitSingleOrNull (): T ? = suspendCancellableCoroutine { cont ->
46
+ subscribe(object : MaybeObserver <T > {
47
+ override fun onSubscribe (d : Disposable ) { cont.disposeOnCancellation(d) }
48
+ override fun onComplete () { cont.resume(null ) }
49
+ override fun onSuccess (t : T ) { cont.resume(t) }
50
+ override fun onError (error : Throwable ) { cont.resumeWithException(error) }
51
+ })
52
+ }
53
+
54
+ /* *
55
+ * Awaits for completion of the [MaybeSource] without blocking the thread.
56
+ * Returns the resulting value, or throws if either no value is produced or this [MaybeSource] produces an error.
57
+ *
58
+ * This suspending function is cancellable.
59
+ * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this
60
+ * function immediately resumes with [CancellationException] and disposes of its subscription.
61
+ *
62
+ * @throws NoSuchElementException if no elements were produced by this [MaybeSource].
63
+ */
64
+ public suspend fun <T > MaybeSource<T>.awaitSingle (): T = awaitSingleOrNull() ? : throw NoSuchElementException ()
65
+
34
66
/* *
35
67
* Awaits for completion of the maybe without blocking a thread.
36
68
* Returns the resulting value, null if no value was produced or throws the corresponding exception if this
@@ -40,8 +72,12 @@ public suspend fun CompletableSource.await(): Unit = suspendCancellableCoroutine
40
72
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
41
73
* immediately resumes with [CancellationException].
42
74
*/
43
- @Suppress(" UNCHECKED_CAST" )
44
- public suspend fun <T > MaybeSource<T>.await (): T ? = (this as MaybeSource <T ?>).awaitOrDefault(null )
75
+ @Deprecated(
76
+ message = " Deprecated in favor of awaitSingleOrNull()" ,
77
+ level = DeprecationLevel .WARNING ,
78
+ replaceWith = ReplaceWith (" this.awaitSingleOrNull()" )
79
+ )
80
+ public suspend fun <T > MaybeSource<T>.await (): T ? = awaitSingleOrNull()
45
81
46
82
/* *
47
83
* Awaits for completion of the maybe without blocking a thread.
@@ -52,24 +88,22 @@ public suspend fun <T> MaybeSource<T>.await(): T? = (this as MaybeSource<T?>).aw
52
88
* If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
53
89
* immediately resumes with [CancellationException].
54
90
*/
55
- public suspend fun <T > MaybeSource<T>.awaitOrDefault (default : T ): T = suspendCancellableCoroutine { cont ->
56
- subscribe(object : MaybeObserver <T > {
57
- override fun onSubscribe (d : Disposable ) { cont.disposeOnCancellation(d) }
58
- override fun onComplete () { cont.resume(default) }
59
- override fun onSuccess (t : T ) { cont.resume(t) }
60
- override fun onError (error : Throwable ) { cont.resumeWithException(error) }
61
- })
62
- }
91
+ @Deprecated(
92
+ message = " Deprecated in favor of awaitSingleOrNull()" ,
93
+ level = DeprecationLevel .WARNING ,
94
+ replaceWith = ReplaceWith (" this.awaitSingleOrNull() ?: default" )
95
+ )
96
+ public suspend fun <T > MaybeSource<T>.awaitOrDefault (default : T ): T = awaitSingleOrNull() ? : default
63
97
64
98
// ------------------------ SingleSource ------------------------
65
99
66
100
/* *
67
- * Awaits for completion of the single value without blocking a thread.
68
- * Returns the resulting value or throws the corresponding exception if this single had produced error.
101
+ * Awaits for completion of the single value response without blocking the thread.
102
+ * Returns the resulting value, or throws the corresponding exception if this response produces an error.
69
103
*
70
104
* This suspending function is cancellable.
71
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
72
- * immediately resumes with [CancellationException].
105
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
106
+ * function immediately disposes of its subscription and resumes with [CancellationException].
73
107
*/
74
108
public suspend fun <T > SingleSource<T>.await (): T = suspendCancellableCoroutine { cont ->
75
109
subscribe(object : SingleObserver <T > {
@@ -82,69 +116,73 @@ public suspend fun <T> SingleSource<T>.await(): T = suspendCancellableCoroutine
82
116
// ------------------------ ObservableSource ------------------------
83
117
84
118
/* *
85
- * Awaits for the first value from the given observable without blocking a thread.
86
- * Returns the resulting value or throws the corresponding exception if this observable had produced error .
119
+ * Awaits the first value from the given [Observable] without blocking the thread and returns the resulting value, or,
120
+ * if the observable has produced an error, throws the corresponding exception.
87
121
*
88
122
* This suspending function is cancellable.
89
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
90
- * immediately resumes with [CancellationException].
123
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
124
+ * function immediately disposes of its subscription and resumes with [CancellationException].
91
125
*
92
- * @throws NoSuchElementException if observable does not emit any value
126
+ * @throws NoSuchElementException if the observable does not emit any value
93
127
*/
94
128
public suspend fun <T > ObservableSource<T>.awaitFirst (): T = awaitOne(Mode .FIRST )
95
129
96
130
/* *
97
- * Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a
98
- * thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
131
+ * Awaits the first value from the given [Observable], or returns the [default] value if none is emitted, without
132
+ * blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
133
+ * corresponding exception.
99
134
*
100
135
* This suspending function is cancellable.
101
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
102
- * immediately resumes with [CancellationException].
136
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
137
+ * function immediately disposes of its subscription and resumes with [CancellationException].
103
138
*/
104
139
public suspend fun <T > ObservableSource<T>.awaitFirstOrDefault (default : T ): T = awaitOne(Mode .FIRST_OR_DEFAULT , default)
105
140
106
141
/* *
107
- * Awaits for the first value from the given observable or `null` value if none is emitted without blocking a
108
- * thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
142
+ * Awaits the first value from the given [Observable], or returns `null` if none is emitted, without blocking the
143
+ * thread, and returns the resulting value, or, if this observable has produced an error, throws the corresponding
144
+ * exception.
109
145
*
110
146
* This suspending function is cancellable.
111
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
112
- * immediately resumes with [CancellationException].
147
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
148
+ * function immediately disposes of its subscription and resumes with [CancellationException].
113
149
*/
114
150
public suspend fun <T > ObservableSource<T>.awaitFirstOrNull (): T ? = awaitOne(Mode .FIRST_OR_DEFAULT )
115
151
116
152
/* *
117
- * Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a
118
- * thread and returns the resulting value or throws the corresponding exception if this observable had produced error.
153
+ * Awaits the first value from the given [Observable], or calls [defaultValue] to get a value if none is emitted,
154
+ * without blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws
155
+ * the corresponding exception.
119
156
*
120
157
* This suspending function is cancellable.
121
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
122
- * immediately resumes with [CancellationException].
158
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
159
+ * function immediately disposes of its subscription and resumes with [CancellationException].
123
160
*/
124
- public suspend fun <T > ObservableSource<T>.awaitFirstOrElse (defaultValue : () -> T ): T = awaitOne(Mode .FIRST_OR_DEFAULT ) ? : defaultValue()
161
+ public suspend fun <T > ObservableSource<T>.awaitFirstOrElse (defaultValue : () -> T ): T =
162
+ awaitOne(Mode .FIRST_OR_DEFAULT ) ? : defaultValue()
125
163
126
164
/* *
127
- * Awaits for the last value from the given observable without blocking a thread.
128
- * Returns the resulting value or throws the corresponding exception if this observable had produced error.
165
+ * Awaits the last value from the given [Observable] without blocking the thread and
166
+ * returns the resulting value, or, if this observable has produced an error, throws the corresponding exception .
129
167
*
130
168
* This suspending function is cancellable.
131
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
132
- * immediately resumes with [CancellationException].
169
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
170
+ * function immediately disposes of its subscription and resumes with [CancellationException].
133
171
*
134
- * @throws NoSuchElementException if observable does not emit any value
172
+ * @throws NoSuchElementException if the observable does not emit any value
135
173
*/
136
174
public suspend fun <T > ObservableSource<T>.awaitLast (): T = awaitOne(Mode .LAST )
137
175
138
176
/* *
139
- * Awaits for the single value from the given observable without blocking a thread.
140
- * Returns the resulting value or throws the corresponding exception if this observable had produced error .
177
+ * Awaits the single value from the given observable without blocking the thread and returns the resulting value, or,
178
+ * if this observable has produced an error, throws the corresponding exception.
141
179
*
142
180
* This suspending function is cancellable.
143
- * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function
144
- * immediately resumes with [CancellationException].
181
+ * If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
182
+ * function immediately disposes of its subscription and resumes with [CancellationException].
145
183
*
146
- * @throws NoSuchElementException if observable does not emit any value
147
- * @throws IllegalArgumentException if observable emits more than one value
184
+ * @throws NoSuchElementException if the observable does not emit any value
185
+ * @throws IllegalArgumentException if the observable emits more than one value
148
186
*/
149
187
public suspend fun <T > ObservableSource<T>.awaitSingle (): T = awaitOne(Mode .SINGLE )
150
188
0 commit comments