Last active
November 13, 2016 12:22
-
-
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.
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
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out the demo here: https://jsbin.com/sibacav/edit?js,output