File size: 1,545 Bytes
b2d7d99
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
type DebouncedFunctionSync<T extends any[], R> = ((...args: T) => R) & {
  clear: () => void
}

/**
 * Example usage:
 * ```typescript
 *   function fetchDataSync(query: string): string {
 *     return `Results for ${query}`;
 *   }
 *
 *   const debouncedFetchDataSync = debounceSync(fetchDataSync, 300);
 *
 *   try {
 *    console.log(debouncedFetchDataSync("query 1")); // This will be ignored
 *     console.log(debouncedFetchDataSync("query 2")); // This will be ignored
 *     console.log(debouncedFetchDataSync("query 3")); // This will return "Results for query 3"
 *   } catch (error) {
 *     console.error(error);
 *   }
 * ```
 *
 * Note that the synchronous version of the debounce function will return
 * the result of the last function call or `undefined` if the function was
 * not called within the specified wait time. You should also be aware that
 * this synchronous version may cause blocking if the debounced function
 * takes a long time to execute.
 *
 * @param func
 * @param wait
 * @returns
 */
export function debounceSync<T extends any[], R>(
  func: (...args: T) => R,
  wait: number
): DebouncedFunctionSync<T, R | undefined> {
  let timeout: NodeJS.Timeout | undefined

  const debounced = (...args: T): R | undefined => {
    if (timeout) {
      clearTimeout(timeout)
    }

    let result: R | undefined
    timeout = setTimeout(() => {
      result = func(...args)
    }, wait)

    return result
  }

  debounced.clear = () => {
    if (timeout) {
      clearTimeout(timeout)
    }
  }

  return debounced
}