Skip to content

Commit a01d0bd

Browse files
committed
Fix the docs for await* in -jdk9 and -reactive
1 parent b6651f1 commit a01d0bd

File tree

2 files changed

+36
-27
lines changed
  • reactive

2 files changed

+36
-27
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirst(): T =
2323
FlowAdapters.toPublisher(this).awaitFirst()
2424

2525
/**
26-
* Awaits the first value from the given observable, or returns the [default] value if none is emitted, without blocking
27-
* the thread, and returns the resulting value, or, if this observable has produced an error, throws the corresponding
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
2828
* exception.
2929
*
3030
* This suspending function is cancellable.
@@ -35,8 +35,8 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirstOrDefault(default: T): T =
3535
FlowAdapters.toPublisher(this).awaitFirstOrDefault(default)
3636

3737
/**
38-
* Awaits the first value from the given observable, or returns `null` if none is emitted, without blocking the thread,
39-
* and returns the resulting value, or, if this observable has produced an error, throws the corresponding exception.
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.
4040
*
4141
* This suspending function is cancellable.
4242
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
@@ -46,8 +46,8 @@ public suspend fun <T> Flow.Publisher<T>.awaitFirstOrNull(): T? =
4646
FlowAdapters.toPublisher(this).awaitFirstOrNull()
4747

4848
/**
49-
* Awaits the first value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
50-
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
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
5151
* corresponding exception.
5252
*
5353
* This suspending function is cancellable.

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

+30-21
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import org.reactivestreams.Publisher
99
import org.reactivestreams.Subscriber
1010
import org.reactivestreams.Subscription
1111
import java.lang.IllegalStateException
12-
import java.util.*
1312
import kotlin.NoSuchElementException
1413
import kotlin.coroutines.*
1514

@@ -26,8 +25,8 @@ import kotlin.coroutines.*
2625
public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST)
2726

2827
/**
29-
* Awaits the first value from the given observable, or returns the [default] value if none is emitted, without blocking
30-
* the thread, and returns the resulting value, or, if this observable has produced an error, throws the corresponding
28+
* Awaits the first value from the given publisher, or returns the [default] value if none is emitted, without blocking
29+
* the thread, and returns the resulting value, or, if this publisher has produced an error, throws the corresponding
3130
* exception.
3231
*
3332
* This suspending function is cancellable.
@@ -37,8 +36,8 @@ public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST)
3736
public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default)
3837

3938
/**
40-
* Awaits the first value from the given observable, or returns `null` if none is emitted, without blocking the thread,
41-
* and returns the resulting value, or, if this observable has produced an error, throws the corresponding exception.
39+
* Awaits the first value from the given publisher, or returns `null` if none is emitted, without blocking the thread,
40+
* and returns the resulting value, or, if this publisher has produced an error, throws the corresponding exception.
4241
*
4342
* This suspending function is cancellable.
4443
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
@@ -47,8 +46,8 @@ public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOn
4746
public suspend fun <T> Publisher<T>.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT)
4847

4948
/**
50-
* Awaits the first value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
51-
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
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
5251
* corresponding exception.
5352
*
5453
* This suspending function is cancellable.
@@ -83,8 +82,8 @@ public suspend fun <T> Publisher<T>.awaitLast(): T = awaitOne(Mode.LAST)
8382
public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE)
8483

8584
/**
86-
* Awaits the single value from the given observable, or returns the [default] value if none is emitted, without
87-
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
85+
* Awaits the single value from the given publisher, or returns the [default] value if none is emitted, without
86+
* blocking the thread, and returns the resulting value, or, if this publisher has produced an error, throws the
8887
* corresponding exception.
8988
*
9089
* This suspending function is cancellable.
@@ -94,26 +93,34 @@ public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE)
9493
* @throws NoSuchElementException if the publisher does not emit any value
9594
* @throws IllegalArgumentException if the publisher emits more than one value
9695
*/
96+
@Deprecated(
97+
message = "Deprecated without a replacement due to its name incorrectly conveying the behavior",
98+
level = DeprecationLevel.WARNING
99+
)
97100
public suspend fun <T> Publisher<T>.awaitSingleOrDefault(default: T): T = awaitOne(Mode.SINGLE_OR_DEFAULT, default)
98101

99102
/**
100-
* Awaits the single value from the given observable without blocking the thread and returns the resulting value, or, if
101-
* this observable has produced an error, throws the corresponding exception. If more than one value or none were
103+
* Awaits the single value from the given publisher without blocking the thread and returns the resulting value, or, if
104+
* this publisher has produced an error, throws the corresponding exception. If more than one value or none were
102105
* produced by the publisher, `null` is returned.
103106
*
104107
* This suspending function is cancellable.
105108
* If the [Job] of the current coroutine is cancelled or completed while the suspending function is waiting, this
106109
* function immediately cancels its [Subscription] and resumes with [CancellationException].
110+
*
111+
* @throws IllegalArgumentException if the publisher emits more than one value
107112
*/
108-
public suspend fun <T> Publisher<T>.awaitSingleOrNull(): T? = try {
109-
awaitOne(Mode.SINGLE_OR_DEFAULT)
110-
} catch (e: TooManyElementsException) {
111-
null
112-
}
113+
@Deprecated(
114+
message = "Deprecated without a replacement due to its name incorrectly conveying the behavior. " +
115+
"There is a specialized version for Reactor's Mono, please use that where applicable.",
116+
level = DeprecationLevel.WARNING,
117+
replaceWith = ReplaceWith("this.awaitSingleOrNull()", "kotlinx.coroutines.reactor")
118+
)
119+
public suspend fun <T> Publisher<T>.awaitSingleOrNull(): T? = awaitOne(Mode.SINGLE_OR_DEFAULT)
113120

114121
/**
115-
* Awaits the single value from the given observable, or calls [defaultValue] to get a value if none is emitted, without
116-
* blocking the thread, and returns the resulting value, or, if this observable has produced an error, throws the
122+
* Awaits the single value from the given publisher, or calls [defaultValue] to get a value if none is emitted, without
123+
* blocking the thread, and returns the resulting value, or, if this publisher has produced an error, throws the
117124
* corresponding exception.
118125
*
119126
* This suspending function is cancellable.
@@ -122,13 +129,15 @@ public suspend fun <T> Publisher<T>.awaitSingleOrNull(): T? = try {
122129
*
123130
* @throws IllegalArgumentException if the publisher emits more than one value
124131
*/
132+
@Deprecated(
133+
message = "Deprecated without a replacement due to its name incorrectly conveying the behavior",
134+
level = DeprecationLevel.WARNING
135+
)
125136
public suspend fun <T> Publisher<T>.awaitSingleOrElse(defaultValue: () -> T): T =
126137
awaitOne(Mode.SINGLE_OR_DEFAULT) ?: defaultValue()
127138

128139
// ------------------------ private ------------------------
129140

130-
private class TooManyElementsException(message: String): IllegalArgumentException(message)
131-
132141
private enum class Mode(val s: String) {
133142
FIRST("awaitFirst"),
134143
FIRST_OR_DEFAULT("awaitFirstOrDefault"),
@@ -195,7 +204,7 @@ private suspend fun <T> Publisher<T>.awaitOne(
195204
/* the check for `cont.isActive` is needed in case `sub.cancel() above calls `onComplete` or
196205
`onError` on its own. */
197206
if (cont.isActive) {
198-
cont.resumeWithException(TooManyElementsException("More than one onNext value for $mode"))
207+
cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode"))
199208
}
200209
} else {
201210
value = t

0 commit comments

Comments
 (0)