2018-01-09 11:31:41 -08:00
|
|
|
|
2018-02-27 17:06:06 -05:00
|
|
|
import { pipe, range, timer, zip } from 'rxjs';
|
|
|
|
import { ajax } from 'rxjs/ajax';
|
|
|
|
import { retryWhen, map, mergeMap } from 'rxjs/operators';
|
2018-01-09 11:31:41 -08:00
|
|
|
|
|
|
|
function backoff(maxTries, ms) {
|
|
|
|
return pipe(
|
2018-10-26 16:25:00 -04:00
|
|
|
retryWhen(attempts => zip(range(1, maxTries), attempts)
|
2018-01-09 11:31:41 -08:00
|
|
|
.pipe(
|
2018-10-26 16:25:00 -04:00
|
|
|
map(([i]) => i * i),
|
2018-01-09 11:31:41 -08:00
|
|
|
mergeMap(i => timer(i * ms))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
ajax('/api/endpoint')
|
|
|
|
.pipe(backoff(3, 250))
|
|
|
|
.subscribe(data => handleData(data));
|
|
|
|
|
|
|
|
function handleData(data) {
|
|
|
|
// ...
|
|
|
|
}
|