this repo has no description
1/* eslint-disable import/prefer-default-export */
2
3/**
4 * @name debounce
5 * @description
6 * Creates a debounced function that delays invoking func until
7 * after delayMs milliseconds have elapsed since the last time the
8 * debounced function was invoked.
9 *
10 * @param delayMs - delay in milliseconds
11 * @param immediate - Specify invoking on the leading edge of the timeout
12 * (Defaults to trailing)
13 *
14 *(f: F): (...args: Parameters<F>) => void
15 */
16export function debounce<F extends (...args: any[]) => any>(
17 fn: F,
18 delayMs: number,
19 immediate = false,
20): (...args: Parameters<F>) => void {
21 let timerId;
22
23 return function debounced(...args) {
24 const shouldCallNow = immediate && !timerId;
25 clearTimeout(timerId);
26
27 if (shouldCallNow) {
28 fn.apply(this, args);
29 }
30
31 timerId = setTimeout(() => {
32 timerId = null;
33 if (!immediate) {
34 fn.apply(this, args);
35 }
36 }, delayMs);
37 };
38}
39
40export const DEFAULT_MOUSE_OVER_DELAY = 300;