Skip to content
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

Add SerialSubscription #183

Closed
sir-boformer opened this issue Aug 24, 2018 · 4 comments
Closed

Add SerialSubscription #183

sir-boformer opened this issue Aug 24, 2018 · 4 comments

Comments

@sir-boformer
Copy link

sir-boformer commented Aug 24, 2018

CompositeSubscription

Basically a bag for multiple subscription (with add and remove methods) that can be unsubscribed at once: http://reactivex.io/RxJava/javadoc/io/reactivex/disposables/CompositeDisposable.html

final s = new CompositeSubscription();
s.add(firstStream.listen(...));
s.add(secondStream.listen(...));
//...
s.cancel(); // cancels all

It's very useful when you listen to multiple streams and want to dispose them all at once (e.g. when a widget in Flutter is disposed).

Danger of this one is that developers add thousands of subscriptions (e.g. for a repeatable task). In RxJava at least the "bag" is never emptied, even if the streams already emptied.

SerialSubscription

A container that keeps track of a single subscription, and you can replace the subscription with another one (set method), which cancels the previous subscription: http://reactivex.io/RxJava/javadoc/io/reactivex/disposables/SerialDisposable.html

final s = new SerialSubscription();
s.set(firstStream.listen(...));
// ...
s.set(secondStream.listen(...)); // cancel firstStream subscription
// ...
s.cancel(); // cancel tracked subscription (if any)

Basically a shorthand for:

Subscription s;

s = firstStream.listen(...);
// ...
s?.cancel();
s = secondStream.listen(...);
// ...
s?.cancel();

I love this one. It ensures that subscriptions never get lost when you replace them. And you can also add them to SerialSubscriptions.

Add subscription argument to doOnListen operator

Use case: Easily add/set a subscription without breaking the flow:

final s = new CompositeSubscription();

observable
  .doOnListen(s.add)
  .listen(...);
@sir-boformer
Copy link
Author

What do you think? If it makes sense, I may take up the task.

@brianegan
Copy link
Collaborator

brianegan commented Aug 24, 2018

Hey there :) Yah, we've had this request a couple of times. I think we've always just said "You can do it manually." E.g. CompositeSubscription could be done like so List<StreamSubscription> subs = []; subs.forEach((sub) => sub.cancel());). But maybe it's not the best answer since this question has been asked a few times now.

I'd be open to this contribution. If you take it up, could you please ensure you write tests for each class? Thanks!

Mishkun added a commit to Mishkun/rxdart that referenced this issue Oct 7, 2018
This commit add CompositeSubscription util class as described in issue ReactiveX#183
CompositeSubscription is a container for subscriptions making easy to cancel all added subscriptions at once.
This Composite implementation is ported from RxJava's implementation of the same concept
brianegan pushed a commit that referenced this issue Oct 16, 2018
…at once (#191)

* Add CompositeSubscription class and docs

This commit add CompositeSubscription util class as described in issue #183
CompositeSubscription is a container for subscriptions making easy to cancel all added subscriptions at once.
This Composite implementation is ported from RxJava's implementation of the same concept

* Add test for CompositeSubscription

This test checks three properties of composite:
 - it should cancel every added subscription on clear and dispose
 - if removed from composite, subscription won't cancel on clear and dispose
 - it should throw an error instead of adding subscriprtion if it was disposed

* Add CompositeSubscription file to export

* Format the PR with the `dartfmt` tool

* Replace /** */ style docs with /// to adhere dart guidelines

* Specify explicit <dynamic> type annotations

* Fix type annotations in tests

* Register CompositeSubscription in test runner

CompositeSubscription tests weren't running on the CI because they weren't included in the test suite

* Make composite cancel subscription on remove
This is more compliant to RxJava CompositeSubscription behaviour

* Remove Java-like fluent interface

In Dart, method cascades can make auto-fluent interfaces for free,
So methods like `add` can now return the subscription added, not the composite
This makes API more versatile and adaptable for more usecases
@brianegan
Copy link
Collaborator

brianegan commented Oct 16, 2018

CompositeSubscription now added. Changing the title. Thanks @Mishkun :)

@brianegan brianegan changed the title Add CompositeSubscription and SerialSubscription Add SerialSubscription Oct 16, 2018
@shtse8
Copy link

shtse8 commented Dec 9, 2022

how about SerialSubscription?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants