Skip to content

Commit

Permalink
Fix @Retry docs and refactor the code (#167)
Browse files Browse the repository at this point in the history
* docs(retry): Fix documentation on count of invocation

* refactor(retry): Improve readability

* docs(retry): Fix examples

* refactor(retry): Introduce DEFAULT_DELAY variable
  • Loading branch information
masoud-msk authored Aug 29, 2024
1 parent 4e3bc7c commit ebd9934
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
39 changes: 24 additions & 15 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1640,7 +1640,7 @@
<div class="ud-section-content">
<div class="ud-subtitle">Description:</div>
<p class="ud-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.
Expand All @@ -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;
</pre>
</div>
</div>
Expand All @@ -1680,7 +1680,7 @@
constructor(private readonly dataProvider: DataProvider) { }

@retry(3)
getData: number): DataDto {
getData(): DataDto {
return this.dataProvider.getData();
}
}</pre>
Expand All @@ -1689,10 +1689,10 @@
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 1:</div>
<pre class="ud-code">
import { retrify } from 'utils-decorators';
import { retryfy } from 'utils-decorators';
import { getData } from './data-provider';

const debouncedFoo = retrify(getData, 3);</pre>
const retriedGetData = retryfy(getData, 3);</pre>
</div>
</div>

Expand All @@ -1708,7 +1708,7 @@
constructor(private readonly dataProvider: DataProvider) { }

@retry([1000, 2000, 3000])
getData: number): DataDto {
getData(): DataDto {
return this.dataProvider.getData();
}
}</pre>
Expand All @@ -1717,10 +1717,10 @@
<div class="ud-code-part">
<div class="ud-subtitle">Function Example 2 (with delays array):</div>
<pre class="ud-code">
import { retrify } from 'utils-decorators';
import { retryfy } from 'utils-decorators';
import { getData } from './data-provider';

const debouncedFoo = retrify(getData, [1000, 2000, 3000]);</pre>
const retriedGetData = retryfy(getData, [1000, 2000, 3000]);</pre>
</div>
</div>

Expand All @@ -1738,28 +1738,37 @@
@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);
}
}</pre>
</div>

<div class="ud-code-part">
<div class="ud-subtitle">Function Example 3 (with configuration object):</div>
<pre class="ud-code">
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),
});</pre>
</div>
</div>
Expand Down
21 changes: 12 additions & 9 deletions src/retry/retryfy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,42 @@ 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');
}

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;
Expand Down

0 comments on commit ebd9934

Please sign in to comment.