UNPKG

@carbon/react

Version:

React components for the Carbon Design System

71 lines (68 loc) 1.85 kB
/** * Copyright IBM Corp. 2016, 2023 * * This source code is licensed under the Apache-2.0 license found in the * LICENSE file in the root directory of this source tree. */ function debounce(func, debounceMs, { signal, edges } = {}) { let pendingThis = undefined; let pendingArgs = null; const leading = edges != null && edges.includes('leading'); const trailing = edges == null || edges.includes('trailing'); const invoke = () => { if (pendingArgs !== null) { func.apply(pendingThis, pendingArgs); pendingThis = undefined; pendingArgs = null; } }; const onTimerEnd = () => { if (trailing) { invoke(); } cancel(); }; let timeoutId = null; const schedule = () => { if (timeoutId != null) { clearTimeout(timeoutId); } timeoutId = setTimeout(() => { timeoutId = null; onTimerEnd(); }, debounceMs); }; const cancelTimer = () => { if (timeoutId !== null) { clearTimeout(timeoutId); timeoutId = null; } }; const cancel = () => { cancelTimer(); pendingThis = undefined; pendingArgs = null; }; const flush = () => { cancelTimer(); invoke(); }; const debounced = function (...args) { if (signal?.aborted) { return; } pendingThis = this; pendingArgs = args; const isFirstCall = timeoutId == null; schedule(); if (leading && isFirstCall) { invoke(); } }; debounced.schedule = schedule; debounced.cancel = cancel; debounced.flush = flush; signal?.addEventListener('abort', cancel, { once: true }); return debounced; } export { debounce };