11cb0ef41Sopenharmony_ci"use strict";
21cb0ef41Sopenharmony_civar __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
31cb0ef41Sopenharmony_ci    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41cb0ef41Sopenharmony_ci    return new (P || (P = Promise))(function (resolve, reject) {
51cb0ef41Sopenharmony_ci        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
61cb0ef41Sopenharmony_ci        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
71cb0ef41Sopenharmony_ci        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
81cb0ef41Sopenharmony_ci        step((generator = generator.apply(thisArg, _arguments || [])).next());
91cb0ef41Sopenharmony_ci    });
101cb0ef41Sopenharmony_ci};
111cb0ef41Sopenharmony_civar __generator = (this && this.__generator) || function (thisArg, body) {
121cb0ef41Sopenharmony_ci    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
131cb0ef41Sopenharmony_ci    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
141cb0ef41Sopenharmony_ci    function verb(n) { return function (v) { return step([n, v]); }; }
151cb0ef41Sopenharmony_ci    function step(op) {
161cb0ef41Sopenharmony_ci        if (f) throw new TypeError("Generator is already executing.");
171cb0ef41Sopenharmony_ci        while (_) try {
181cb0ef41Sopenharmony_ci            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
191cb0ef41Sopenharmony_ci            if (y = 0, t) op = [op[0] & 2, t.value];
201cb0ef41Sopenharmony_ci            switch (op[0]) {
211cb0ef41Sopenharmony_ci                case 0: case 1: t = op; break;
221cb0ef41Sopenharmony_ci                case 4: _.label++; return { value: op[1], done: false };
231cb0ef41Sopenharmony_ci                case 5: _.label++; y = op[1]; op = [0]; continue;
241cb0ef41Sopenharmony_ci                case 7: op = _.ops.pop(); _.trys.pop(); continue;
251cb0ef41Sopenharmony_ci                default:
261cb0ef41Sopenharmony_ci                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
271cb0ef41Sopenharmony_ci                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
281cb0ef41Sopenharmony_ci                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
291cb0ef41Sopenharmony_ci                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
301cb0ef41Sopenharmony_ci                    if (t[2]) _.ops.pop();
311cb0ef41Sopenharmony_ci                    _.trys.pop(); continue;
321cb0ef41Sopenharmony_ci            }
331cb0ef41Sopenharmony_ci            op = body.call(thisArg, _);
341cb0ef41Sopenharmony_ci        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
351cb0ef41Sopenharmony_ci        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
361cb0ef41Sopenharmony_ci    }
371cb0ef41Sopenharmony_ci};
381cb0ef41Sopenharmony_ciObject.defineProperty(exports, "__esModule", { value: true });
391cb0ef41Sopenharmony_civar options_1 = require("./options");
401cb0ef41Sopenharmony_civar delay_factory_1 = require("./delay/delay.factory");
411cb0ef41Sopenharmony_cifunction backOff(request, options) {
421cb0ef41Sopenharmony_ci    if (options === void 0) { options = {}; }
431cb0ef41Sopenharmony_ci    return __awaiter(this, void 0, void 0, function () {
441cb0ef41Sopenharmony_ci        var sanitizedOptions, backOff;
451cb0ef41Sopenharmony_ci        return __generator(this, function (_a) {
461cb0ef41Sopenharmony_ci            switch (_a.label) {
471cb0ef41Sopenharmony_ci                case 0:
481cb0ef41Sopenharmony_ci                    sanitizedOptions = options_1.getSanitizedOptions(options);
491cb0ef41Sopenharmony_ci                    backOff = new BackOff(request, sanitizedOptions);
501cb0ef41Sopenharmony_ci                    return [4 /*yield*/, backOff.execute()];
511cb0ef41Sopenharmony_ci                case 1: return [2 /*return*/, _a.sent()];
521cb0ef41Sopenharmony_ci            }
531cb0ef41Sopenharmony_ci        });
541cb0ef41Sopenharmony_ci    });
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ciexports.backOff = backOff;
571cb0ef41Sopenharmony_civar BackOff = /** @class */ (function () {
581cb0ef41Sopenharmony_ci    function BackOff(request, options) {
591cb0ef41Sopenharmony_ci        this.request = request;
601cb0ef41Sopenharmony_ci        this.options = options;
611cb0ef41Sopenharmony_ci        this.attemptNumber = 0;
621cb0ef41Sopenharmony_ci    }
631cb0ef41Sopenharmony_ci    BackOff.prototype.execute = function () {
641cb0ef41Sopenharmony_ci        return __awaiter(this, void 0, void 0, function () {
651cb0ef41Sopenharmony_ci            var e_1, shouldRetry;
661cb0ef41Sopenharmony_ci            return __generator(this, function (_a) {
671cb0ef41Sopenharmony_ci                switch (_a.label) {
681cb0ef41Sopenharmony_ci                    case 0:
691cb0ef41Sopenharmony_ci                        if (!!this.attemptLimitReached) return [3 /*break*/, 7];
701cb0ef41Sopenharmony_ci                        _a.label = 1;
711cb0ef41Sopenharmony_ci                    case 1:
721cb0ef41Sopenharmony_ci                        _a.trys.push([1, 4, , 6]);
731cb0ef41Sopenharmony_ci                        return [4 /*yield*/, this.applyDelay()];
741cb0ef41Sopenharmony_ci                    case 2:
751cb0ef41Sopenharmony_ci                        _a.sent();
761cb0ef41Sopenharmony_ci                        return [4 /*yield*/, this.request()];
771cb0ef41Sopenharmony_ci                    case 3: return [2 /*return*/, _a.sent()];
781cb0ef41Sopenharmony_ci                    case 4:
791cb0ef41Sopenharmony_ci                        e_1 = _a.sent();
801cb0ef41Sopenharmony_ci                        this.attemptNumber++;
811cb0ef41Sopenharmony_ci                        return [4 /*yield*/, this.options.retry(e_1, this.attemptNumber)];
821cb0ef41Sopenharmony_ci                    case 5:
831cb0ef41Sopenharmony_ci                        shouldRetry = _a.sent();
841cb0ef41Sopenharmony_ci                        if (!shouldRetry || this.attemptLimitReached) {
851cb0ef41Sopenharmony_ci                            throw e_1;
861cb0ef41Sopenharmony_ci                        }
871cb0ef41Sopenharmony_ci                        return [3 /*break*/, 6];
881cb0ef41Sopenharmony_ci                    case 6: return [3 /*break*/, 0];
891cb0ef41Sopenharmony_ci                    case 7: throw new Error("Something went wrong.");
901cb0ef41Sopenharmony_ci                }
911cb0ef41Sopenharmony_ci            });
921cb0ef41Sopenharmony_ci        });
931cb0ef41Sopenharmony_ci    };
941cb0ef41Sopenharmony_ci    Object.defineProperty(BackOff.prototype, "attemptLimitReached", {
951cb0ef41Sopenharmony_ci        get: function () {
961cb0ef41Sopenharmony_ci            return this.attemptNumber >= this.options.numOfAttempts;
971cb0ef41Sopenharmony_ci        },
981cb0ef41Sopenharmony_ci        enumerable: true,
991cb0ef41Sopenharmony_ci        configurable: true
1001cb0ef41Sopenharmony_ci    });
1011cb0ef41Sopenharmony_ci    BackOff.prototype.applyDelay = function () {
1021cb0ef41Sopenharmony_ci        return __awaiter(this, void 0, void 0, function () {
1031cb0ef41Sopenharmony_ci            var delay;
1041cb0ef41Sopenharmony_ci            return __generator(this, function (_a) {
1051cb0ef41Sopenharmony_ci                switch (_a.label) {
1061cb0ef41Sopenharmony_ci                    case 0:
1071cb0ef41Sopenharmony_ci                        delay = delay_factory_1.DelayFactory(this.options, this.attemptNumber);
1081cb0ef41Sopenharmony_ci                        return [4 /*yield*/, delay.apply()];
1091cb0ef41Sopenharmony_ci                    case 1:
1101cb0ef41Sopenharmony_ci                        _a.sent();
1111cb0ef41Sopenharmony_ci                        return [2 /*return*/];
1121cb0ef41Sopenharmony_ci                }
1131cb0ef41Sopenharmony_ci            });
1141cb0ef41Sopenharmony_ci        });
1151cb0ef41Sopenharmony_ci    };
1161cb0ef41Sopenharmony_ci    return BackOff;
1171cb0ef41Sopenharmony_ci}());
1181cb0ef41Sopenharmony_ci//# sourceMappingURL=backoff.js.map