backoff-rxjs
Version:
A collection of helpful RxJS operators to deal with backoff strategies (like exponential backoff)
25 lines (24 loc) • 1.46 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.retryBackoff = void 0;
var rxjs_1 = require("rxjs");
var utils_1 = require("../utils");
function retryBackoff(config) {
var _a = typeof config === 'number' ? { initialInterval: config } : config, initialInterval = _a.initialInterval, _b = _a.maxRetries, maxRetries = _b === void 0 ? Infinity : _b, _c = _a.maxInterval, maxInterval = _c === void 0 ? Infinity : _c, _d = _a.shouldRetry, shouldRetry = _d === void 0 ? function () { return true; } : _d, _e = _a.resetOnSuccess, resetOnSuccess = _e === void 0 ? false : _e, _f = _a.backoffDelay, backoffDelay = _f === void 0 ? utils_1.exponentialBackoffDelay : _f;
return function (source) {
return (0, rxjs_1.defer)(function () {
var index = 0;
return source.pipe((0, rxjs_1.retryWhen)(function (errors) {
return errors.pipe((0, rxjs_1.concatMap)(function (error) {
var attempt = index++;
return (0, rxjs_1.iif)(function () { return 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)(function () {
if (resetOnSuccess) {
index = 0;
}
}));
});
};
}
exports.retryBackoff = retryBackoff;