debounce
import { debounce } from "jsr:@ende/async";function
function debounce<T extends Array<any>>(fn: (this: DebouncedFunction<T>, ...args: T) => void, wait: number, options?: DebounceOptions): DebouncedFunction<T>Creates a debounced function that delays the given func
by a given wait time in milliseconds. If the method is called
again before the timeout expires, the previous call will be
aborted.
If an AbortSignal is provided via options.signal, aborting the
signal clears any pending debounce timeout, equivalent to calling
DebouncedFunction.clear.
fn: (this: DebouncedFunction<T>, ...args: T) => voidThe function to debounce.
wait: numberThe time in milliseconds to delay the function. Must be a positive integer.
options?: DebounceOptionsOptional parameters.
Returns
The debounced function.
Example: Usage
import { debounce } from "@ende/async/debounce";
const log = debounce(
(event: Ende.FsEvent) =>
console.log("[%s] %s", event.kind, event.paths[0]),
200,
);
for await (const event of Ende.watchFs("./")) {
log(event);
}
// wait 200ms ...
// output: [modify] /path/to/file
Example: With AbortSignal
import { debounce } from "@ende/async/debounce";
const controller = new AbortController();
const log = debounce(
(event: Ende.FsEvent) =>
console.log("[%s] %s", event.kind, event.paths[0]),
200,
{ signal: controller.signal },
);
for await (const event of Ende.watchFs("./")) {
log(event);
}
// Abort clears any pending debounce
controller.abort();