Skip to content

Instantly share code, notes, and snippets.

@qwtel
Created March 18, 2017 15:36
Show Gist options
  • Save qwtel/7eac93e888ac9ddf8581f1f3338bc24c to your computer and use it in GitHub Desktop.
Save qwtel/7eac93e888ac9ddf8581f1f3338bc24c to your computer and use it in GitHub Desktop.
Solid retry logic with rxjs5
// emits a value after `init` ms, then after `init * exp` ms, then `init * exp * exp` ms, etc.
function expInterval(init, exp) {
return Observable.create((observer) => {
let n = init;
let id;
function next() {
observer.next(n);
n *= exp;
id = setTimeout(next, n);
}
id = setTimeout(next, n);
return () => {
clearTimeout(id);
};
});
}
// Sends a request and retries when the browser sends a `online` event,
// as well as after 1sec, 2sec, 4sec, 8sec, etc...
function solidRetryLogic(request) {
return Observable.ajax(request)
.retryWhen(() => Observable.merge(
Observable.fromEvent(window, 'online'),
expInterval(1000, 2),
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment