All files / src Retrier.ts

100% Statements 44/44
96.3% Branches 26/27
100% Functions 14/14
100% Lines 44/44
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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                      1x   1x 7x         32x     1x     1x           26x     1x     1x           7x     2x                                                                         1x   7x             7x       7x 7x       26x 26x                     5x 1x   4x 4x                   8x 1x     7x 7x 7x   7x   7x 7x 4x       7x 29x 29x 1x   29x 29x 26x       7x 3x 1x     4x 1x     7x      
/**
 * @module Retrier
 * @preferred
 *
 * @description Module for Retrier.
 *
 */ /** */
 
/**
 * Options class for Retrier
 */
export class RetrierOptions {
 
    public static readonly RETRIES_DEFAULT = 10;
    private _retries: number = RetrierOptions.RETRIES_DEFAULT;
    /**
     * How many times should retry the operation
     */
    public get Retries(): number {
        return this._retries !== undefined ? this._retries : RetrierOptions.RETRIES_DEFAULT;
    }
    public set Retries(v: number) {
        this._retries = v;
    }
 
    public static readonly RETRY_INTERVAL_MS_DEFAULT = 10;
    private _retryIntervalMs: number;
    /**
     * The interval between tries in milliseconds
     */
    public get RetryIntervalMs(): number {
        return this._retryIntervalMs !== undefined ? this._retryIntervalMs : RetrierOptions.RETRY_INTERVAL_MS_DEFAULT;
    }
    public set RetryIntervalMs(v: number) {
        this._retryIntervalMs = v;
    }
 
    public static readonly TIMEOUT_MS_DEFAULT = 1000;
    private _timeoutMs: number;
    /**
     * The Timeout interval in milliseconds
     */
    public get TimeoutMs(): number {
        return this._timeoutMs !== undefined ? this._timeoutMs : RetrierOptions.TIMEOUT_MS_DEFAULT;
    }
    public set TimeoutMs(v: number) {
        this._timeoutMs = v;
    }
 
    /**
     * Optional callback, triggered right before each try
     */
    public OnTry?: () => void;
    /**
     * Optional callback, triggered on success
     */
    public OnSuccess?: () => void;
    /**
     * Optional callback, triggered on fail (timeout or too many retries)
     */
    public OnFail?: () => void;
}
 
/**
 * Utility class for retrying operations.
 * Usage example:
 * ```
 *          const funcToRetry: () => Promise<boolean> = async () => {
 *              let hasSucceeded = false;
 *              // ...
 *              // custom logic
 *              // ...
 *              return hasSucceeded;
 *          }
 *          const retrierSuccess = await Retrier.Create(funcToRetry)
 *              .Setup({
 *                  retries: 3,
 *                  retryIntervalMs: 1,
 *                  timeoutMs: 1000
 *              })
 *              .Run();
 * ```
 */
export class Retrier {
 
    private _isRunning: boolean = false;
 
    /**
     * Factory method for creating a Retrier
     * @param {()=>Promise<boolean>} callback The method that will be invoked on each try
     */
    public static Create(callback: () => Promise<boolean>) {
        return new Retrier(callback, new RetrierOptions());
    }
 
    private constructor(
        private _callback: () => Promise<boolean>,
        public readonly Options: RetrierOptions) {
    }
 
    private async wait(ms: number) {
        return new Promise<any>((resolve, reject) => {
            setTimeout(resolve, ms);
        });
    }
 
    /**
     * Method to override the default Retrier settings.
     * @param {Partial<RetrierOptions>} options The options to be overridden
     * @throws Error if the Retrier is running.
     * @returns the Retrier instance
     */
    public Setup(options: Partial<RetrierOptions>) {
        if (this._isRunning) {
            throw Error('Retrier already started!');
        }
        Object.assign(this.Options, options);
        return this;
    }
 
    /**
     * Public method that starts the Retrier
     * @throws Error if the Retrier is already started.
     * @returns {Promise<boolean>} A boolean value that indicates if the process has been succeeded.
     */
    public async Run(): Promise<boolean> {
 
        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 = await this._callback();
            if (!succeeded) {
                await 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;
    }
}