-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from parzh/develop
Release 1.6.x
- Loading branch information
Showing
21 changed files
with
378 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** @public */ | ||
const delays = { | ||
/* | ||
Notes to self: | ||
- beware of returning NaNs and negative numbers | ||
- number-like strings are forbidden, as in: `delays["100"]()` // ❌ | ||
- functions in this namespace must have similar argument structure | ||
*/ | ||
|
||
/** @see https://en.wikipedia.org/wiki/Exponential_backoff */ | ||
exponential(retryCount: number): number { | ||
return 2 ** retryCount * 100; | ||
}, | ||
} as const; | ||
|
||
export type DelayNamed = keyof typeof delays; | ||
export type Delay = number | DelayNamed; | ||
|
||
export function isNamed(delay: Delay): delay is DelayNamed { | ||
return delay in delays; | ||
} | ||
|
||
export default delays; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
/** @deprecated */ | ||
/** @deprecated Use `Retryer["setCount"]` from _/typings/retryer.ts_ instead */ | ||
export default interface RetryCountResetter { | ||
(newValue?: number): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,106 @@ | ||
export default interface Retryer { | ||
(): void; | ||
import type { Delay } from "../delays"; | ||
|
||
readonly count: number; | ||
export type OnMaxRetryCountExceeded = "resolve" | "reject" | (() => unknown); | ||
|
||
after(msec: number): void; | ||
export interface RetryerProps { | ||
/** | ||
* Readonly number of retries that occurred so far. | ||
* Always 1 less than the number of total attempts. | ||
* @example | ||
* console.log(retry.count); | ||
* // [attempt #1] logs 0 | ||
* // [attempt #2] logs 1 | ||
* // [attempt #3] logs 2 | ||
* // ... | ||
* | ||
* retry(); | ||
*/ | ||
readonly count: number; | ||
|
||
/** | ||
* Set value of `retry.count` for the next attempt | ||
* @example | ||
* retry.setCount(42); | ||
* | ||
* console.log(retry.count); | ||
* // [attempt #1] logs 0 | ||
* // [attempt #2] logs 42 | ||
* // [attempt #3] logs 42 | ||
* // ... | ||
* | ||
* retry(); | ||
*/ | ||
setCount(newValue: number): void; | ||
|
||
/** | ||
* Set upper limit for the value of `retry.count` | ||
* @param value Value of `retry.count` | ||
* @param onExceeded (defaults to `"reject"`) Limit exceed action | ||
* @example | ||
* // (assuming that every attempt ends with retry) | ||
* | ||
* retry.setMaxCount(0); | ||
* retry.setMaxCount(0, "reject"); | ||
* // [attempt #1] reject with "Retry limit exceeded …" | ||
* | ||
* retry.setMaxCount(1); | ||
* // [attempt #1] retry | ||
* // [attempt #2] reject with "Retry limit exceeded …" | ||
* | ||
* retry.setMaxCount(1, "resolve"); | ||
* retry.setMaxCount(1, resolve); | ||
* // [attempt #1] retry | ||
* // [attempt #2] resolve to `undefined` | ||
* | ||
* retry.setMaxCount(1, () => resolve(42)); | ||
* // [attempt #1] retry | ||
* // [attempt #2] resolve to `42` | ||
* | ||
* retry.setMaxCount(1, () => retry.setCount(0)); | ||
* // [attempt #1] retry | ||
* // [attempt #2] retry (update count) | ||
* // [attempt #3] retry | ||
* // [attempt #4] retry (update count) | ||
* // [attempt #5] retry | ||
* // ... | ||
* | ||
* retry.setMaxCount(1, () => retry.setCount(1)); | ||
* // [attempt #1] retry | ||
* // [attempt #2] retry (update count) | ||
* // [attempt #3] retry (update count) | ||
* // [attempt #4] retry (update count) | ||
* // [attempt #5] retry (update count) | ||
* // ... | ||
*/ | ||
setMaxCount(value: number, onExceeded?: OnMaxRetryCountExceeded): void; | ||
|
||
/** | ||
* Delays retry(s) by a given strategy or timer | ||
* @param delay The delay | ||
* @example | ||
* retry.after(300); | ||
* // retry after 300 milliseconds | ||
* | ||
* retry.after(1000); | ||
* // retry after 1 second | ||
* | ||
* retry.after("exponential"); | ||
* // use "Exponential backoff" retry strategy | ||
* | ||
* retry.after(0); | ||
* // retry asynchronously | ||
*/ | ||
after(delay: Delay): void; | ||
|
||
/** | ||
* Cancel the delayed retry | ||
* @example | ||
* retry.after(1000); // won't retry | ||
* retry.cancel(); | ||
*/ | ||
cancel(): void; | ||
} | ||
|
||
export default interface Retryer extends RetryerProps { | ||
(): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.