Skip to content

Instantly share code, notes, and snippets.

@imsarvesh
Last active January 10, 2018 13:53
Show Gist options
  • Save imsarvesh/4f7d0ff02142b848c2cff14782f0cc42 to your computer and use it in GitHub Desktop.
Save imsarvesh/4f7d0ff02142b848c2cff14782f0cc42 to your computer and use it in GitHub Desktop.
Start and Stop the Observable counter
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