UNPKG

kentico-cloud-delivery

Version:

Official Kentico Cloud Delivery SDK

44 lines 1.97 kB
"use strict"; exports.__esModule = true; var rxjs_1 = require("rxjs"); var operators_1 = require("rxjs/operators"); var DeliveryRetryStrategy = /** @class */ (function () { function DeliveryRetryStrategy() { var _this = this; this.strategy = function (options) { return function (attempts) { return attempts.pipe(operators_1.mergeMap(function (error, i) { var retryAttempt = i + 1; // if maximum number of retries have been met // or response is a status code we don't wish to retry, throw error if (retryAttempt > options.maxRetryAttempts || options.excludedStatusCodes.find(function (e) { return e === error.status; })) { return rxjs_1.throwError(error); } // calculate retry time var retryTimeout = _this.getRetryTimeout(retryAttempt); // debug log attempt _this.debugLogAttempt(retryAttempt, retryTimeout); return rxjs_1.timer(retryTimeout); })); }; }; } /** * Calculates retry attempt timeout in ms * @param attempt Index of the attempt to calculate increasing delay when retrying */ DeliveryRetryStrategy.prototype.getRetryTimeout = function (attempt) { return Math.pow(2, attempt) * 100; }; /** * Logs attempt in console. * This function is also used for testing in jasmine spy * @param attempt Index of attempt */ DeliveryRetryStrategy.prototype.debugLogAttempt = function (attempt, retryTimeout) { console.warn("Attempt " + attempt + ": retrying in " + retryTimeout + "ms"); }; return DeliveryRetryStrategy; }()); exports.DeliveryRetryStrategy = DeliveryRetryStrategy; exports.deliveryRetryStrategy = new DeliveryRetryStrategy(); //# sourceMappingURL=delivery-retry-strategy.js.map