All files / sn-client-js/src Retrier.ts

100% Statements 43/43
92.59% Branches 25/27
100% Functions 14/14
100% Lines 43/43
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         12x     1x     1x           8x     1x       1x           7x     2x                                                                           1x   7x             7x       7x 7x       8x 8x                     5x 1x   4x 4x                   8x 1x     7x 7x 7x   7x   7x 1x 1x       7x 11x 11x 1x   11x 11x     5x 3x 1x     2x 1x     5x    
/**
 * @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
     */
    onTry?: () => void;
    /**
     * Optional callback, triggered on success
     */
    onSuccess?: () => void;
    /**
     * Optional callback, triggered on fail (timeout or too many retries)
     */
    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(() => {
            Eif (!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();
            !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;
    }
}