Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 1 | /* |
Roman Elizarov | 1f74a2d | 2018-06-29 19:19:45 +0300 | [diff] [blame^] | 2 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 3 | */ |
| 4 | |
| 5 | package kotlinx.coroutines.experimental.rx2 |
| 6 | |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 7 | import io.reactivex.* |
| 8 | import io.reactivex.functions.* |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 9 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 10 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * Creates cold [Completable] that runs a given [block] in a coroutine. |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 14 | * Every time the returned completable is subscribed, it starts a new coroutine. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 15 | * Unsubscribing cancels running coroutine. |
| 16 | * |
| 17 | * | **Coroutine action** | **Signal to subscriber** |
| 18 | * | ------------------------------------- | ------------------------ |
| 19 | * | Completes successfully | `onCompleted` |
| 20 | * | Failure with exception or unsubscribe | `onError` |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 21 | * |
| 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 Elizarov | c7d10a4 | 2018-03-13 18:28:42 +0300 | [diff] [blame] | 24 | * The [coroutineContext] of the parent coroutine may be used, |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 25 | * 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] | 26 | * The parent job may be also explicitly specified using [parent] parameter. |
| 27 | * |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 28 | * 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 Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 31 | * @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] | 32 | * @param block the coroutine code. |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 33 | */ |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 34 | public 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 Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 41 | subscriber.setCancellable(coroutine) |
Roman Elizarov | 2adf8bc | 2018-01-24 20:09:57 +0300 | [diff] [blame] | 42 | coroutine.start(CoroutineStart.DEFAULT, coroutine, block) |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /** @suppress **Deprecated**: Binary compatibility */ |
| 46 | @Deprecated(message = "Binary compatibility", level = DeprecationLevel.HIDDEN) |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 47 | @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] | 48 | public fun rxCompletable( |
Roman Elizarov | c0d559b | 2017-09-28 14:27:05 +0300 | [diff] [blame] | 49 | context: CoroutineContext = DefaultDispatcher, |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 50 | block: suspend CoroutineScope.() -> Unit |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 51 | ): Completable = |
| 52 | rxCompletable(context, block = block) |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 53 | |
| 54 | private class RxCompletableCoroutine( |
Roman Elizarov | 2b12d58 | 2017-06-22 20:12:19 +0300 | [diff] [blame] | 55 | parentContext: CoroutineContext, |
Roman Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 56 | private val subscriber: CompletableEmitter |
Roman Elizarov | 2b12d58 | 2017-06-22 20:12:19 +0300 | [diff] [blame] | 57 | ) : AbstractCoroutine<Unit>(parentContext, true), Cancellable { |
Roman Elizarov | 6640b2b | 2018-01-17 19:08:55 +0300 | [diff] [blame] | 58 | 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 Elizarov | 331750b | 2017-02-15 17:59:17 +0300 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | // Cancellable impl |
| 67 | override fun cancel() { cancel(cause = null) } |
| 68 | } |