Could Subscription.add() method accept multiple tear down functions at once?
See original GitHub issueRxJS version: 5.*
Actual behavior:
I often find myself adding multiple Subscription
s into a single Subscription
so I can later unsubscribe with a single unsubscribe()
call (for example in Angular).
This means I need to do for example the following:
const subscription = new Subscription();
const sub1 = Observable...subscribe();
const sub2 = Observable...subscribe();
const sub3 = Observable...subscribe();
subscription.add(sub1);
subscription.add(sub2);
subscription.add(sub3);
...
subscription.unsubscribe();
Desired behavior:
It would be helpful to me if I could add all tear down functions into subscription
at once. For example similarly to Array.push()
function that accepts multiple items:
subscription.add(sub1, sub2, sub3);
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to perform an action after multiple subscriptions are ...
First of all you shouldn't subscribe directly in methods methodOne() and methodTwo() . You can share the returned Observable for example. – ...
Read more >Best Practices for Managing RxJS Subscriptions - This Dot Labs
Unsubscribing Manually ... One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS ...
Read more >Yet another way to handle RxJS subscriptions - Medium
I simply make us of a single parent Subscription instance and add child subscriptions to it, but you could take different approaches as...
Read more >Subscription - RxJS
A Subscription essentially just has an unsubscribe() function to release resources or cancel Observable executions. Subscriptions can also be put together, so ...
Read more >Subscribing to Multiple Observables in Angular Components
The forkJoin operator will subscribe to each Observable passed into it. Once it receives a value from all the Observables, it will emit...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I recently discovered that
sub.add(another)
doesn’t returnsub
, butanother
. This means that chaining can have adverse effects if one of the chained subscriptions ends before the root subscription does:In this case, “Interval 2” never logs to the console, as “Interval 1” effectively unsubscribes “Interval 2” when it ends due to the
first()
operator.This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.