backoff-rxjs
Version:
A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff)
21 lines (20 loc) • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryBackoff = void 0;
const rxjs_1 = require("rxjs");
const utils_1 = require("../utils");
function retryBackoff(config) {
const { initialInterval, maxRetries = Infinity, maxInterval = Infinity, shouldRetry = () => true, resetOnSuccess = true, backoffDelay = utils_1.exponentialBackoffDelay, } = typeof config === 'number' ? { initialInterval: config } : config;
return (source) => (0, rxjs_1.defer)(() => {
let index = 0;
return source.pipe((0, rxjs_1.retryWhen)(errors => errors.pipe((0, rxjs_1.concatMap)(error => {
const attempt = index++;
return (0, rxjs_1.iif)(() => attempt < maxRetries && shouldRetry(error), (0, rxjs_1.timer)((0, utils_1.getDelay)(backoffDelay(attempt, initialInterval), maxInterval)), (0, rxjs_1.throwError)(error));
}))), (0, rxjs_1.tap)(() => {
if (resetOnSuccess) {
index = 0;
}
}));
});
}
exports.retryBackoff = retryBackoff;