The ability to reply to discussions is limited to PRO members. Want to join in the discussion? Click here to subscribe now.

Stopping a shared observable execution

Stopping a shared observable execution

2:39
ConnectableObservable has the connect() method to conveniently dictate the start of the shared execution of the source Observable. However, we need a mechanism to dictate the stop of the shared execution, otherwise a leak happens. This lesson will teach you how to do that, and it's all about Subscriptions.
Watch this lesson now
Avatar
egghead.io

ConnectableObservable has the connect() method to conveniently dictate the start of the shared execution of the source Observable. However, we need a mechanism to dictate the stop of the shared execution, otherwise a leak happens. This lesson will teach you how to do that, and it's all about Subscriptions.

Avatar
Tomasz Kula

Does calling unsubscribe on connectableObservable subscription clear subscriptions for subA and subB?

Asumming:
- sub = connectableObservable subscription to internal Subject
- subA = obsA subscription to connectableObservable
- subB = obsB subscription to connectableObservable

What should correct usubscribe code look like?

A

setTimeout(() => {
  subA.unsubscribe();
  subB.unsubscribe();
  sub.unsubscribe();
}, 5000)

B

setTimeout(() => {
  sub.unsubscribe();
}, 5000)
Avatar
Andre

Hi Tomasz. Unsubscribing from sub will not clear subscriptions A and B, because those are attached to the underlying subject. The subscription sub is attached to the source observable. So you would have to write:

setTimeout(() => {
  subA.unsubscribe();
  subB.unsubscribe();
  sub.unsubscribe();
}, 5000)

That said, usually we don't explicitly call connect(), so this use case is actually rather rare. As we see later on in the course, there are better ways of using multicast so that subscriptions are handled more automatically.

In reply to Tomasz Kula
HEY, QUICK QUESTION!
Joel's Head
Why are we asking?