Created
March 18, 2017 15:36
-
-
Save qwtel/7eac93e888ac9ddf8581f1f3338bc24c to your computer and use it in GitHub Desktop.
Solid retry logic with rxjs5
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
// 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