All files / src Retrier.js

0% Statements 0/58
0% Branches 0/36
0% Functions 0/22
0% Lines 0/53
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100                                                                                                                                                                                                       
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
class RetrierOptions {
    constructor() {
        this._retries = RetrierOptions.RETRIES_DEFAULT;
    }
    get Retries() {
        return this._retries !== undefined ? this._retries : RetrierOptions.RETRIES_DEFAULT;
    }
    set Retries(v) {
        this._retries = v;
    }
    get RetryIntervalMs() {
        return this._retryIntervalMs !== undefined ? this._retryIntervalMs : RetrierOptions.RETRY_INTERVAL_MS_DEFAULT;
    }
    set RetryIntervalMs(v) {
        this._retryIntervalMs = v;
    }
    get TimeoutMs() {
        return this._timeoutMs !== undefined ? this._timeoutMs : RetrierOptions.TIMEOUT_MS_DEFAULT;
    }
    set TimeoutMs(v) {
        this._timeoutMs = v;
    }
}
RetrierOptions.RETRIES_DEFAULT = 10;
RetrierOptions.RETRY_INTERVAL_MS_DEFAULT = 10;
RetrierOptions.TIMEOUT_MS_DEFAULT = 1000;
exports.RetrierOptions = RetrierOptions;
class Retrier {
    constructor(_callback, Options) {
        this._callback = _callback;
        this.Options = Options;
        this._isRunning = false;
    }
    static Create(callback) {
        return new Retrier(callback, new RetrierOptions());
    }
    wait(ms) {
        return __awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => {
                setTimeout(resolve, ms);
            });
        });
    }
    Setup(options) {
        if (this._isRunning) {
            throw Error('Retrier already started!');
        }
        Object.assign(this.Options, options);
        return this;
    }
    Run() {
        return __awaiter(this, void 0, void 0, function* () {
            if (this._isRunning) {
                throw Error('Retrier already started!');
            }
            let succeeded = false;
            let retries = 0;
            let timedOut = false;
            this._isRunning = true;
            setTimeout(() => {
                if (!succeeded) {
                    timedOut = true;
                }
            }, this.Options.TimeoutMs);
            while (!succeeded && !timedOut && (this.Options.Retries > retries)) {
                retries++;
                if (this.Options.OnTry) {
                    this.Options.OnTry();
                }
                succeeded = yield this._callback();
                if (!succeeded) {
                    yield this.wait(this.Options.RetryIntervalMs);
                }
            }
            if (succeeded) {
                if (!timedOut && this.Options.OnSuccess) {
                    this.Options.OnSuccess();
                }
            }
            else {
                if (this.Options.OnFail) {
                    this.Options.OnFail();
                }
            }
            return succeeded;
        });
    }
}
exports.Retrier = Retrier;
//# sourceMappingURL=Retrier.js.map