Observables often need to be stopped before they are completed. This lesson shows how to use
takeUntil to stop a running timer. Then we use the starting stream and the stopping stream together to create a simple stopwatch.
Great series, thanks!
One question: how would you handle a case when start/stop button is the same button?
There's a few different approaches depending on the exact behavior you want. Here's one implementation: https://gist.run/?id=1185b7701932b028401b
Amazing! Thank you.
For those of you looking for docs or info about switchMap and switchMapTo, it looks like these were added in version 5.0, they were previously known as flatMapLatest -> https://github.com/ReactiveX/rxjs/blob/master/MIGRATION.md
In the lessen 'Stopping a Stream with TakeUntil' I tried to add error and complete handlers to the subscribe call and on complete function was never called when the stop button is clicked. Any idea why?
takeUntil will stop the stream it's attached to, so if you want to complete the
start$ stream, you would need to move the
start$ .switchMapTo(interval$) .takeUntil(stop$) .subscribe( x=> console.log(x), err => console.log(err), ()=> console.log('complete') );
Just be aware you'll no longer be able to click the start button, because the
start$ is completed.
Thanks a lot. It works. Reading rxjs documentation it is not obvious what is the "complete" behaviour of each operator, especially when multiple streams are combined. Any suggestions?
Yep, when I started this video, my idea was to simply chain a takeUntil(stop$) on top of what we already had, something like this:
start$ .switchMapTo(interval$) .takeUntil(stop$) .subscribe(x => console.log(x));
So I didn't understand when you did something different. It was when I tried it myself that I realized that the code above will take
start$ until stop$, so it only worked once. On this case it's really just the
interval$ that we want to takeUntil(stop$).
Really enjoying this @John!
Hi Thanks for these series , but I couldn't continue watching it and here is why: Almost all of the series which you are the teacher , starts from somewhere unlcear and ends in somewhere unclear.
I disagree completely. I bought the subscription precisely because I love John's unique style of no-bullshit explanation of the essence of the topic using only one example. I've never seen anything better. I'm here to learn, and to learn fast, not to listen to teacher's greetings and introductions. If I were John I would even encourage other teachers to do the same, that's what unique about egghead compared to other courses and that's what brought a lot of people here.