File size: 705 Bytes
e16348e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * utils.js: Shared helpers
 */

/**
 * Returns a debounced version of fn that fires after `delay` ms of inactivity.
 * The returned function also exposes cancel(), so a caller that's about to
 * issue an equivalent call immediately (bypassing the debounce) can drop any
 * pending trailing call instead of leaving it to fire later against a
 * position that's no longer current.
 * @param {Function} fn
 * @param {number} delay
 */
export function debounce(fn, delay) {
    let timer;
    const debounced = function (...args) {
        clearTimeout(timer);
        timer = setTimeout(() => fn.apply(this, args), delay);
    };
    debounced.cancel = () => clearTimeout(timer);
    return debounced;
}