-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easy Unsubscription #277
Comments
If this is exactly the type of behavior #254 is meant to provide, please close this in favor of it. |
In your latest code example, if you cancel the job returned by launch, it would make the So your code does return a kind of "disposable". It is returned by Aggregating the cancellation can easily be made with cancellation via explicit job. For example: val compositeDisposable = Job()
launch(parent = compositeDisposable) {
channel.consumeEach { println(it) }
}
compositeDisposable.cancel() // This cancel all child jobs. |
This is not correct. cancelling the result of You can easily convince yourself if you print something when cancel is called: fun main(args: Array<String>) {
// writeable stream in the model layer
val broadcastChannel = BroadcastChannel<Int>(10)
// readable stream
val sub = broadcastChannel.openSubscription()
// use a delegate to print something when cancel is called on the subscription
val source: SubscriptionReceiveChannel<Int> = object : SubscriptionReceiveChannel<Int> by sub {
override fun cancel(cause: Throwable?): Boolean {
println("cancelling subscribtion with cause: $cause")
return sub.cancel(cause)
}
}
val job = launch {
source // closing this prevents downstream operators from emitting
.map { it + 1 }
.map { it + 2 }
// calling `.cancel(null)` on the `ReceiveChannel` returned by the previous
// `.map` operator above DOES close the upstream channel. This easily
// avoid memory leaks
.cancel(null)
}
runBlocking { job.join() }
} You'll see that this code does print "cancelling subscribtion with cause: null" because of the |
@jcornaz is absolutely right that channel operators like Note, that current implementation of I'm closing this particular issue anyway. |
I'm confused, because there still is a need for multi-job cancellation. I would recommend adding a Is there a reason |
Pass a parent Job, cancel the parent. Closeable is JVM-specific. |
One of the best parts of Rx is that unsubscriptions automatically transfer upstream. I can create a complex stream from multiple data sources, transform them, and subscribe to them. Then, I can add them to a
CompositeDisposable
- which can automatically get cleared during the appropriate lifecycle event, likeonDestroyed()
(I'm an android developer). It would be really helpful if co-routines could implement this behavior.The biggest roadblock to this is that because
Channel<T>
subscriptions are hot, unsubscriptions do not automatically transfer upstream. Would #254 provide this? More specifically:The text was updated successfully, but these errors were encountered: