From ebd99349c442370163459a9643752f5093072fd9 Mon Sep 17 00:00:00 2001 From: Masoud Salehi Date: Thu, 29 Aug 2024 15:57:42 +0330 Subject: [PATCH] Fix @retry docs and refactor the code (#167) * docs(retry): Fix documentation on count of invocation * refactor(retry): Improve readability * docs(retry): Fix examples * refactor(retry): Introduce DEFAULT_DELAY variable --- index.html | 39 ++++++++++++++++++++++++--------------- src/retry/retryfy.ts | 21 ++++++++++++--------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/index.html b/index.html index 884305a..5cea830 100644 --- a/index.html +++ b/index.html @@ -1640,7 +1640,7 @@
Description:

- Retries execution of the decorated method. The method will be invoked extra x + 1 (where x is the retries + Retries execution of the decorated method. The method will be invoked x + 1 times (where x is the retries values) in the worst case (when all invocation failed). You can provide you own delay time (or delay times array) between the invocations - see details below. Note that the default delay between retries is 1000ms. @@ -1660,10 +1660,10 @@ delaysArray?: number[]; retries?: number; delay: number; - onRetry?: OnRetry; + onRetry?: OnRetry | string; } - export type OnRetry = (error: Error, retriesCount: number) => void | string; + export type OnRetry = (error: Error, retriesCount: number) => void;

@@ -1680,7 +1680,7 @@ constructor(private readonly dataProvider: DataProvider) { } @retry(3) - getData: number): DataDto { + getData(): DataDto { return this.dataProvider.getData(); } } @@ -1689,10 +1689,10 @@
Function Example 1:
-  import { retrify } from 'utils-decorators';
+  import { retryfy } from 'utils-decorators';
   import { getData } from './data-provider';
 
-  const debouncedFoo = retrify(getData, 3);
+ const retriedGetData = retryfy(getData, 3);
@@ -1708,7 +1708,7 @@ constructor(private readonly dataProvider: DataProvider) { } @retry([1000, 2000, 3000]) - getData: number): DataDto { + getData(): DataDto { return this.dataProvider.getData(); } } @@ -1717,10 +1717,10 @@
Function Example 2 (with delays array):
-  import { retrify } from 'utils-decorators';
+  import { retryfy } from 'utils-decorators';
   import { getData } from './data-provider';
 
-  const debouncedFoo = retrify(getData, [1000, 2000, 3000]);
+ const retriedGetData = retryfy(getData, [1000, 2000, 3000]);
@@ -1738,14 +1738,23 @@ @retry({ retries: 3, delay: 1500, - onError: (e, retriesCount) => console.log(e, retriesCount), + onRetry: (e, retriesCount) => console.log(e, retriesCount), + }) + getData(): DataDto { + return this.dataProvider.getData(); + } + + @retry({ + retries: 3, + delay: 1500, + onRetry: 'onRetry', }) - getData: number): DataDto { + getData2(): DataDto { return this.dataProvider.getData(); } onRetry(e: Error, retriesCount: number) { - console.log(e, retriesCount) + console.log(e, retriesCount); } } @@ -1753,13 +1762,13 @@
Function Example 3 (with configuration object):
-  import { retrify } from 'utils-decorators';
+  import { retryfy } from 'utils-decorators';
   import { getData } from './data-provider';
 
-  const debouncedFoo = retrify(getData, {
+  const retriedGetData = retryfy(getData, {
     retries: 3,
     delay: 1500,
-    onError: (e, retriesCount) => console.log(e, retriesCount),
+    onRetry: (e, retriesCount) => console.log(e, retriesCount),
   });
diff --git a/src/retry/retryfy.ts b/src/retry/retryfy.ts index a488d49..bbd6c3c 100644 --- a/src/retry/retryfy.ts +++ b/src/retry/retryfy.ts @@ -2,27 +2,29 @@ import { AsyncMethod } from '../common/model/common.model'; import { sleep } from '../common/utils/utils'; import { OnRetry, RetryInput, RetryInputConfig } from './retry.model'; +const DEFAULT_DELAY = 1000; + function getRetriesArray(input: RetryInput): number[] { if (Array.isArray(input)) { return input; } if (!Number.isNaN(input as number) && Number.isInteger(input as number)) { - return Array(input as number).fill(1).map(() => 1000); + return Array(input as number).fill(DEFAULT_DELAY); } if (typeof input === 'object') { - const config = input as RetryInputConfig; + const { retries, delaysArray, delay } = input; - if (config.retries && config.delaysArray) { + if (retries && delaysArray) { throw new Error('You can not provide both retries and delaysArray'); } - if (config.delaysArray) { - return config.delaysArray; + if (delaysArray) { + return delaysArray; } - return Array(input.retries).fill(1).map(() => input.delay ?? 1000); + return Array(retries).fill(delay ?? DEFAULT_DELAY); } throw new Error('invalid input'); @@ -30,11 +32,12 @@ function getRetriesArray(input: RetryInput): number[] { function getOnRetry(input: RetryInput, context: any): OnRetry { if (typeof input === 'object') { - if (typeof (input as RetryInputConfig).onRetry === 'string') { - return context[(input as RetryInputConfig).onRetry as string].bind(context); + const { onRetry } = (input as RetryInputConfig); + if (typeof onRetry === 'string') { + return context[onRetry].bind(context); } - return (input as RetryInputConfig).onRetry as OnRetry; + return onRetry; } return undefined;