Member-only story

RxJS: Closed Subjects

Nicholas Jamieson
3 min readFeb 6, 2018

--

Photo by Tim Mossholder on Unsplash

This article looks at the unsubscribe method of Subject — and its derived classes — as it has some surprising behaviour.

Subscriptions

If you look at the signature for Observable.prototype.subscribe, you’ll see that it returns a Subscription. And if you’ve used observables, you will be familiar with calling the subscription’s unsubscribe method. However, a subscription contains more than just the unsubscribe method.

In particular, the Subscription class implements the ISubscription interface:

Where AnonymousSubscription is the same interface, but without the read-onlyclosed property.

The closed property indicates whether or not the subscription has been unsubscribed — either manually or automatically (if the observable completes or errors).

Subscribers and unsubscription

Interestingly, what’s actually returned from a call to subscribe is an instance of the Subscriber class — which extends the Subscription class.

--

--

Nicholas Jamieson
Nicholas Jamieson

Written by Nicholas Jamieson

RxJS core team member; front-end developer; mentor; speaker; open-source contributor

Responses (2)