UNPKG

@igorskyflyer/zep

Version:

🧠 Zep is a zero-dependency, efficient debounce module. ⏰

75 lines (74 loc) 2.57 kB
export type ZepCallback = (...args: any[]) => void; export type ZepErrorHandler = (error: unknown) => void; export type ZepEventHandler = () => void; export declare class Zep { #private; /** * Creates a new instance of Zep. * @param callback The function/callback to debounce. * @param time The time limit (in ms) for the debouncing. */ constructor(callback: ZepCallback, time?: number); /** * Returns the number of callback executions. */ get executionCount(): number; /** * Indicates whether Zep is waiting for a Timer to finish its execution, if true, Zep.run() won’t create new Timers when called. */ get isWaiting(): boolean; /** * Indicates whether a Timer is currently running the `callback` provided in the constructor. */ get isRunning(): boolean; /** * Indicates whether the execution of Zep.run() was cancelled. Execution can be cancelled by calling Zep.cancel(). */ get wasCancelled(): boolean; /** * Indicates whether the execution of Zep.run() was aborted. Execution can be aborted by calling Zep.abort(). */ get wasAborted(): boolean; /** * A handler to call when the execution of Zep.run() has been cancelled. */ onCancelled(handler: ZepEventHandler): Zep; /** * A handler to call when the execution of Zep.run() has been aborted. */ onAborted(handler: ZepEventHandler): Zep; /** * A handler to call before Zep.run(). */ onBeforeRun(handler: ZepEventHandler): Zep; /** * A handler to call after Zep.run(). */ onAfterRun(handler: ZepEventHandler): Zep; /** * A handler to call after `Zep()` has finished running, i.e. no more calls to the `Zep.run()` method have been issued in the given time-frame. */ onCompleted(handler: ZepEventHandler): Zep; /** * A handler to call when an error has occurred during execution. */ onError(handler: ZepErrorHandler): Zep; /** * Stops the execution but NOT the current running Timer - if applicable. * @see abort */ cancel(): void; /** * Aborts the execution, stops Zep completely and - if applicable - the current running Timer without waiting for it to finish its execution. * @see cancel */ abort(): void; /** * Writes Zep statistical information to the console. */ writeStats(): void; /** * Runs the callback defined in the constructor if necessary or else debounces it. */ run(...args: any[]): Zep; }