Member-only story
RxJS: Composing Subscriptions
RxJS code involves making subscriptions to observables. Lots of subscriptions.
If each subscription is assigned to its own variable or property, the situation can be difficult to manage.
Fortunately, there are techniques — such as using takeUntil
or takeWhile
— that make dealing with subscriptions much easier. If you’re not familiar with the techniques, you should read Don’t Unsubscribe.
However, there are situations in which you still need to deal with subscriptions. For example, it’s sometimes necessary to manage subscriptions when writing operators.
Let’s look at subscriptions and at how subscription composition can be used to simplify some user-land operators.
What is a subscription?
The Subscription
class and its related types look like this:
A Subscription
instance is what’s returned from a call to subscribe
and, most of the time, it’s only the subscription’s unsubscribe
method that’s called. However, the Subscription
class also has add
and remove
methods.