6

When I have multiple subscribers to one observable, for example:

const myInterval = Rx.Observable.interval(500);

const subscriptionOne = myInterval.subscribe(val => doSomething());
const subscriptionTwo = myInterval.subscribe(val => doSomething());

How can I know how many subscribers still register to the myInterval observable? I need this information for example to prevent memory leaks in case that I forgot to unsubscribe from one of them?

1 Answers1

1

When using angular 2, you should try to use the async pipe as much as possible since this will unsubscribe automatically for you when your component gets destroyed.

That being said. Your interval observable here is a cold observable. Meaning, for every subscription the producer of values will be set up. This means, a new subscription is created each time. So knowing how many subscriptions you still have to a cold observable is a question you cannot answer.

I'm afraid the only thing you can do is managing your subscriptions in a clean way and rely on angular as much as possible for this.

KwintenP
  • 4,637
  • 2
  • 22
  • 30
  • And for example, if I use in my component the valueChanges.subscribe in forms? I need to unsubscribe manually? –  Oct 20 '16 at 15:21