blob: e83be120c11948d09aec3043b6242c01a9e993bd [file] [log] [blame]
Konrad Kamińskid6bb1482017-04-07 09:26:40 +02001/*
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.*
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020023
24/**
25 * Creates cold [maybe][Maybe] 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.
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020027 * Coroutine returns a single, possibly null value. Unsubscribing cancels running coroutine.
28 *
29 * | **Coroutine action** | **Signal to subscriber**
30 * | ------------------------------------- | ------------------------
31 * | Returns a non-null value | `onSuccess`
32 * | Returns a null | `onComplete`
33 * | Failure with exception or unsubscribe | `onError`
Roman Elizarovc0d559b2017-09-28 14:27:05 +030034 *
35 * The [context] for the new coroutine can be explicitly specified.
36 * See [CoroutineDispatcher] for the standard context implementations that are provided by `kotlinx.coroutines`.
Roman Elizarovc7d10a42018-03-13 18:28:42 +030037 * The [coroutineContext] of the parent coroutine may be used,
Roman Elizarovc0d559b2017-09-28 14:27:05 +030038 * 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 +030039 * The parent job may be also explicitly specified using [parent] parameter.
40 *
Roman Elizarovc0d559b2017-09-28 14:27:05 +030041 * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [DefaultDispatcher] is used.
42 *
43 * @param context context of the coroutine. The default value is [DefaultDispatcher].
Roman Elizarove8f694e2017-11-28 10:12:00 +030044 * @param parent explicitly specifies the parent job, overrides job from the [context] (if any).
Roman Elizarovc0d559b2017-09-28 14:27:05 +030045 * @param block the coroutine code.
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020046 */
Roman Elizarove8f694e2017-11-28 10:12:00 +030047public fun <T> rxMaybe(
48 context: CoroutineContext = DefaultDispatcher,
49 parent: Job? = null,
50 block: suspend CoroutineScope.() -> T?
51): Maybe<T> = Maybe.create { subscriber ->
52 val newContext = newCoroutineContext(context, parent)
53 val coroutine = RxMaybeCoroutine(newContext, subscriber)
Roman Elizarove8f694e2017-11-28 10:12:00 +030054 subscriber.setCancellable(coroutine)
Roman Elizarov2adf8bc2018-01-24 20:09:57 +030055 coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
Roman Elizarove8f694e2017-11-28 10:12:00 +030056}
57
58/** @suppress **Deprecated**: Binary compatibility */
59@Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN)
Roman Elizarovc0d559b2017-09-28 14:27:05 +030060@JvmOverloads // for binary compatibility with older code compiled before context had a default
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020061public fun <T> rxMaybe(
Roman Elizarovc0d559b2017-09-28 14:27:05 +030062 context: CoroutineContext = DefaultDispatcher,
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020063 block: suspend CoroutineScope.() -> T?
Roman Elizarove8f694e2017-11-28 10:12:00 +030064): Maybe<T> =
65 rxMaybe(context, block = block)
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020066
67private class RxMaybeCoroutine<T>(
Roman Elizarov2b12d582017-06-22 20:12:19 +030068 parentContext: CoroutineContext,
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020069 private val subscriber: MaybeEmitter<T>
Roman Elizarov2b12d582017-06-22 20:12:19 +030070) : AbstractCoroutine<T>(parentContext, true), Cancellable {
Roman Elizarov6640b2b2018-01-17 19:08:55 +030071 override fun onCompleted(value: T) {
72 if (!subscriber.isDisposed) {
73 if (value == null) subscriber.onComplete() else subscriber.onSuccess(value)
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020074 }
75 }
76
Roman Elizarov6640b2b2018-01-17 19:08:55 +030077 override fun onCompletedExceptionally(exception: Throwable) {
78 if (!subscriber.isDisposed) subscriber.onError(exception)
79 }
80
Konrad Kamińskid6bb1482017-04-07 09:26:40 +020081 // Cancellable impl
82 override fun cancel() { cancel(cause = null) }
83}