Skip to content

Instantly share code, notes, and snippets.

@johnlindquist
Created January 29, 2016 18:17
Show Gist options
  • Save johnlindquist/948b9a3eac5ec3f71ae1 to your computer and use it in GitHub Desktop.
Save johnlindquist/948b9a3eac5ec3f71ae1 to your computer and use it in GitHub Desktop.
const go = state=> Object.assign(state, {isRunning:true});
const inc = state=> Object.assign(state, {isRunning:true, tick: state.tick + 1});
const zero = state=> Object.assign(state, {isRunning:false, tick:0});
const stop = state=> Object.assign(state, {isRunning:false});
const timer = Observable.interval(1000);
const {start, pause, reset} = this;
this.timerView = start
.switchMap(() => Observable.merge(
timer.mapTo(inc),
pause.mapTo(stop),
reset.mapTo(zero)
)
.takeUntil(pause.merge(reset))
)
.scan((state, op) => {
console.log(state);
return op(state);
}, {
tick:0,
isRunning:false
})
.share();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment