/** * IdmpGlobalKey type - Represents the key used to identify and store promises globally * Can be a string, number, symbol, or falsy value (false, null, undefined) */ type IdmpGlobalKey = string | number | symbol | false | null | undefined; /** * IdmpPromise type - Function that returns a Promise of generic type T */ type IdmpPromise = () => Promise; /** * Configuration options for IDMP (Intelligent Deduplication of Multiple Promises) */ interface IdmpOptions { /** * Maximum number of retry attempts for failed promises * @default 30 */ maxRetry?: number; /** * Minimum delay between retry attempts in milliseconds * @default 50 */ minRetryDelay?: number; /** * Maximum delay between retry attempts in milliseconds * @default 5000 */ maxRetryDelay?: number; /** * Maximum cache age in milliseconds * After this period, the cached promise result will be cleared * @default 3000 * @max 604800000 (7 days) */ maxAge?: number; signal?: AbortSignal; /** * Function executed before each retry attempt * @param err - The error object that caused the retry * @param extra - Additional context information * @param extra.globalKey - The key identifying this promise in the global store * @param extra.retryCount - Current retry attempt number */ onBeforeRetry?: (err: any, extra: { globalKey: IdmpGlobalKey; retryCount: number; }) => void; } /** * Internal status enum for promise states */ declare const enum Status { /** Promise hasn't been initiated yet */ UNSENT = 0, /** Promise is in progress */ OPENING = 1, /** Promise was aborted (reserved for future use) */ ABORTED = 2, /** Promise was rejected */ REJECTED = 3, /** Promise was resolved successfully */ RESOLVED = 4 } /** * Normalizes options, applying defaults as needed * @param options - User-provided options * @returns Normalized options object */ declare const getOptions: (options?: IdmpOptions) => { maxRetry: number; maxAge: number; minRetryDelay: number; maxRetryDelay: number; onBeforeRetry: (err: any, extra: { globalKey: IdmpGlobalKey; retryCount: number; }) => void; f: boolean; signal: AbortSignal | undefined; }; /** * Main IDMP function - Intelligent Deduplication of Multiple Promises * * Ensures that multiple calls to the same asynchronous operation (identified by globalKey) * will reuse the same promise, avoiding duplicate network requests or operations. * Includes automatic retry logic, caching, and request deduplication. * * @param globalKey - Unique identifier for this promise operation * @param promiseFunc - Function that returns the promise to execute * @param options - Configuration options * @returns Promise resolving to the result of promiseFunc * * @example * ```typescript * // Basic usage * const data = await idmp('user-123', () => fetchUserData(123)); * * // With options * const data = await idmp('user-123', () => fetchUserData(123), { * maxRetry: 5, * maxAge: 60000, // 1 minute cache * onBeforeRetry: (err, {retryCount}) => console.log(`Retry #${retryCount}`) * }); * ``` */ declare const idmp: { (globalKey: IdmpGlobalKey, promiseFunc: IdmpPromise, options?: IdmpOptions): Promise; /** * Clear the cached result for a specific key */ flush: (globalKey: IdmpGlobalKey) => void; /** * Clear all cached results */ flushAll: () => void; _s: Record | undefined]> | undefined; }; /** * Type definition for the idmp function including its methods */ type Idmp = typeof idmp; export default idmp; export { getOptions, type Idmp, type IdmpGlobalKey, type IdmpOptions, type IdmpPromise, };