blob: 6e63cdb0cf90e483eb663c9a67c9f6c377fbae5b [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.rx2
18
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030019import io.reactivex.*
20import io.reactivex.functions.*
Roman Elizarovc0d559b2017-09-28 14:27:05 +030021import kotlinx.coroutines.experimental.*
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030022import kotlin.coroutines.experimental.*
Roman Elizarov331750b2017-02-15 17:59:17 +030023
24/**
25 * Creates cold [single][Single] that will run a given [block] in a coroutine.
Roman Elizarovc0d559b2017-09-28 14:27:05 +030026 * Every time the returned observable is subscribed, it starts a new coroutine.
Roman Elizarov331750b2017-02-15 17:59:17 +030027 * Coroutine returns a single value. Unsubscribing cancels running coroutine.
28 *
29 * | **Coroutine action** | **Signal to subscriber**
30 * | ------------------------------------- | ------------------------
31 * | Returns a value | `onSuccess`
32 * | Failure with exception or unsubscribe | `onError`
Roman Elizarovc0d559b2017-09-28 14:27:05 +030033 *
34 * The [context] for the new coroutine can be explicitly specified.
35 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
Roman Elizarovc7d10a42018-03-13 18:28:42 +030036 * The [coroutineContext] of the parent coroutine may be used,
Roman Elizarovc0d559b2017-09-28 14:27:05 +030037 * 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 +030038 * The parent job may be also explicitly specified using [parent] parameter.
39 *
Roman Elizarovc0d559b2017-09-28 14:27:05 +030040 * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
41 *
42 * @param context context of the coroutine. The default value is [DefaultDispatcher].
Roman Elizarove8f694e2017-11-28 10:12:00 +030043 * @param parent explicitly specifies the parent job, overrides job from the [context] (if any).
Roman Elizarovc0d559b2017-09-28 14:27:05 +030044 * @param block the coroutine code.
Roman Elizarov331750b2017-02-15 17:59:17 +030045 */
Roman Elizarove8f694e2017-11-28 10:12:00 +030046public fun <T> rxSingle(
47 context: CoroutineContext = DefaultDispatcher,
48 parent: Job? = null,
49 block: suspend CoroutineScope.() -> T
50): Single<T> = Single.create { subscriber ->
51 val newContext = newCoroutineContext(context, parent)
52 val coroutine = RxSingleCoroutine(newContext, subscriber)
Roman Elizarove8f694e2017-11-28 10:12:00 +030053 subscriber.setCancellable(coroutine)
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030054 coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
Roman Elizarove8f694e2017-11-28 10:12:00 +030055}
56
57/** @suppress **Deprecated**: Binary compatibility */
58@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
Roman Elizarovc0d559b2017-09-28 14:27:05 +030059@JvmOverloads // for binary compatibility with older code compiled before context had a default
Roman Elizarov331750b2017-02-15 17:59:17 +030060public fun <T> rxSingle(
Roman Elizarovc0d559b2017-09-28 14:27:05 +030061 context: CoroutineContext = DefaultDispatcher,
Roman Elizarov331750b2017-02-15 17:59:17 +030062 block: suspend CoroutineScope.() -> T
Roman Elizarove8f694e2017-11-28 10:12:00 +030063): Single<T> =
64 rxSingle(context, block = block)
Roman Elizarov331750b2017-02-15 17:59:17 +030065
66private class RxSingleCoroutine<T>(
Roman Elizarov2b12d582017-06-22 20:12:19 +030067 parentContext: CoroutineContext,
Roman Elizarov331750b2017-02-15 17:59:17 +030068 private val subscriber: SingleEmitter<T>
Roman Elizarov2b12d582017-06-22 20:12:19 +030069) : AbstractCoroutine<T>(parentContext, true), Cancellable {
Roman Elizarov6640b2b2018-01-17 19:08:55 +030070 override fun onCompleted(value: T) {
71 if (!subscriber.isDisposed) subscriber.onSuccess(value)
72 }
73
74 override fun onCompletedExceptionally(exception: Throwable) {
75 if (!subscriber.isDisposed) subscriber.onError(exception)
Roman Elizarov331750b2017-02-15 17:59:17 +030076 }
77
78 // Cancellable impl
79 override fun cancel() { cancel(cause = null) }
80}