std/async/debounce.ts
Hajime-san 897b86d99c
docs(async): use Array.fromAsync() in debounce() example (#4283)
* docs(async): `debounce` example replace with `Array.fromAsync`

* Update async/debounce.ts

---------

Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-02-07 16:04:31 +11:00

77 lines
2.1 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
/**
* A debounced function that will be delayed by a given `wait`
* time in milliseconds. If the method is called again before
* the timeout expires, the previous call will be aborted.
*/
export interface DebouncedFunction<T extends Array<unknown>> {
(...args: T): void;
/** Clears the debounce timeout and omits calling the debounced function. */
clear(): void;
/** Clears the debounce timeout and calls the debounced function immediately. */
flush(): void;
/** Returns a boolean whether a debounce call is pending or not. */
readonly pending: boolean;
}
/**
* 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.
*
* @example
* ```
* import { debounce } from "https://deno.land/std@$STD_VERSION/async/debounce.ts";
*
* await Array.fromAsync(
* Deno.watchFs('./'),
* debounce((event) => {
* console.log('[%s] %s', event.kind, event.paths[0]);
* }, 200),
* );
* // wait 200ms ...
* // output: Function debounced after 200ms with baz
* ```
*
* @param fn The function to debounce.
* @param wait The time in milliseconds to delay the function.
*/
// deno-lint-ignore no-explicit-any
export function debounce<T extends Array<any>>(
fn: (this: DebouncedFunction<T>, ...args: T) => void,
wait: number,
): DebouncedFunction<T> {
let timeout: number | null = null;
let flush: (() => void) | null = null;
const debounced: DebouncedFunction<T> = ((...args: T) => {
debounced.clear();
flush = () => {
debounced.clear();
fn.call(debounced, ...args);
};
timeout = setTimeout(flush, wait);
}) as DebouncedFunction<T>;
debounced.clear = () => {
if (typeof timeout === "number") {
clearTimeout(timeout);
timeout = null;
flush = null;
}
};
debounced.flush = () => {
flush?.();
};
Object.defineProperty(debounced, "pending", {
get: () => typeof timeout === "number",
});
return debounced;
}