Join egghead, unlock knowledge.

Want more egghead? It's 45% off for a limited time only!

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

Unlock All Content for 45% Off

Already subscribed? Sign In

Save 45% for a limited time.

Get access to all courses and lessons on egghead today.

Autoplay

    Handle Clicks and Intervals Together with Merge

    John LindquistJohn Lindquist

    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.

    angularAngular
    2 - 6
    ngrx-store@ngrx/store
    rxjsRxJS
    5 - 6
    Code

    Code

    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
    Transcript

    Transcript

    00:00 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.

    00:21 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.

    00:55 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.

    01:13 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.

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

    Discuss

    Discuss