RxJS Lap Button (From Koutnik’s RxJS book)

In the first chapter of Randall Koutnik’s book Building Reactive Websites with RxJS he has a challenge where he asks the reader to add a lap button to the stopwatch project. This lap button essentially freezes the output while continuing to count behind the scenes. I couldn’t figure this out for a little while, and found no online resources to help me out. After some head banging I was able to figure out a solution:

I thought this was a pretty neat solution! Here’s the breakdown. lapClick$ is an Observable<boolean> which starts with false. Whenever it receives an emission, the scan will invert its current condition. That is, when the lap button is clicked once it becomes true then false etc.

Next, we use combineLatest to actually see into lapClick$ in our main function. We filter based on if the lapClick$ is true or not, if it’s not we continue with our normal logic. That’s it! I thought this was a fun solution but it took some thinking to actually arrive at the answer.