The Subscription
class in RxJS has an add()
method that let's you add subscriptions. All added subscriptions will be unsubscribed when you call unsubscribe()
on the root subscription.
This lets you clean up multiple subscriptions with one command.
// Create a new subscription
const subscription = new Subscription();
// Subscribe to some observables
// and add them to the subscription
subscription.add(one$.subscribe({ /* ... */ }));
subscription.add(two$.subscribe({ /* ... */ }));
subscription.add(three$.subscribe({ /* ... */ }));
// During clean up
// This unsubscribes the subscription of one$, two$ and three$
subscription.unsubscribe();