.kotlinx.kotlinx-coroutines-rx1.0.26.1-eap13.source-code.RxSingle.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlinx-coroutines-rx1 Show documentation
Show all versions of kotlinx-coroutines-rx1 Show documentation
Coroutines support libraries for Kotlin
The newest version!
/*
* Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:UseExperimental(ExperimentalTypeInference::class)
package kotlinx.coroutines.rx1
import kotlinx.coroutines.*
import rx.*
import kotlin.coroutines.*
import kotlin.experimental.*
/**
* Creates cold [Single] that runs a given [block] in a coroutine.
* Every time the returned single is subscribed, it starts a new coroutine.
* Coroutine returns a single value. Unsubscribing cancels running coroutine.
*
* | **Coroutine action** | **Signal to subscriber**
* | ------------------------------------- | ------------------------
* | Returns a value | `onSuccess`
* | Failure with exception or unsubscribe | `onError`
*
* Coroutine context is inherited from a [CoroutineScope], additional context elements can be specified with [context] argument.
* If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
* The parent job is inherited from a [CoroutineScope] as well, but it can also be overridden
* with corresponding [coroutineContext] element.
*
* @param context context of the coroutine.
* @param block the coroutine code.
*/
@BuilderInference
public fun CoroutineScope.rxSingle(
context: CoroutineContext = EmptyCoroutineContext,
block: suspend CoroutineScope.() -> T
): Single = Single.create { subscriber ->
val newContext = newCoroutineContext(context)
val coroutine = RxSingleCoroutine(newContext, subscriber)
subscriber.add(coroutine)
coroutine.start(CoroutineStart.DEFAULT, coroutine, block)
}
/**
* Creates cold [Single] that runs a given [block] in a coroutine.
* @suppress **Deprecated** Use [CoroutineScope.rxSingle] instead.
*/
@Deprecated(
message = "Standalone coroutine builders are deprecated, use extensions on CoroutineScope instead",
replaceWith = ReplaceWith("GlobalScope.rxSingle(context, block)",
imports = ["kotlinx.coroutines.GlobalScope", "kotlinx.coroutines.rx1.rxSingle"])
)
public fun rxSingle(
context: CoroutineContext = Dispatchers.Default,
parent: Job? = null,
block: suspend CoroutineScope.() -> T
): Single = GlobalScope.rxSingle(context + (parent ?: EmptyCoroutineContext), block)
private class RxSingleCoroutine(
parentContext: CoroutineContext,
private val subscriber: SingleSubscriber
) : AbstractCoroutine(parentContext, true), Subscription {
override fun onCompleted(value: T) {
subscriber.onSuccess(value)
}
override fun onCompletedExceptionally(exception: Throwable) {
subscriber.onError(exception)
}
// Subscription impl
override fun isUnsubscribed(): Boolean = isCompleted
override fun unsubscribe() { cancel() }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy