UNPKG

@storm-stack/utilities

Version:

This package includes various base utility class and various functions to assist in the development process.

36 lines (35 loc) 719 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.debounce = debounce; function debounce(func, debounceMs, { signal } = {}) { let timeoutId = null; const debounced = function (...args) { if (timeoutId !== null) { clearTimeout(timeoutId); } if (signal?.aborted) { return; } timeoutId = setTimeout(() => { func(...args); timeoutId = null; }, debounceMs); }; const onAbort = () => { debounced.cancel(); }; debounced.cancel = () => { if (timeoutId !== null) { clearTimeout(timeoutId); timeoutId = null; } }; signal?.addEventListener("abort", onAbort, { once: true }); return debounced; }