• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright 2016-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3  */
4 
5 @file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")
6 
7 package kotlinx.coroutines.rx2
8 
9 import io.reactivex.*
10 import kotlinx.coroutines.*
11 import kotlin.coroutines.*
12 import kotlin.internal.*
13 
14 /**
15  * Creates cold [maybe][Maybe] that will run a given [block] in a coroutine.
16  * Every time the returned observable is subscribed, it starts a new coroutine.
17  * Coroutine returns a single, possibly null value. Unsubscribing cancels running coroutine.
18  *
19  * | **Coroutine action**                  | **Signal to subscriber**
20  * | ------------------------------------- | ------------------------
21  * | Returns a non-null value              | `onSuccess`
22  * | Returns a null                        | `onComplete`
23  * | Failure with exception or unsubscribe | `onError`
24  *
25  * Coroutine context can be specified with [context] argument.
26  * If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
27  * Method throws [IllegalArgumentException] if provided [context] contains a [Job] instance.
28  */
29 public fun <T> rxMaybe(
30     context: CoroutineContext = EmptyCoroutineContext,
31     block: suspend CoroutineScope.() -> T?
32 ): Maybe<T> {
33     require(context[Job] === null) { "Maybe context cannot contain job in it." +
34             "Its lifecycle should be managed via Disposable handle. Had $context" }
35     return rxMaybeInternal(GlobalScope, context, block)
36 }
37 
38 @Deprecated(
39     message = "CoroutineScope.rxMaybe is deprecated in favour of top-level rxMaybe",
40     level = DeprecationLevel.WARNING,
41     replaceWith = ReplaceWith("rxMaybe(context, block)")
42 ) // Since 1.3.0, will be error in 1.3.1 and hidden in 1.4.0
43 @LowPriorityInOverloadResolution
rxMaybenull44 public fun <T> CoroutineScope.rxMaybe(
45     context: CoroutineContext = EmptyCoroutineContext,
46     block: suspend CoroutineScope.() -> T?
47 ): Maybe<T> = rxMaybeInternal(this, context, block)
48 
49 private fun <T> rxMaybeInternal(
50     scope: CoroutineScope, // support for legacy rxMaybe in scope
51     context: CoroutineContext,
52     block: suspend CoroutineScope.() -> T?
53 ): Maybe<T> = Maybe.create { subscriber ->
54     val newContext = scope.newCoroutineContext(context)
55     val coroutine = RxMaybeCoroutine(newContext, subscriber)
56     subscriber.setCancellable(RxCancellable(coroutine))
57     coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
58 }
59 
60 private class RxMaybeCoroutine<T>(
61     parentContext: CoroutineContext,
62     private val subscriber: MaybeEmitter<T>
63 ) : AbstractCoroutine<T>(parentContext, true) {
onCompletednull64     override fun onCompleted(value: T) {
65         if (!subscriber.isDisposed) {
66             try {
67                 if (value == null) subscriber.onComplete() else subscriber.onSuccess(value)
68             } catch(e: Throwable) {
69                 handleCoroutineException(context, e)
70             }
71         }
72     }
73 
onCancellednull74     override fun onCancelled(cause: Throwable, handled: Boolean) {
75         if (!subscriber.isDisposed) {
76             try {
77                 subscriber.onError(cause)
78             } catch (e: Throwable) {
79                 handleCoroutineException(context, e)
80             }
81         } else if (!handled) {
82             handleCoroutineException(context, cause)
83         }
84     }
85 }
86