ipull
Version:
The only file downloader you'll ever need. For node.js and the browser, CLI and library for fast and reliable file downloads.
31 lines • 937 B
JavaScript
export function abortableDebounce(func, { wait = 0, maxWait = wait, signal }) {
let timeoutId = null;
let lastCallTime = 0;
signal?.addEventListener("abort", () => {
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = null;
}
});
return (...args) => {
const now = Date.now();
if (timeoutId) {
clearTimeout(timeoutId);
}
if (signal?.aborted) {
return; // If the signal is aborted, do nothing
}
const timeSinceLastCall = now - lastCallTime;
if (timeSinceLastCall >= maxWait) {
func(...args);
lastCallTime = now;
}
else {
timeoutId = setTimeout(() => {
func(...args);
lastCallTime = Date.now();
}, Math.max(wait - timeSinceLastCall, 0));
}
};
}
//# sourceMappingURL=abortableDebounce.js.map