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 | |
| 19 | import kotlinx.coroutines.experimental.Deferred |
| 20 | import kotlinx.coroutines.experimental.Job |
| 21 | import kotlinx.coroutines.experimental.channels.ReceiveChannel |
| 22 | import rx.* |
| 23 | import kotlin.coroutines.experimental.CoroutineContext |
| 24 | |
| 25 | /** |
| 26 | * Converts this job to the hot reactive completable that signals |
| 27 | * with [onCompleted][CompletableSubscriber.onCompleted] when the corresponding job completes. |
| 28 | * |
| 29 | * Every subscriber gets the signal at the same time. |
| 30 | * Unsubscribing from the resulting completable **does not** affect the original job in any way. |
| 31 | * |
| 32 | * @param context -- the coroutine context from which the resulting completable is going to be signalled |
| 33 | */ |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 34 | public fun Job.asCompletable(context: CoroutineContext): Completable = rxCompletable(context) { |
| 35 | this@asCompletable.join() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Converts this deferred value to the hot reactive single that signals either |
| 40 | * [onSuccess][SingleSubscriber.onSuccess] or [onError][SingleSubscriber.onError]. |
| 41 | * |
| 42 | * Every subscriber gets the same completion value. |
| 43 | * Unsubscribing from the resulting single **does not** affect the original deferred value in any way. |
| 44 | * |
| 45 | * @param context -- the coroutine context from which the resulting single is going to be signalled |
| 46 | */ |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 47 | public fun <T> Deferred<T>.asSingle(context: CoroutineContext): Single<T> = rxSingle<T>(context) { |
| 48 | this@asSingle.await() |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Converts a stream of elements received from the channel to the hot reactive observable. |
| 53 | * |
| 54 | * Every subscriber receives values from this channel in **fan-out** fashion. If the are multiple subscribers, |
| 55 | * they'll receive values in round-robin way. |
| 56 | * |
| 57 | * @param context -- the coroutine context from which the resulting observable is going to be signalled |
| 58 | */ |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 59 | public fun <T> ReceiveChannel<T>.asObservable(context: CoroutineContext): Observable<T> = rxObservable(context) { |
| 60 | for (t in this@asObservable) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 61 | send(t) |
| 62 | } |
Roman Elizarov | 3c3aed7 | 2017-03-09 12:31:59 +0300 | [diff] [blame] | 63 | |
| 64 | /** |
| 65 | * @suppress **Deprecated**: Renamed to [asCompletable] |
| 66 | */ |
| 67 | @Deprecated(message = "Renamed to `asCompletable`", |
| 68 | replaceWith = ReplaceWith("asCompletable(context)")) |
| 69 | public fun Job.toCompletable(context: CoroutineContext): Completable = asCompletable(context) |
| 70 | |
| 71 | /** |
| 72 | * @suppress **Deprecated**: Renamed to [asSingle] |
| 73 | */ |
| 74 | @Deprecated(message = "Renamed to `asSingle`", |
| 75 | replaceWith = ReplaceWith("asSingle(context)")) |
| 76 | public fun <T> Deferred<T>.toSingle(context: CoroutineContext): Single<T> = asSingle(context) |
| 77 | |
| 78 | /** |
| 79 | * @suppress **Deprecated**: Renamed to [asObservable] |
| 80 | */ |
| 81 | @Deprecated(message = "Renamed to `asObservable`", |
| 82 | replaceWith = ReplaceWith("asObservable(context)")) |
| 83 | public fun <T> ReceiveChannel<T>.toObservable(context: CoroutineContext): Observable<T> = asObservable(context) |