question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Could Subscription.add() method accept multiple tear down functions at once?

See original GitHub issue

RxJS version: 5.*

Actual behavior:

I often find myself adding multiple Subscriptions 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:closed
  • Created 6 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
larrifaxcommented, Nov 20, 2017

I recently discovered that sub.add(another) doesn’t return sub, but another. This means that chaining can have adverse effects if one of the chained subscriptions ends before the root subscription does:

const sub = new Rx.Subscription();

sub
  .add(Rx.Observable.interval(1000).do(x => console.log("Interval 1", x)).first().subscribe())
  .add(Rx.Observable.interval(1000).do(x => console.log("Interval 2", x)).subscribe());

setTimeout(() => sub.unsubscribe(), 5000);

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.

0reactions
lock[bot]commented, Jun 6, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found