This repository has been archived on 2026-03-28. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
StasisWarden-Legacy/node_modules/es-toolkit/dist/function/throttle.mjs
T

24 lines
681 B
JavaScript

import { debounce } from './debounce.mjs';
function throttle(func, throttleMs, { signal, edges = ['leading', 'trailing'] } = {}) {
let pendingAt = null;
const debounced = debounce(func, throttleMs, { signal, edges });
const throttled = function (...args) {
if (pendingAt == null) {
pendingAt = Date.now();
}
else {
if (Date.now() - pendingAt >= throttleMs) {
pendingAt = Date.now();
debounced.cancel();
}
}
debounced(...args);
};
throttled.cancel = debounced.cancel;
throttled.flush = debounced.flush;
return throttled;
}
export { throttle };