blob: 32686d4775e330f5fbb357b53ef2f0482255e798 [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.rx2
6
Roman Elizarov2adf8bc2018-01-24 20:09:57 +03007import io.reactivex.*
8import io.reactivex.functions.*
Roman Elizarovc0d559b2017-09-28 14:27:05 +03009import kotlinx.coroutines.experimental.*
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030010import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030011
12/**
13 * Creates cold [Completable] that runs a given [block] in a coroutine.
Roman Elizarovc0d559b2017-09-28 14:27:05 +030014 * Every time the returned completable is subscribed, it starts a new coroutine.
Roman Elizarov331750b2017-02-15 17:59:17 +030015 * Unsubscribing cancels running coroutine.
16 *
17 * | **Coroutine action** | **Signal to subscriber**
18 * | ------------------------------------- | ------------------------
19 * | Completes successfully | `onCompleted`
20 * | Failure with exception or unsubscribe | `onError`
Roman Elizarovc0d559b2017-09-28 14:27:05 +030021 *
22 * The [context] for the new coroutine can be explicitly specified.
23 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
Roman Elizarovc7d10a42018-03-13 18:28:42 +030024 * The [coroutineContext] of the parent coroutine may be used,
Roman Elizarovc0d559b2017-09-28 14:27:05 +030025 * 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 +030026 * The parent job may be also explicitly specified using [parent] parameter.
27 *
Roman Elizarovc0d559b2017-09-28 14:27:05 +030028 * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
29 *
30 * @param context context of the coroutine. The default value is [DefaultDispatcher].
Roman Elizarove8f694e2017-11-28 10:12:00 +030031 * @param parent explicitly specifies the parent job, overrides job from the [context] (if any).
Roman Elizarovc0d559b2017-09-28 14:27:05 +030032 * @param block the coroutine code.
Roman Elizarov331750b2017-02-15 17:59:17 +030033 */
Roman Elizarove8f694e2017-11-28 10:12:00 +030034public fun rxCompletable(
35 context: CoroutineContext = DefaultDispatcher,
36 parent: Job? = null,
37 block: suspend CoroutineScope.() -> Unit
38): Completable = Completable.create { subscriber ->
39 val newContext = newCoroutineContext(context, parent)
40 val coroutine = RxCompletableCoroutine(newContext, subscriber)
Roman Elizarove8f694e2017-11-28 10:12:00 +030041 subscriber.setCancellable(coroutine)
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030042 coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
Roman Elizarove8f694e2017-11-28 10:12:00 +030043}
44
45/** @suppress **Deprecated**: Binary compatibility */
46@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
Roman Elizarovc0d559b2017-09-28 14:27:05 +030047@JvmOverloads // for binary compatibility with older code compiled before context had a default
Roman Elizarov331750b2017-02-15 17:59:17 +030048public fun rxCompletable(
Roman Elizarovc0d559b2017-09-28 14:27:05 +030049 context: CoroutineContext = DefaultDispatcher,
Roman Elizarov331750b2017-02-15 17:59:17 +030050 block: suspend CoroutineScope.() -> Unit
Roman Elizarove8f694e2017-11-28 10:12:00 +030051): Completable =
52 rxCompletable(context, block = block)
Roman Elizarov331750b2017-02-15 17:59:17 +030053
54private class RxCompletableCoroutine(
Roman Elizarov2b12d582017-06-22 20:12:19 +030055 parentContext: CoroutineContext,
Roman Elizarov331750b2017-02-15 17:59:17 +030056 private val subscriber: CompletableEmitter
Roman Elizarov2b12d582017-06-22 20:12:19 +030057) : AbstractCoroutine<Unit>(parentContext, true), Cancellable {
Roman Elizarov6640b2b2018-01-17 19:08:55 +030058 override fun onCompleted(value: Unit) {
59 if (!subscriber.isDisposed) subscriber.onComplete()
60 }
61
62 override fun onCompletedExceptionally(exception: Throwable) {
63 if (!subscriber.isDisposed) subscriber.onError(exception)
Roman Elizarov331750b2017-02-15 17:59:17 +030064 }
65
66 // Cancellable impl
67 override fun cancel() { cancel(cause = null) }
68}