Skip to content

Commit

Permalink
refactor(retry): Improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
masoud-msk committed Aug 28, 2024
1 parent ba1de99 commit 1101a3c
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/retry/retryfy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,34 @@ function getRetriesArray(input: RetryInput): number[] {
}

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(1000);
}

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 ?? 1000);
}

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 1101a3c

Please sign in to comment.