Skip to content

Instantly share code, notes, and snippets.

@tw3
Created March 28, 2019 23:09
Show Gist options
  • Save tw3/27532b16055f56c127769a113a910ab8 to your computer and use it in GitHub Desktop.
Save tw3/27532b16055f56c127769a113a910ab8 to your computer and use it in GitHub Desktop.
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