@mr-hooks/use-retry
Version:
A hook for auto retrying asynchronous operations with a backoff strategy
76 lines (70 loc) • 3.45 kB
TypeScript
type BackoffStrategy = (retryCount: number) => number;
type MakeSimpleBackoffStrategy = (options: {
timeout: number;
maxRetries: number;
}) => BackoffStrategy;
/**
* makeSimpleBackoffStrategy will make a backoff strategy that returns the passed time out for
* the passed number of retries
* @param {number} timeout - the number of ms to be returned for each backoff
* @param {number} maxRetries - the maximum number of times to retry before abandoning the retry
*/
declare const makeSimpleBackoffStrategy: MakeSimpleBackoffStrategy;
type MakeFibonacciBackoffStrategy = (options: {
timeoutMultiplier?: number;
maxRetries: number;
}) => BackoffStrategy;
/**
* makeFibonacciBackoffStrategy will make a backoff strategy that returns a fibonacci sequence.
* The sequence will be multiplied the passed `timeoutMultiplier`, allowing you to scale the
* backoff by some number.
* This implementation only goes up to `4181`, from then on it repeats that number
* This implementation uses a precalculated list of
* fibonacci numbers for cpu efficiency at the cost of memory
* @param {number} timeoutMultiplier [timeoutMultiplier=1000] - The multiplier to the fibonacci
* sequence, defaults to `1000` resulting in a fibonacci sequence in seconds
* @param {number} maxRetries - the maximum number of times to retry before abandoning the retry
*/
declare const makeFibonacciBackoffStrategy: MakeFibonacciBackoffStrategy;
type MakeExponentialBackoffStrategy = (options: {
timeoutMultiplier?: number;
base?: number;
offset?: number;
maxRetries: number;
}) => BackoffStrategy;
/**
* makeExponentialBackoffStrategy will make a backoff strategy that performs the `base`
* to the power of the (retryCount + offset), then multiplied by the timeoutMultiplier
* until maxRetries is met.
* By default, it will produce powers of 2 in hundreds of ms starting at 400ms
* Note: Even with very small bases, this function grows very fast.
* @param {number} base [base=2] - the base of the exponent
* @param {number} offset [offset=2] - the offset added to `retryCount`
* ensuring the first value is not `1`
* @param {number} timeoutMultiplier [timeoutMultiplier=100] - The multiplier to the exponential
* sequence, defaults to `100` resulting in an exponential sequence in hundreds of ms
* @param {number} maxRetries - the maximum number of times to retry before abandoning the retry
*/
declare const makeExponentialBackoffStrategy: MakeExponentialBackoffStrategy;
declare const IDLE: unique symbol;
declare const BACKOFF: unique symbol;
declare const ABANDONED: unique symbol;
declare const SUCCEEDED: unique symbol;
declare const RUNNING: unique symbol;
type Status = typeof IDLE | typeof BACKOFF | typeof ABANDONED | typeof SUCCEEDED | typeof RUNNING;
type Retry = () => any;
type UseRetry = (options: {
isFailing: boolean;
isSucceeded: boolean;
isRunning: boolean;
retry: Retry;
backoffStrategy?: BackoffStrategy;
}) => {
retryCount: number;
status: Status;
};
/**
* useRetry acts like useState, but keeps the url in sync.
*/
declare const useRetry: UseRetry;
export { ABANDONED, BACKOFF, BackoffStrategy, IDLE, MakeExponentialBackoffStrategy, MakeFibonacciBackoffStrategy, MakeSimpleBackoffStrategy, RUNNING, Retry, SUCCEEDED, Status, UseRetry, useRetry as default, makeExponentialBackoffStrategy, makeFibonacciBackoffStrategy, makeSimpleBackoffStrategy };