In my Angular 2 app i have many observables and subscriptions. Ofcourse I should unsubscribe when I leave the page, but i\'m trying to find out if it\'s possible to get the
The following utility function may help...
function subscriberCount(sourceObservable: Observable, description: string) {
let counter = 0;
return Observable.create((subscriber: Subscriber) => {
const subscription = sourceObservable.subscribe(subscriber);
counter++;
console.log(`${description} subscriptions: ${counter}`);
return () => {
subscription.unsubscribe();
counter--;
console.log(`${description} subscriptions: ${counter}`);
}
});
}
Use it like this:
const timer$ = subscriberCount(Observable.timer(1000), 'Timer');
And the console will log whenever the subscriber count changes