kentico-cloud-delivery
Version:
Official Kentico Cloud Delivery SDK
42 lines • 1.81 kB
JavaScript
import { throwError, timer } from 'rxjs';
import { mergeMap } from 'rxjs/operators';
var RetryStrategy = /** @class */ (function () {
function RetryStrategy() {
var _this = this;
this.strategy = function (options) { return function (attempts) {
return attempts.pipe(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 throwError(error);
}
// calculate retry time
var retryTimeout = _this.getRetryTimeout(retryAttempt);
// debug log attempt
_this.debugLogAttempt(retryAttempt, retryTimeout);
return timer(retryTimeout);
}));
}; };
}
/**
* Calculates retry attempt timeout in ms
* @param attempt Index of the attempt to calculate increasing delay when retrying
*/
RetryStrategy.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
*/
RetryStrategy.prototype.debugLogAttempt = function (attempt, retryTimeout) {
console.warn("Attempt " + attempt + ": retrying in " + retryTimeout + "ms");
};
return RetryStrategy;
}());
export { RetryStrategy };
export var retryStrategy = new RetryStrategy();
//# sourceMappingURL=retry-strategy.js.map