dash-core
Version:
A foundational toolkit of types, collections, services, and architectural patterns designed to accelerate application development.
60 lines (59 loc) • 2.69 kB
TypeScript
import { TimeSpan, ServiceLogger } from 'dash-core';
type pollerOptions = {
/** Initial delay before starting the first poll cycle. */
initialDelay: TimeSpan;
/** Maximum delay between polling cycles. */
maxDelay: TimeSpan;
/** Factor by which the delay will be multiplied on each failure. */
factor: number;
/** Time period after which the delay will reset to the initial delay if no errors occur. */
resetPeriod: TimeSpan;
/** The step used for linear delay decrease. */
linearStep: TimeSpan;
/** Optional logger for logging events during polling. */
logger?: ServiceLogger;
};
/**
* Handles polling with adaptive intervals, adjusting delays based on success or failure.
*/
export declare class AdaptivePoller {
private currentTimeout?;
private currentDelay;
private lastErrorTime?;
private polling;
private operation?;
private onError?;
private onFail?;
private readonly options;
private readonly defaultOptions;
/**
* Returns whether the polling operation is currently active.
* @returns {boolean} True if polling is ongoing, false otherwise.
*/
get isPolling(): boolean;
/**
* Creates an instance of the AdaptivePoller class.
* @param {pollerOptions} options - Configuration options for the polling mechanism.
* @param {TimeSpan} options.initialDelay - The initial delay before the first polling attempt.
* @param {TimeSpan} options.maxDelay - The maximum delay between polling attempts.
* @param {number} options.factor - The factor by which the delay will be multiplied after each failure.
* @param {TimeSpan} options.resetPeriod - The period after which the delay will reset to the initial value if no errors occur.
* @param {TimeSpan} options.linearStep - The step used to decrease the delay linearly on success.
* @param {ServiceLogger} [options.logger] - Optional logger to log events during polling.
*/
constructor(options?: Partial<pollerOptions>);
/**
* Starts the polling process, which continues until explicitly stopped.
* @param {() => Promise<void>} operation - The operation to perform during each poll cycle.
* @param {(error: Error) => void} onError - Optional callback to handle errors during polling.
* @param {(error: Error) => void} onFail - Optional callback to handle failure events (when the interval exceeds maxDelay).
*/
start(operation: () => Promise<void>, onError?: (error: Error) => void, onFail?: (error: Error) => void): void;
/**
* Stops the polling process.
*/
stop(): void;
private runPoll;
private scheduleNextPoll;
}
export {};