/** * A callback function type that can accept any number of parameters and return any value. * @callback TCallback * @param {...unknown} cbParams - Parameters to pass to the callback */ export type TCallback = (...cbParams: unknown[]) => void; /** * Options for controlling the invocation behavior of ScheduledCallback * @typedef {Object} TInvokeOptions * @property {number} [minSec=1] - Minimum seconds between successive invocations (for manual triggers) * @property {number} [maxSec=10] - Maximum seconds between automatic invocations (for timer-based calls) */ export interface TInvokeOptions { minSec?: number; maxSec?: number; } /** * Class for managing scheduled and on-demand callback invocations with rate limiting. * * Scheduled callbacks offer the ability to invoke a method on regular intervals * as well as on-demand. Multiple on-demand invocations within a short period are * avoided, and the next scheduled call is postponed to take the previous calls * into account to ensure enough time has passed between successive calls. * * @example * // Basic usage with automatic invocations every 10 seconds * const scb = new ScheduledCallback((...args) => console.log('Called with:', args)); * scb.invoke(); // Starts the timer * * @example * // Manual invocation with rate limiting * const scb = new ScheduledCallback(() => console.log('Data saved')); * // Will only execute if at least 1 second has passed since last call * setInterval(() => scb.invoke({ minSec: 1 }), 500); // Will skip many calls * * @example * // Mixed automatic and manual invocations * const scb = new ScheduledCallback(updateUI); * scb.invoke({ maxSec: 5 }); // Starts 5-second interval * button.addEventListener('click', () => { * // Manual refresh, but respects the 1-second minimum * scb.invoke({ minSec: 1, maxSec: 5 }); * }); */ export declare class ScheduledCallback { timer: NodeJS.Timeout | number | null; cb: TCallback; lastTimeInvoked: number; nextInvokeTime: number; constructor(cb: TCallback); /** * Invokes the callback either immediately (if conditions permit) or schedules it. * Manages rate limiting between successive calls and coordinates with timer-based invocations. * * @param {TInvokeOptions} [options={}] - Invocation options * @param {number} [options.minSec=1] - Minimum seconds between manual invocations * @param {number} [options.maxSec=10] - Maximum seconds between automatic invocations * @param {...unknown} cbParams - Parameters to pass to the callback * @returns {boolean} - True if the callback was invoked, false if it was skipped due to rate limiting * * @example * const scb = new ScheduledCallback(console.log); * // First call starts the timer with 10 second intervals * scb.invoke({ maxSec: 10 }, 'Initial call'); * // Subsequent calls will be rate-limited * scb.invoke({ minSec: 2 }, 'Manual call'); */ invoke({ minSec, maxSec }?: TInvokeOptions, ...cbParams: unknown[]): boolean; /** * Cancels any pending timer-based callback invocation. * Does not affect manual invocations. * * @example * const scb = new ScheduledCallback(doCleanup); * scb.invoke({ maxSec: 60 }); // Starts timer * // Later... * scb.cancelTimer(); // Stops automatic invocations */ cancelTimer(): void; } //# sourceMappingURL=scheduled-callbacks.d.ts.map