blob: 7627f7c2749a505ec37519f8a878f78396950885 [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
Roman Elizarovc0d559b2017-09-28 14:27:05 +030019import kotlinx.coroutines.experimental.*
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030020import rx.*
21import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030022
23/**
24 * Creates cold [Single] that runs a given [block] in a coroutine.
Roman Elizarovc0d559b2017-09-28 14:27:05 +030025 * Every time the returned single is subscribed, it starts a new coroutine.
Roman Elizarov331750b2017-02-15 17:59:17 +030026 * Coroutine returns a single value. Unsubscribing cancels running coroutine.
27 *
28 * | **Coroutine action** | **Signal to subscriber**
29 * | ------------------------------------- | ------------------------
30 * | Returns a value | `onSuccess`
31 * | Failure with exception or unsubscribe | `onError`
Roman Elizarovc0d559b2017-09-28 14:27:05 +030032 *
33 * The [context] for the new coroutine can be explicitly specified.
34 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
Roman Elizarovc7d10a42018-03-13 18:28:42 +030035 * The [coroutineContext] of the parent coroutine may be used,
Roman Elizarovc0d559b2017-09-28 14:27:05 +030036 * in which case the [Job] of the resulting coroutine is a child of the job of the parent coroutine.
Roman Elizarove8f694e2017-11-28 10:12:00 +030037 * The parent job may be also explicitly specified using [parent] parameter.
38 *
Roman Elizarovc0d559b2017-09-28 14:27:05 +030039 * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
40 *
41 * @param context context of the coroutine. The default value is [DefaultDispatcher].
Roman Elizarove8f694e2017-11-28 10:12:00 +030042 * @param parent explicitly specifies the parent job, overrides job from the [context] (if any).
Roman Elizarovc0d559b2017-09-28 14:27:05 +030043 * @param block the coroutine code.
Roman Elizarov331750b2017-02-15 17:59:17 +030044 */
Roman Elizarove8f694e2017-11-28 10:12:00 +030045public fun <T> rxSingle(
46 context: CoroutineContext = DefaultDispatcher,
47 parent: Job? = null,
48 block: suspend CoroutineScope.() -> T
49): Single<T> = Single.create { subscriber ->
50 val newContext = newCoroutineContext(context, parent)
51 val coroutine = RxSingleCoroutine(newContext, subscriber)
Roman Elizarove8f694e2017-11-28 10:12:00 +030052 subscriber.add(coroutine)
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030053 coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
Roman Elizarove8f694e2017-11-28 10:12:00 +030054}
55
56/** @suppress **Deprecated**: Binary compatibility */
57@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
Roman Elizarovc0d559b2017-09-28 14:27:05 +030058@JvmOverloads // for binary compatibility with older code compiled before context had a default
Roman Elizarov331750b2017-02-15 17:59:17 +030059public fun <T> rxSingle(
Roman Elizarovc0d559b2017-09-28 14:27:05 +030060 context: CoroutineContext = DefaultDispatcher,
Roman Elizarov331750b2017-02-15 17:59:17 +030061 block: suspend CoroutineScope.() -> T
Roman Elizarove8f694e2017-11-28 10:12:00 +030062): Single<T> =
63 rxSingle(context, block = block)
Roman Elizarov331750b2017-02-15 17:59:17 +030064
65private class RxSingleCoroutine<T>(
Roman Elizarov2b12d582017-06-22 20:12:19 +030066 parentContext: CoroutineContext,
Roman Elizarov331750b2017-02-15 17:59:17 +030067 private val subscriber: SingleSubscriber<T>
Roman Elizarov2b12d582017-06-22 20:12:19 +030068) : AbstractCoroutine<T>(parentContext, true), Subscription {
Roman Elizarov6640b2b2018-01-17 19:08:55 +030069 override fun onCompleted(value: T) {
70 subscriber.onSuccess(value)
71 }
72
73 override fun onCompletedExceptionally(exception: Throwable) {
74 subscriber.onError(exception)
Roman Elizarov331750b2017-02-15 17:59:17 +030075 }
76
77 // Subscription impl
78 override fun isUnsubscribed(): Boolean = isCompleted
79 override fun unsubscribe() { cancel() }
80}