Skip to content

Instantly share code, notes, and snippets.

@allenevans
Last active November 13, 2016 12:22
Show Gist options
  • Save allenevans/276d0ccada06fcf5d9ae8679fc635fd0 to your computer and use it in GitHub Desktop.
Save allenevans/276d0ccada06fcf5d9ae8679fc635fd0 to your computer and use it in GitHub Desktop.
This countdown timer takes a boolean observable input stream and counts down to zero. Pushing a true / false through the input stream will reset the count.
const countdown: Rx.Observable = (
inputStream: Rx.Observable<boolean>,
duration: number,
interval: number = 1000
): Rx.Observable<number> => {
return inputStream
.switchMap((enabled) => {
return enabled ? Rx.Observable.timer(0, interval)
.takeUntil(Rx.Observable.timer(duration + interval)) :
Rx.Observable.never();
})
.map((value) => duration - value * interval);
}
@allenevans
Copy link
Author

allenevans commented Nov 13, 2016

Check out the demo here: https://jsbin.com/sibacav/edit?js,output

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment