Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package kotlinx.coroutines.experimental.rx2 |
| 18 | |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 19 | import io.reactivex.* |
| 20 | import io.reactivex.functions.* |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 21 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 22 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Creates cold [single][Single] that will run a given [block] in a coroutine. |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 26 | * Every time the returned observable is subscribed, it starts a new coroutine. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 27 | * 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 Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 33 | * |
| 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 Elizarov | c7d10a4 | 2018-03-13 18:28:42 +0300 | [diff] [blame] | 36 | * The [coroutineContext] of the parent coroutine may be used, |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 37 | * in which case the [Job] of the resulting coroutine is a child of the job of the parent coroutine. |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 38 | * The parent job may be also explicitly specified using [parent] parameter. |
| 39 | * |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 40 | * 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 Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 43 | * @param parent explicitly specifies the parent job, overrides job from the [context] (if any). |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 44 | * @param block the coroutine code. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 45 | */ |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 46 | public 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 Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 53 | subscriber.setCancellable(coroutine) |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 54 | coroutine.start(CoroutineStart.DEFAULT, coroutine, block) |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /** @suppress **Deprecated**: Binary compatibility */ |
| 58 | @Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN) |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 59 | @JvmOverloads // for binary compatibility with older code compiled before context had a default |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 60 | public fun <T> rxSingle( |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 61 | context: CoroutineContext = DefaultDispatcher, |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 62 | block: suspend CoroutineScope.() -> T |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 63 | ): Single<T> = |
| 64 | rxSingle(context, block = block) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 65 | |
| 66 | private class RxSingleCoroutine<T>( |
Roman Elizarov | 2b12d58 | 2017-06-22 20:12:19 +0300 | [diff] [blame] | 67 | parentContext: CoroutineContext, |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 68 | private val subscriber: SingleEmitter<T> |
Roman Elizarov | 2b12d58 | 2017-06-22 20:12:19 +0300 | [diff] [blame] | 69 | ) : AbstractCoroutine<T>(parentContext, true), Cancellable { |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 70 | 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 Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // Cancellable impl |
| 79 | override fun cancel() { cancel(cause = null) } |
| 80 | } |