/********************************************************** * Copyright © 2022 Fict Ledger * All Rights Reserved. */ import { type IRetryOptions, type TOnErrFn, type TRetryFn } from "./retriable-fn.js"; import { reset as resetSingleton, type THelp, type TTag } from "./singleton-promise.js"; /** * Creates a singleton promise with retry capabilities, combining both singleton pattern * and automatic retry logic. This ensures: * 1. Given function is retried multiple times till successfully resolved or max attempts reached. * 2. If all retries fail, throws error and the singleton is reset to allow fresh attempts on subsequent calls. * 3. If success, only one instance of the promise exists at a time (singleton behavior). * * The function works by wrapping the provided function with both retry logic (via `retryFn`) * and singleton behavior (via `singletonPromise`). This is particularly useful for managing * resources like WebSocket connections that should have a single instance but need to handle * temporary connection issues. * * @template TResult * @param {IRetryOptions} retryOptions - Retry configuration * @param {TRetryFn} fn - Function to create the promise that should be retried * @param {TTag} FnTag - Unique identifier for the singleton * @param {THelp} FnHelp - Descriptive help text * @param {TOnErrFn} onError - Logger function for error reporting * @returns {Promise} Singleton promise with retry behavior * * @example * // Creating a retriable singleton for a database connection * const getDbConnection = () => retriableSingleton( * { * delayMS: 1000, * maxRetryAttempts: 5 * }, * async () => { * const conn = await createDatabaseConnection(); * await conn.ping(); // Verify connection is alive * return conn; * }, * 'db-connection-main', * 'Main database connection singleton' * ); * * // Usage (will always return the same promise if connection succeeds) * const db1P: Promise = getDbConnection(); * const db2P: Promise = getDbConnection(); * assert(db1P == db2P); */ export declare function retriableSingleton(retryOptions: Partial, fn: TRetryFn, FnTag: TTag, FnHelp?: THelp, onError?: TOnErrFn): Promise; export { resetSingleton }; //# sourceMappingURL=singleton-retriable.d.ts.map