Created
March 28, 2019 23:09
-
-
Save tw3/27532b16055f56c127769a113a910ab8 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 { rxObserver } = require('api/v0.3'); | |
const { of, timer } = require('rxjs'); | |
const { delay, expand, takeWhile, map } = require('rxjs/operators'); | |
const sessionDuration = 9; | |
const warningAmount = 7; | |
const expireEpoch = Date.now() + sessionDuration | |
const warningEpoch = expireEpoch - warningAmount; | |
function getTimeLeft() { | |
return expireEpoch - Date.now(); | |
} | |
function getDelay(timeLeft) { | |
return 1; | |
} | |
timer(warningEpoch) | |
.pipe( | |
expand(() => { | |
const timeLeft = getTimeLeft(); | |
return (timeLeft > 0) ? | |
of(undefined).pipe(delay(getDelay(timeLeft))) : | |
empty(); | |
}), | |
map(() => getTimeLeft()), | |
takeWhile(timeLeft => timeLeft > 0), | |
map(timeLeft => { | |
return timeLeft; | |
}) | |
) | |
.subscribe(rxObserver()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment