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 | |
| 5 | package kotlinx.coroutines.experimental.rx2 |
| 6 | |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 7 | import io.reactivex.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 8 | import io.reactivex.disposables.Disposable |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 9 | import kotlinx.coroutines.experimental.channels.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 10 | |
| 11 | /** |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 12 | * Subscribes to this [MaybeSource] and returns a channel to receive elements emitted by it. |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 13 | * The resulting channel shall be [cancelled][ReceiveChannel.cancel] to unsubscribe from this source. |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 14 | */ |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 15 | @Suppress("CONFLICTING_OVERLOADS") |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 16 | public fun <T> MaybeSource<T>.openSubscription(): ReceiveChannel<T> { |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 17 | val channel = SubscriptionChannel<T>() |
| 18 | subscribe(channel) |
| 19 | return channel |
| 20 | } |
| 21 | |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 22 | /** @suppress **Deprecated**: Left here for binary compatibility */ |
| 23 | @Deprecated(level = DeprecationLevel.HIDDEN, message = "Left here for binary compatibility") |
| 24 | @Suppress("CONFLICTING_OVERLOADS") |
| 25 | public fun <T> MaybeSource<T>.openSubscription(): SubscriptionReceiveChannel<T> = |
| 26 | openSubscription() as SubscriptionReceiveChannel<T> |
| 27 | |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 28 | /** |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 29 | * @suppress **Deprecated**: Renamed to [openSubscription] |
| 30 | */ |
| 31 | @Deprecated(message = "Renamed to `openSubscription`", |
| 32 | replaceWith = ReplaceWith("openSubscription()")) |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 33 | public fun <T> MaybeSource<T>.open(): SubscriptionReceiveChannel<T> = |
| 34 | openSubscription() as SubscriptionReceiveChannel<T> |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 35 | |
| 36 | /** |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 37 | * Subscribes to this [ObservableSource] and returns a channel to receive elements emitted by it. |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 38 | * The resulting channel shall be [cancelled][ReceiveChannel.cancel] to unsubscribe from this source. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 39 | */ |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 40 | @Suppress("CONFLICTING_OVERLOADS") |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 41 | public fun <T> ObservableSource<T>.openSubscription(): ReceiveChannel<T> { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 42 | val channel = SubscriptionChannel<T>() |
| 43 | subscribe(channel) |
| 44 | return channel |
| 45 | } |
| 46 | |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 47 | /** @suppress **Deprecated**: Left here for binary compatibility */ |
| 48 | @Deprecated(level = DeprecationLevel.HIDDEN, message = "Left here for binary compatibility") |
| 49 | @Suppress("CONFLICTING_OVERLOADS") |
| 50 | public fun <T> ObservableSource<T>.openSubscription(): SubscriptionReceiveChannel<T> = |
| 51 | openSubscription() as SubscriptionReceiveChannel<T> |
| 52 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 53 | /** |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 54 | * @suppress **Deprecated**: Renamed to [openSubscription] |
| 55 | */ |
| 56 | @Deprecated(message = "Renamed to `openSubscription`", |
| 57 | replaceWith = ReplaceWith("openSubscription()")) |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 58 | public fun <T> ObservableSource<T>.open(): SubscriptionReceiveChannel<T> = |
| 59 | openSubscription() as SubscriptionReceiveChannel<T> |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 60 | |
| 61 | /** |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 62 | * Subscribes to this [Observable] and returns an iterator to receive elements emitted by it. |
| 63 | * |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 64 | * 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] | 65 | * unsubscribe from the observable. |
| 66 | */ |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 67 | @Suppress("DeprecatedCallableAddReplaceWith") |
| 68 | @Deprecated(message = |
| 69 | "This iteration operator for `for (x in source) { ... }` loop is deprecated, " + |
| 70 | "because it leaves code vulnerable to leaving unclosed subscriptions on exception. " + |
| 71 | "Use `source.consumeEach { x -> ... }`.") |
Roman Elizarov | 0f66a6d | 2017-06-22 14:57:53 +0300 | [diff] [blame] | 72 | public operator fun <T> ObservableSource<T>.iterator() = openSubscription().iterator() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 73 | |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 74 | /** |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 75 | * Subscribes to this [MaybeSource] and performs the specified action for each received element. |
| 76 | */ |
Roman Elizarov | 537c359 | 2017-08-16 19:04:31 +0300 | [diff] [blame] | 77 | public inline suspend fun <T> MaybeSource<T>.consumeEach(action: (T) -> Unit) { |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 78 | val channel = openSubscription() |
| 79 | for (x in channel) action(x) |
| 80 | channel.cancel() |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 84 | * Subscribes to this [ObservableSource] and performs the specified action for each received element. |
| 85 | */ |
Roman Elizarov | 537c359 | 2017-08-16 19:04:31 +0300 | [diff] [blame] | 86 | public inline suspend fun <T> ObservableSource<T>.consumeEach(action: (T) -> Unit) { |
Marko Devcic | 1d6230a | 2018-04-04 20:13:08 +0200 | [diff] [blame] | 87 | val channel = openSubscription() |
| 88 | for (x in channel) action(x) |
| 89 | channel.cancel() |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 90 | } |
| 91 | |
Roman Elizarov | 537c359 | 2017-08-16 19:04:31 +0300 | [diff] [blame] | 92 | /** |
| 93 | * @suppress: **Deprecated**: binary compatibility with old code |
| 94 | */ |
| 95 | @Deprecated("binary compatibility with old code", level = DeprecationLevel.HIDDEN) |
| 96 | public suspend fun <T> ObservableSource<T>.consumeEach(action: suspend (T) -> Unit) = |
| 97 | consumeEach { action(it) } |
| 98 | |
Roman Elizarov | f5f0983 | 2018-05-16 15:10:28 +0300 | [diff] [blame] | 99 | private class SubscriptionChannel<T> : |
| 100 | LinkedListChannel<T>(), ReceiveChannel<T>, Observer<T>, MaybeObserver<T>, SubscriptionReceiveChannel<T> |
| 101 | { |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 102 | @Volatile |
| 103 | var subscription: Disposable? = null |
| 104 | |
| 105 | // AbstractChannel overrides |
| 106 | override fun afterClose(cause: Throwable?) { |
| 107 | subscription?.dispose() |
| 108 | } |
| 109 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 110 | // Observer overrider |
| 111 | override fun onSubscribe(sub: Disposable) { |
| 112 | subscription = sub |
| 113 | } |
| 114 | |
Konrad Kamiński | d6bb148 | 2017-04-07 09:26:40 +0200 | [diff] [blame] | 115 | override fun onSuccess(t: T) { |
| 116 | offer(t) |
| 117 | } |
| 118 | |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 119 | override fun onNext(t: T) { |
| 120 | offer(t) |
| 121 | } |
| 122 | |
| 123 | override fun onComplete() { |
| 124 | close(cause = null) |
| 125 | } |
| 126 | |
| 127 | override fun onError(e: Throwable) { |
| 128 | close(cause = e) |
| 129 | } |
| 130 | } |