Last active
January 10, 2018 13:53
-
-
Save imsarvesh/4f7d0ff02142b848c2cff14782f0cc42 to your computer and use it in GitHub Desktop.
Start and Stop the Observable counter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Rx from 'rxjs' | |
const Observable = Rx.Observable; | |
// App logic | |
const startButton = document.querySelector('#start'); | |
const stopButton = document.querySelector('#stop'); | |
const start$ = Observable.fromEvent(startButton, 'click'); | |
const interval$ = Observable.interval(1000); | |
const stop$ = Observable.fromEvent(stopButton, 'click'); | |
const intervalThatStops$ = interval$.takeUntil(stop$); | |
const data = {count:0}; | |
start$ | |
.switchMapTo(intervalThatStops$) | |
.startWith(data) | |
.scan((acc)=> { | |
return {count: acc.count + 1} | |
},{count: 0}) | |
.subscribe(x => console.log(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment