Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame] | 2 | * Copyright 2016-2018 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 |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 13 | import kotlin.coroutines.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * Awaits for the first value from the given publisher without blocking a thread and |
| 17 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 18 | * |
| 19 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 20 | * 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] | 21 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 22 | * |
| 23 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 24 | */ |
| 25 | public suspend fun <T> Publisher<T>.awaitFirst(): T = awaitOne(Mode.FIRST) |
| 26 | |
| 27 | /** |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 28 | * Awaits for the first value from the given observable or the [default] value if none is emitted without blocking a |
| 29 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 30 | * |
| 31 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 32 | * 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] | 33 | * immediately resumes with [CancellationException]. |
| 34 | */ |
| 35 | public suspend fun <T> Publisher<T>.awaitFirstOrDefault(default: T): T = awaitOne(Mode.FIRST_OR_DEFAULT, default) |
| 36 | |
| 37 | /** |
Konrad KamiĆski | b8ed47c | 2018-02-06 12:17:53 +0100 | [diff] [blame] | 38 | * Awaits for the first value from the given observable or `null` value if none is emitted without blocking a |
| 39 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 40 | * |
| 41 | * This suspending function is cancellable. |
| 42 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 43 | * immediately resumes with [CancellationException]. |
| 44 | */ |
| 45 | public suspend fun <T> Publisher<T>.awaitFirstOrNull(): T? = awaitOne(Mode.FIRST_OR_DEFAULT) |
| 46 | |
| 47 | /** |
| 48 | * Awaits for the first value from the given observable or call [defaultValue] to get a value if none is emitted without blocking a |
| 49 | * thread and returns the resulting value or throws the corresponding exception if this observable had produced error. |
| 50 | * |
| 51 | * This suspending function is cancellable. |
| 52 | * If the [Job] of the current coroutine is cancelled or completed while this suspending function is waiting, this function |
| 53 | * immediately resumes with [CancellationException]. |
| 54 | */ |
| 55 | public suspend fun <T> Publisher<T>.awaitFirstOrElse(defaultValue: () -> T): T = awaitOne(Mode.FIRST_OR_DEFAULT) ?: defaultValue() |
| 56 | |
| 57 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 58 | * Awaits for the last value from the given publisher without blocking a thread and |
| 59 | * returns the resulting value or throws the corresponding exception if this publisher had produced error. |
| 60 | * |
| 61 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 62 | * 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] | 63 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 64 | * |
| 65 | * @throws NoSuchElementException if publisher does not emit any value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 66 | */ |
| 67 | public suspend fun <T> Publisher<T>.awaitLast(): T = awaitOne(Mode.LAST) |
| 68 | |
| 69 | /** |
| 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. |
| 72 | * |
| 73 | * This suspending function is cancellable. |
Roman Elizarov | d82b3a9 | 2017-06-23 21:52:08 +0300 | [diff] [blame] | 74 | * 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] | 75 | * immediately resumes with [CancellationException]. |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 76 | * |
| 77 | * @throws NoSuchElementException if publisher does not emit any value |
| 78 | * @throws IllegalArgumentException if publisher emits more than one value |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 79 | */ |
| 80 | public suspend fun <T> Publisher<T>.awaitSingle(): T = awaitOne(Mode.SINGLE) |
| 81 | |
| 82 | // ------------------------ private ------------------------ |
| 83 | |
| 84 | private enum class Mode(val s: String) { |
| 85 | FIRST("awaitFirst"), |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 86 | FIRST_OR_DEFAULT("awaitFirstOrDefault"), |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 87 | LAST("awaitLast"), |
| 88 | SINGLE("awaitSingle"); |
| 89 | override fun toString(): String = s |
| 90 | } |
| 91 | |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 92 | private suspend fun <T> Publisher<T>.awaitOne( |
| 93 | mode: Mode, |
| 94 | default: T? = null |
| 95 | ): T = suspendCancellableCoroutine { cont -> |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 96 | subscribe(object : Subscriber<T> { |
| 97 | private lateinit var subscription: Subscription |
| 98 | private var value: T? = null |
| 99 | private var seenValue = false |
| 100 | |
| 101 | override fun onSubscribe(sub: Subscription) { |
| 102 | subscription = sub |
Vsevolod Tolstopyatov | 80a2947 | 2018-04-17 16:02:02 +0300 | [diff] [blame] | 103 | cont.invokeOnCancellation { sub.cancel() } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 104 | sub.request(if (mode == Mode.FIRST) 1 else Long.MAX_VALUE) |
| 105 | } |
| 106 | |
| 107 | override fun onNext(t: T) { |
| 108 | when (mode) { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 109 | Mode.FIRST, Mode.FIRST_OR_DEFAULT -> { |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 110 | if (!seenValue) { |
| 111 | seenValue = true |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 112 | subscription.cancel() |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 113 | cont.resume(t) |
Roman Elizarov | 1b9cbf4 | 2017-07-12 12:43:24 +0300 | [diff] [blame] | 114 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 115 | } |
| 116 | Mode.LAST, Mode.SINGLE -> { |
| 117 | if (mode == Mode.SINGLE && seenValue) { |
Roman Elizarov | 3e9f244 | 2018-04-28 17:38:22 +0300 | [diff] [blame] | 118 | subscription.cancel() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 119 | if (cont.isActive) |
Vsevolod Tolstopyatov | 85c7f4d | 2019-02-19 15:55:24 +0300 | [diff] [blame^] | 120 | cont.resumeWithException(IllegalArgumentException("More than one onNext value for $mode")) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 121 | } else { |
| 122 | value = t |
| 123 | seenValue = true |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
Roman Elizarov | 87142a2 | 2017-07-21 19:08:45 +0300 | [diff] [blame] | 129 | @Suppress("UNCHECKED_CAST") |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 130 | override fun onComplete() { |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 131 | if (seenValue) { |
| 132 | if (cont.isActive) cont.resume(value as T) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 133 | return |
| 134 | } |
Roman Elizarov | 4a67afb | 2017-03-16 11:10:47 +0300 | [diff] [blame] | 135 | when { |
| 136 | mode == Mode.FIRST_OR_DEFAULT -> { |
| 137 | cont.resume(default as T) |
| 138 | } |
| 139 | cont.isActive -> { |
| 140 | cont.resumeWithException(NoSuchElementException("No value received via onNext for $mode")) |
| 141 | } |
| 142 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | override fun onError(e: Throwable) { |
| 146 | cont.resumeWithException(e) |
| 147 | } |
| 148 | }) |
| 149 | } |
| 150 | |