blob: e06868038c89759e5b316993dae28bde7a468a4e [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +03001/*
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
17package kotlinx.coroutines.experimental.rx1
18
19import kotlinx.coroutines.experimental.Deferred
20import kotlinx.coroutines.experimental.Job
21import kotlinx.coroutines.experimental.channels.ReceiveChannel
22import rx.*
23import 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 Elizarov3c3aed72017-03-09 12:31:59 +030034public fun Job.asCompletable(context: CoroutineContext): Completable = rxCompletable(context) {
35 this@asCompletable.join()
Roman Elizarov331750b2017-02-15 17:59:17 +030036}
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 Elizarov3c3aed72017-03-09 12:31:59 +030047public fun <T> Deferred<T>.asSingle(context: CoroutineContext): Single<T> = rxSingle<T>(context) {
48 this@asSingle.await()
Roman Elizarov331750b2017-02-15 17:59:17 +030049}
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 Elizarov3c3aed72017-03-09 12:31:59 +030059public fun <T> ReceiveChannel<T>.asObservable(context: CoroutineContext): Observable<T> = rxObservable(context) {
60 for (t in this@asObservable)
Roman Elizarov331750b2017-02-15 17:59:17 +030061 send(t)
62}
Roman Elizarov3c3aed72017-03-09 12:31:59 +030063
64/**
65 * @suppress **Deprecated**: Renamed to [asCompletable]
66 */
67@Deprecated(message = "Renamed to `asCompletable`",
68 replaceWith = ReplaceWith("asCompletable(context)"))
69public 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)"))
76public 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)"))
83public fun <T> ReceiveChannel<T>.toObservable(context: CoroutineContext): Observable<T> = asObservable(context)