Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Stopping a shared observable execution


    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.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    The connect observable has the connect method to conveniently dictate the start of the shared execution of this source observable. But doing this is actually rather dangerous, because we may be created a leak, because connect here essentially says when should we start the shard execution, but we don't yet have anything saying when to stop the shared execution. Let me give an example.

    This source observable is finite, it has only five values being emitted, so I'm going to remove this and now it's an infinite observable, it ticks every one second. If we add do here just to console log when do those events happen on the source, then we're just tapping into the shared execution of this source observable, we're not adding observers. Then if we remember subscribe will always return a subscription, right? So we can keep the reference to that, to both A and B, and the we can do something like after five seconds, we're going to unsubscribe from A and unsubscribe from B.

    When we run this, we're going to see the source is producing those events and the observers are getting those, and then after five seconds both of those observers unsubscribe, but the shard execution keeps on going, because it doesn't have anything to say when should it stop. Connect just started it, and it keeps on going on forever. Connect actually returns a subscription, and we can keep that like this.

    Just like a subscribe will return a subscription, a connect also returns a subscription because internally, remember, connect with subscribe to this source observable using this object. So it makes sense, it should return a subscription. Then we can get this, and we can unsubscribe from that in order to tell when to stop the shared execution. Now if we run this after five seconds we're going to see that the shared execution will stop, and then nothing happens after that.

    Just remember that with connect we are manually controlling the start of the shared execution, and then we keep a subscription in order to manually control the stop of the shared execution. All of this is in order to avoid leaks.