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.


    Handle Clicks and Intervals Together with Merge

    John LindquistJohn Lindquist
    2 - 6
    5 - 6

    Observable.merge allows you take two different source streams and use either one of them to make changes to the same state of your data. This lesson shows how you can use a stream of clicks and an interval stream and use either one to update the clock.



    Become a Member to view code

    You must be a 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




    For my time machine I do want my observable interval always running. I do want it to update every second. But I also want the clicks to be able to update it. From other observable lessons we've learned that observable.merge is what we use when we want one stream or another stream to update the same thing.

    We can say observable.merge, which can take these two observables. We'll say this click, because a subject is a type of observable, and we'll say observable.interval. Let me get rid of that semicolon, either one of these can go ahead and map to a new date. Then I can take my clock and assign it to that stream of merge.

    What I'm going to do here just to show this off is change this to five seconds so that now when I refresh here, you see that I forgot to import merge. You'll forget to import these a lot, so just expect these to pop up every now and again. I'll say import/merge. Let's save again.

    I'll refresh. Now you'll see this clock. Nothing happens, nothing happens, nothing happens, but after five seconds it'll show up. Now it's just sitting there, but I can click Update to make it update. Then after five seconds it automatically updates itself.

    We have those two streams working for us. Whenever I click or whenever five seconds passes my clock will now update.