@mdf.js/utils
Version:
MMS - API Core - Common utils tools
76 lines • 2.9 kB
TypeScript
/**
* Copyright 2024 Mytra Control S.L. All rights reserved.
*
* Use of this source code is governed by an MIT-style license that can be found in the LICENSE file
* or at https://opensource.org/licenses/MIT.
*/
import { Boom, Crash, Multi } from '@mdf.js/crash';
/**
* The wait time in milliseconds for retrying an operation.
*/
export declare const WAIT_TIME = 100;
/**
* Maximum wait time in milliseconds for retrying an operation.
*/
export declare const MAX_WAIT_TIME = 15000;
/**
* Represents a function that logs errors.
* @param error - The error to be logged.
*/
export type LoggerFunction = (error: Crash | Multi | Boom) => void;
/**
* Type definition for the arguments of a task function.
*/
export type TaskArguments = any[];
/**
* Represents a task that returns a promise.
* @template R The type of the result returned by the task.
*/
export type TaskAsPromise<R> = (...args: TaskArguments) => Promise<R>;
/** Represents the options for retrying an operation */
export interface RetryOptions {
/** The logger function used for logging retry attempts */
logger?: LoggerFunction;
/** The time to wait between retry attempts, in milliseconds */
waitTime?: number;
/** The maximum time to wait between retry attempts, in milliseconds */
maxWaitTime?: number;
/**
* A function that determines whether to interrupt the retry process
* Should return true to interrupt, false otherwise.
* @deprecated User `abortSignal` instead
*/
interrupt?: () => boolean;
/** The signal to be used to interrupt the retry process */
abortSignal?: AbortSignal;
/** The maximum number of retry attempts. */
attempts?: number;
/** Timeout for each try */
timeout?: number;
}
/**
* Perform the retry functionality for a promise
* @param task - promise to execute
* @param bindTo - instance to be binded to the task
* @param funcArgs - promise arguments
* @param options - control execution options
* @returns
*/
export declare const retryBind: <T, U>(task: TaskAsPromise<T>, bindTo: U, funcArgs?: TaskArguments, options?: RetryOptions) => Promise<T>;
/**
* Perform the retry functionality for a promise
* @param task - promise to execute
* @param funcArgs - promise arguments
* @param options - control execution options
* @returns
*/
export declare const retry: <T>(task: TaskAsPromise<T>, funcArgs?: TaskArguments, options?: RetryOptions) => Promise<T>;
/**
* Wraps a task with retry functionality.
* @param task - The task to be executed.
* @param funcArgs - The arguments to be passed to the task.
* @param options - The options for retry behavior.
* @returns - A function that, when called, executes the task with retry.
*/
export declare const wrapOnRetry: <T>(task: TaskAsPromise<T>, funcArgs?: TaskArguments, options?: RetryOptions) => (() => Promise<T>);
//# sourceMappingURL=retry.d.ts.map