@worker-tools/deno-kv-storage
Version:
An implementation of the StorageArea (1,2,3) interface for Deno with an extensible system for supporting various database backends.
52 lines • 1.45 kB
JavaScript
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
/**
* 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.
*
* ```
* import { debounce } from "./debounce.ts";
*
* const log = debounce(
* (event: Deno.FsEvent) =>
* console.log("[%s] %s", event.kind, event.paths[0]),
* 200,
* );
*
* for await (const event of Deno.watchFs("./")) {
* log(event);
* }
* ```
*
* @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(fn, wait) {
let timeout = null;
let flush = null;
const debounced = ((...args) => {
debounced.clear();
flush = () => {
debounced.clear();
fn.call(debounced, ...args);
};
timeout = setTimeout(flush, wait);
});
debounced.clear = () => {
if (typeof timeout === "number") {
clearTimeout(timeout);
timeout = null;
flush = null;
}
};
debounced.flush = () => {
flush === null || flush === void 0 ? void 0 : flush();
};
Object.defineProperty(debounced, "pending", {
get: () => typeof timeout === "number",
});
return debounced;
}
//# sourceMappingURL=debounce.js.map