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