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 | |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 19 | import kotlinx.atomicfu.* |
| 20 | import kotlinx.coroutines.experimental.channels.* |
| 21 | import org.reactivestreams.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 22 | |
| 23 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 24 | * Subscribes to this [Publisher] and returns a channel to receive elements emitted by it. |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 25 | * The resulting channel shall be [cancelled][ReceiveChannel.cancel] to unsubscribe from this publisher. |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 26 | * @param request how many items to request from publisher in advance (optional, on-demand request by default). |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 27 | */ |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 28 | @Suppress("CONFLICTING_OVERLOADS") |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 29 | public fun <T> Publisher<T>.openSubscription(request: Int = 0): ReceiveChannel<T> { |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 30 | val channel = SubscriptionChannel<T>(request) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 31 | subscribe(channel) |
| 32 | return channel |
| 33 | } |
| 34 | |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 35 | /** @suppress **Deprecated**: Left here for binary compatibility */ |
| 36 | @JvmOverloads // for binary compatibility |
| 37 | @Deprecated(level = DeprecationLevel.HIDDEN, message = "Left here for binary compatibility") |
| 38 | @Suppress("CONFLICTING_OVERLOADS") |
| 39 | public fun <T> Publisher<T>.openSubscription(request: Int = 0): SubscriptionReceiveChannel<T> = |
| 40 | openSubscription(request) as SubscriptionReceiveChannel<T> |
| 41 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 42 | /** |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 43 | * @suppress **Deprecated**: Renamed to [openSubscription] |
| 44 | */ |
| 45 | @Deprecated(message = "Renamed to `openSubscription`", |
| 46 | replaceWith = ReplaceWith("openSubscription()")) |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 47 | public fun <T> Publisher<T>.open(): SubscriptionReceiveChannel<T> = |
| 48 | openSubscription() as SubscriptionReceiveChannel<T> |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 49 | |
| 50 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 51 | * Subscribes to this [Publisher] and returns an iterator to receive elements emitted by it. |
| 52 | * |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 53 | * This is a shortcut for `open().iterator()`. See [openSubscription] if you need an ability to manually |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 54 | * unsubscribe from the observable. |
| 55 | */ |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 56 | @Suppress("DeprecatedCallableAddReplaceWith") |
| 57 | @Deprecated(message = |
| 58 | "This iteration operator for `for (x in source) { ... }` loop is deprecated, " + |
| 59 | "because it leaves code vulnerable to leaving unclosed subscriptions on exception. " + |
| 60 | "Use `source.consumeEach { x -> ... }`.") |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 61 | public operator fun <T> Publisher<T>.iterator() = openSubscription().iterator() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 62 | |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 63 | /** |
| 64 | * Subscribes to this [Publisher] and performs the specified action for each received element. |
| 65 | */ |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 66 | public suspend inline fun <T> Publisher<T>.consumeEach(action: (T) -> Unit) { |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 67 | val channel = openSubscription() |
| 68 | for (x in channel) action(x) |
| 69 | channel.cancel() |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 70 | } |
| 71 | |
Roman Elizarov | 537c359 | 2017-08-16 19:04:31 +0300 | [diff] [blame] | 72 | /** |
| 73 | * @suppress: **Deprecated**: binary compatibility with old code |
| 74 | */ |
| 75 | @Deprecated("binary compatibility with old code", level = DeprecationLevel.HIDDEN) |
| 76 | public suspend fun <T> Publisher<T>.consumeEach(action: suspend (T) -> Unit) = |
| 77 | consumeEach { action(it) } |
| 78 | |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 79 | private class SubscriptionChannel<T>( |
| 80 | private val request: Int |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 81 | ) : LinkedListChannel<T>(), ReceiveChannel<T>, Subscriber<T>, SubscriptionReceiveChannel<T> { |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 82 | init { |
| 83 | require(request >= 0) { "Invalid request size: $request" } |
| 84 | } |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 85 | |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 86 | @Volatile |
| 87 | private var subscription: Subscription? = null |
| 88 | |
| 89 | // requested from subscription minus number of received minus number of enqueued receivers, |
| 90 | // can be negative if we have receivers, but no subscription yet |
| 91 | private val _requested = atomic(0) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 92 | |
| 93 | // AbstractChannel overrides |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 94 | override fun onReceiveEnqueued() { |
| 95 | _requested.loop { wasRequested -> |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 96 | val subscription = this.subscription |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 97 | val needRequested = wasRequested - 1 |
| 98 | if (subscription != null && needRequested < 0) { // need to request more from subscription |
| 99 | // try to fixup by making request |
| 100 | if (wasRequested != request && !_requested.compareAndSet(wasRequested, request)) |
| 101 | return@loop // continue looping if failed |
| 102 | subscription.request((request - needRequested).toLong()) |
| 103 | return |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 104 | } |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 105 | // just do book-keeping |
| 106 | if (_requested.compareAndSet(wasRequested, needRequested)) return |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 110 | override fun onReceiveDequeued() { |
| 111 | _requested.incrementAndGet() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | override fun afterClose(cause: Throwable?) { |
| 115 | subscription?.cancel() |
| 116 | } |
| 117 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 118 | // Subscriber overrides |
| 119 | override fun onSubscribe(s: Subscription) { |
| 120 | subscription = s |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 121 | while (true) { // lock-free loop on _requested |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 122 | if (isClosedForSend) { |
| 123 | s.cancel() |
| 124 | return |
| 125 | } |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 126 | val wasRequested = _requested.value |
| 127 | if (wasRequested >= request) return // ok -- normal story |
| 128 | // otherwise, receivers came before we had subscription or need to make initial request |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 129 | // try to fixup by making request |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 130 | if (!_requested.compareAndSet(wasRequested, request)) continue |
| 131 | s.request((request - wasRequested).toLong()) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 132 | return |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | override fun onNext(t: T) { |
Roman Elizarov | 3e342e3 | 2018-01-13 20:05:51 +0300 | [diff] [blame] | 137 | _requested.decrementAndGet() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 138 | offer(t) |
| 139 | } |
| 140 | |
| 141 | override fun onComplete() { |
| 142 | close(cause = null) |
| 143 | } |
| 144 | |
| 145 | override fun onError(e: Throwable) { |
| 146 | close(cause = e) |
| 147 | } |
| 148 | } |
| 149 | |