Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-2017 JetBrains s.r.o. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package kotlinx.coroutines.experimental.reactive |
| 18 | |
| 19 | import kotlinx.coroutines.experimental.CancellationException |
| 20 | import kotlinx.coroutines.experimental.Job |
| 21 | import kotlinx.coroutines.experimental.suspendCancellableCoroutine |
| 22 | import org.reactivestreams.Publisher |
| 23 | import org.reactivestreams.Subscriber |
| 24 | import org.reactivestreams.Subscription |
| 25 | |
| 26 | /** |
| 27 | * Awaits for the first value from the given publisher without blocking a thread and |
| 28 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 29 | * |
| 30 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 31 | * 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] | 32 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 33 | * |
| 34 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 35 | */ |
| 36 | public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST) |
| 37 | |
| 38 | /** |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 39 | * Awaits for the first value from the given observable or the [default] 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. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 43 | * 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] | 44 | * immediately resumes with [CancellationException]. |
| 45 | */ |
| 46 | public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default) |
| 47 | |
| 48 | /** |
Konrad KamiĆski | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 49 | * Awaits for the first value from the given observable or `null` 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>.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT) |
| 57 | |
| 58 | /** |
| 59 | * Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a |
| 60 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 61 | * |
| 62 | * This suspending function is cancellable. |
| 63 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 64 | * immediately resumes with [CancellationException]. |
| 65 | */ |
| 66 | public suspend fun <T> Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T = awaitOne(Mode.FIRST_OR_DEFAULT) ?: defaultValue() |
| 67 | |
| 68 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 69 | * Awaits for the last value from the given publisher without blocking a thread and |
| 70 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 71 | * |
| 72 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 73 | * 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] | 74 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 75 | * |
| 76 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 77 | */ |
| 78 | public suspend fun <T> Publisher<T>.awaitLast(): T = awaitOne(Mode.LAST) |
| 79 | |
| 80 | /** |
| 81 | * Awaits for the single value from the given publisher without blocking a thread and |
| 82 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 83 | * |
| 84 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 85 | * 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] | 86 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 87 | * |
| 88 | * @throws NoSuchElementException if publisher does not emit any value |
| 89 | * @throws IllegalArgumentException if publisher emits more than one value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 90 | */ |
| 91 | public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE) |
| 92 | |
| 93 | // ------------------------ private ------------------------ |
| 94 | |
| 95 | private enum class Mode(val s: String) { |
| 96 | FIRST("awaitFirst"), |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 97 | FIRST_OR_DEFAULT("awaitFirstOrDefault"), |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 98 | LAST("awaitLast"), |
| 99 | SINGLE("awaitSingle"); |
| 100 | override fun toString(): String = s |
| 101 | } |
| 102 | |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 103 | private suspend fun <T> Publisher<T>.awaitOne( |
| 104 | mode: Mode, |
| 105 | default: T? = null |
| 106 | ): T = suspendCancellableCoroutine { cont -> |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 107 | subscribe(object : Subscriber<T> { |
| 108 | private lateinit var subscription: Subscription |
| 109 | private var value: T? = null |
| 110 | private var seenValue = false |
| 111 | |
| 112 | override fun onSubscribe(sub: Subscription) { |
| 113 | subscription = sub |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 114 | cont.invokeOnCancellation { sub.cancel() } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 115 | sub.request(if (mode == Mode.FIRST) 1 else Long.MAX_VALUE) |
| 116 | } |
| 117 | |
| 118 | override fun onNext(t: T) { |
| 119 | when (mode) { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 120 | Mode.FIRST, Mode.FIRST_OR_DEFAULT -> { |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 121 | if (!seenValue) { |
| 122 | seenValue = true |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 123 | subscription.cancel() |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 124 | cont.resume(t) |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 125 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 126 | } |
| 127 | Mode.LAST, Mode.SINGLE -> { |
| 128 | if (mode == Mode.SINGLE && seenValue) { |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 129 | subscription.cancel() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 130 | if (cont.isActive) |
| 131 | cont.resumeWithException(IllegalArgumentException("More that one onNext value for $mode")) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 132 | } else { |
| 133 | value = t |
| 134 | seenValue = true |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Roman Elizarov | 87142a2 | 2017-07-21 19:08:45 +0300 | [diff] [blame] | 140 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 141 | override fun onComplete() { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 142 | if (seenValue) { |
| 143 | if (cont.isActive) cont.resume(value as T) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 144 | return |
| 145 | } |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 146 | when { |
| 147 | mode == Mode.FIRST_OR_DEFAULT -> { |
| 148 | cont.resume(default as T) |
| 149 | } |
| 150 | cont.isActive -> { |
| 151 | cont.resumeWithException(NoSuchElementException("No value received via onNext for $mode")) |
| 152 | } |
| 153 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | override fun onError(e: Throwable) { |
| 157 | cont.resumeWithException(e) |
| 158 | } |
| 159 | }) |
| 160 | } |
| 161 | |