blob: 8bbb54e59d18d5db2de82a1a3e5640066fa85367 [file] [log] [blame]
Roman Elizarov331750b2017-02-15 17:59:17 +03001/*
Roman Elizarov1f74a2d2018-06-29 19:19:45 +03002 * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
Roman Elizarov331750b2017-02-15 17:59:17 +03003 */
4
5package kotlinx.coroutines.experimental.rx1
6
Roman Elizarovc0d559b2017-09-28 14:27:05 +03007import kotlinx.coroutines.experimental.*
Roman Elizarov2adf8bc2018-01-24 20:09:57 +03008import rx.*
9import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030010
11/**
12 * Creates cold [Single] that runs a given [block] in a coroutine.
Roman Elizarovc0d559b2017-09-28 14:27:05 +030013 * Every time the returned single is subscribed, it starts a new coroutine.
Roman Elizarov331750b2017-02-15 17:59:17 +030014 * Coroutine returns a single value. Unsubscribing cancels running coroutine.
15 *
16 * | **Coroutine action** | **Signal to subscriber**
17 * | ------------------------------------- | ------------------------
18 * | Returns a value | `onSuccess`
19 * | Failure with exception or unsubscribe | `onError`
Roman Elizarovc0d559b2017-09-28 14:27:05 +030020 *
21 * The [context] for the new coroutine can be explicitly specified.
22 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
Roman Elizarovc7d10a42018-03-13 18:28:42 +030023 * The [coroutineContext] of the parent coroutine may be used,
Roman Elizarovc0d559b2017-09-28 14:27:05 +030024 * 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 +030025 * The parent job may be also explicitly specified using [parent] parameter.
26 *
Roman Elizarovc0d559b2017-09-28 14:27:05 +030027 * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
28 *
29 * @param context context of the coroutine. The default value is [DefaultDispatcher].
Roman Elizarove8f694e2017-11-28 10:12:00 +030030 * @param parent explicitly specifies the parent job, overrides job from the [context] (if any).
Roman Elizarovc0d559b2017-09-28 14:27:05 +030031 * @param block the coroutine code.
Roman Elizarov331750b2017-02-15 17:59:17 +030032 */
Roman Elizarove8f694e2017-11-28 10:12:00 +030033public fun <T> rxSingle(
34 context: CoroutineContext = DefaultDispatcher,
35 parent: Job? = null,
36 block: suspend CoroutineScope.() -> T
37): Single<T> = Single.create { subscriber ->
38 val newContext = newCoroutineContext(context, parent)
39 val coroutine = RxSingleCoroutine(newContext, subscriber)
Roman Elizarove8f694e2017-11-28 10:12:00 +030040 subscriber.add(coroutine)
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030041 coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
Roman Elizarove8f694e2017-11-28 10:12:00 +030042}
43
44/** @suppress **Deprecated**: Binary compatibility */
45@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
Roman Elizarovc0d559b2017-09-28 14:27:05 +030046@JvmOverloads // for binary compatibility with older code compiled before context had a default
Roman Elizarov331750b2017-02-15 17:59:17 +030047public fun <T> rxSingle(
Roman Elizarovc0d559b2017-09-28 14:27:05 +030048 context: CoroutineContext = DefaultDispatcher,
Roman Elizarov331750b2017-02-15 17:59:17 +030049 block: suspend CoroutineScope.() -> T
Roman Elizarove8f694e2017-11-28 10:12:00 +030050): Single<T> =
51 rxSingle(context, block = block)
Roman Elizarov331750b2017-02-15 17:59:17 +030052
53private class RxSingleCoroutine<T>(
Roman Elizarov2b12d582017-06-22 20:12:19 +030054 parentContext: CoroutineContext,
Roman Elizarov331750b2017-02-15 17:59:17 +030055 private val subscriber: SingleSubscriber<T>
Roman Elizarov2b12d582017-06-22 20:12:19 +030056) : AbstractCoroutine<T>(parentContext, true), Subscription {
Roman Elizarov6640b2b2018-01-17 19:08:55 +030057 override fun onCompleted(value: T) {
58 subscriber.onSuccess(value)
59 }
60
61 override fun onCompletedExceptionally(exception: Throwable) {
62 subscriber.onError(exception)
Roman Elizarov331750b2017-02-15 17:59:17 +030063 }
64
65 // Subscription impl
66 override fun isUnsubscribed(): Boolean = isCompleted
67 override fun unsubscribe() { cancel() }
68}