Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Vsevolod Tolstopyatov | 6d1a6e3 | 2020-02-18 15:28:00 +0300 | [diff] [blame] | 2 | * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 5 | package kotlinx.coroutines.reactive |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 6 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 7 | import kotlinx.coroutines.CancellationException |
| 8 | import kotlinx.coroutines.Job |
| 9 | import kotlinx.coroutines.suspendCancellableCoroutine |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 10 | import org.reactivestreams.Publisher |
| 11 | import org.reactivestreams.Subscriber |
| 12 | import org.reactivestreams.Subscription |
SokolovaMaria | 1dcfd97 | 2019-08-09 17:35:14 +0300 | [diff] [blame] | 13 | import java.util.* |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 14 | import kotlin.coroutines.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Awaits for the first value from the given publisher without blocking a thread and |
| 18 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 19 | * |
| 20 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 21 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 22 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 23 | * |
| 24 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 25 | */ |
| 26 | public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST) |
| 27 | |
| 28 | /** |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 29 | * Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a |
| 30 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 31 | * |
| 32 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 33 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 34 | * immediately resumes with [CancellationException]. |
| 35 | */ |
| 36 | public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default) |
| 37 | |
| 38 | /** |
Konrad Kamiński | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 39 | * Awaits for the first value from the given observable or `null` value if none is emitted without blocking a |
| 40 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 41 | * |
| 42 | * This suspending function is cancellable. |
| 43 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 44 | * immediately resumes with [CancellationException]. |
| 45 | */ |
| 46 | public suspend fun <T> Publisher<T>.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT) |
| 47 | |
| 48 | /** |
| 49 | * Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a |
| 50 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 51 | * |
| 52 | * This suspending function is cancellable. |
| 53 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 54 | * immediately resumes with [CancellationException]. |
| 55 | */ |
| 56 | public suspend fun <T> Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T = awaitOne(Mode.FIRST_OR_DEFAULT) ?: defaultValue() |
| 57 | |
| 58 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 59 | * Awaits for the last value from the given publisher without blocking a thread and |
| 60 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 61 | * |
| 62 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 63 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 64 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 65 | * |
| 66 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 67 | */ |
| 68 | public suspend fun <T> Publisher<T>.awaitLast(): T = awaitOne(Mode.LAST) |
| 69 | |
| 70 | /** |
| 71 | * Awaits for the single value from the given publisher without blocking a thread and |
| 72 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 73 | * |
| 74 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 75 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 76 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 77 | * |
| 78 | * @throws NoSuchElementException if publisher does not emit any value |
| 79 | * @throws IllegalArgumentException if publisher emits more than one value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 80 | */ |
| 81 | public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE) |
| 82 | |
Sébastien Deleuze | 5dd94a3 | 2020-09-22 17:18:53 +0200 | [diff] [blame] | 83 | /** |
| 84 | * Awaits for the single value from the given publisher or the [default] value if none is emitted without blocking a thread and |
| 85 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 86 | * |
| 87 | * This suspending function is cancellable. |
| 88 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 89 | * immediately resumes with [CancellationException]. |
| 90 | * |
| 91 | * @throws NoSuchElementException if publisher does not emit any value |
| 92 | * @throws IllegalArgumentException if publisher emits more than one value |
| 93 | */ |
| 94 | public suspend fun <T> Publisher<T>.awaitSingleOrDefault(default: T): T = awaitOne(Mode.SINGLE_OR_DEFAULT, default) |
| 95 | |
| 96 | /** |
| 97 | * Awaits for the single value from the given publisher or `null` value if none is emitted without blocking a thread and |
| 98 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 99 | * |
| 100 | * 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]. |
| 103 | * |
| 104 | * @throws NoSuchElementException if publisher does not emit any value |
| 105 | * @throws IllegalArgumentException if publisher emits more than one value |
| 106 | */ |
| 107 | public suspend fun <T> Publisher<T>.awaitSingleOrNull(): T = awaitOne(Mode.SINGLE_OR_DEFAULT) |
| 108 | |
| 109 | /** |
| 110 | * Awaits for the single value from the given publisher or call [defaultValue] to get a value if none is emitted without blocking a thread and |
| 111 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 112 | * |
| 113 | * This suspending function is cancellable. |
| 114 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 115 | * immediately resumes with [CancellationException]. |
| 116 | * |
| 117 | * @throws NoSuchElementException if publisher does not emit any value |
| 118 | * @throws IllegalArgumentException if publisher emits more than one value |
| 119 | */ |
| 120 | public suspend fun <T> Publisher<T>.awaitSingleOrElse(defaultValue: () -> T): T = awaitOne(Mode.SINGLE_OR_DEFAULT) ?: defaultValue() |
| 121 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 122 | // ------------------------ private ------------------------ |
| 123 | |
| 124 | private enum class Mode(val s: String) { |
| 125 | FIRST("awaitFirst"), |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 126 | FIRST_OR_DEFAULT("awaitFirstOrDefault"), |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 127 | LAST("awaitLast"), |
Sébastien Deleuze | 5dd94a3 | 2020-09-22 17:18:53 +0200 | [diff] [blame] | 128 | SINGLE("awaitSingle"), |
| 129 | SINGLE_OR_DEFAULT("awaitSingleOrDefault"); |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 130 | override fun toString(): String = s |
| 131 | } |
| 132 | |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 133 | private suspend fun <T> Publisher<T>.awaitOne( |
| 134 | mode: Mode, |
| 135 | default: T? = null |
| 136 | ): T = suspendCancellableCoroutine { cont -> |
SokolovaMaria | 1dcfd97 | 2019-08-09 17:35:14 +0300 | [diff] [blame] | 137 | injectCoroutineContext(cont.context).subscribe(object : Subscriber<T> { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 138 | private lateinit var subscription: Subscription |
| 139 | private var value: T? = null |
| 140 | private var seenValue = false |
| 141 | |
| 142 | override fun onSubscribe(sub: Subscription) { |
| 143 | subscription = sub |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 144 | cont.invokeOnCancellation { sub.cancel() } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 145 | sub.request(if (mode == Mode.FIRST) 1 else Long.MAX_VALUE) |
| 146 | } |
| 147 | |
| 148 | override fun onNext(t: T) { |
| 149 | when (mode) { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 150 | Mode.FIRST, Mode.FIRST_OR_DEFAULT -> { |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 151 | if (!seenValue) { |
| 152 | seenValue = true |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 153 | subscription.cancel() |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 154 | cont.resume(t) |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 155 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 156 | } |
Sébastien Deleuze | 5dd94a3 | 2020-09-22 17:18:53 +0200 | [diff] [blame] | 157 | Mode.LAST, Mode.SINGLE, Mode.SINGLE_OR_DEFAULT -> { |
| 158 | if ((mode == Mode.SINGLE || mode == Mode.SINGLE_OR_DEFAULT) && seenValue) { |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 159 | subscription.cancel() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 160 | if (cont.isActive) |
Vsevolod Tolstopyatov | 85c7f4d | 2019-02-19 15:55:24 +0300 | [diff] [blame] | 161 | cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode")) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 162 | } else { |
| 163 | value = t |
| 164 | seenValue = true |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
Roman Elizarov | 87142a2 | 2017-07-21 19:08:45 +0300 | [diff] [blame] | 170 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 171 | override fun onComplete() { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 172 | if (seenValue) { |
| 173 | if (cont.isActive) cont.resume(value as T) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 174 | return |
| 175 | } |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 176 | when { |
Sébastien Deleuze | 5dd94a3 | 2020-09-22 17:18:53 +0200 | [diff] [blame] | 177 | (mode == Mode.FIRST_OR_DEFAULT || mode == Mode.SINGLE_OR_DEFAULT) -> { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 178 | cont.resume(default as T) |
| 179 | } |
| 180 | cont.isActive -> { |
| 181 | cont.resumeWithException(NoSuchElementException("No value received via onNext for $mode")) |
| 182 | } |
| 183 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | override fun onError(e: Throwable) { |
| 187 | cont.resumeWithException(e) |
| 188 | } |
| 189 | }) |
| 190 | } |
| 191 | |