MCPcopy
hub / github.com/ReactTooltip/react-tooltip / debounce

Function debounce

src/utils/debounce.ts:8–56  ·  view source on GitHub ↗
(
  func: (...args: A) => void,
  wait?: number,
  immediate?: boolean,
)

Source from the content-addressed store, hash-verified

6 * @param { boolean } immediate Param to define if the function will be executed immediately
7 */
8const debounce = <T, A extends any[]>(
9 func: (...args: A) => void,
10 wait?: number,
11 immediate?: boolean,
12) => {
13 let timeout: NodeJS.Timeout | null = null
14 let currentFunc = func
15
16 const debounced = function debounced(this: T, ...args: A): void {
17 const later = () => {
18 timeout = null
19 if (!immediate) {
20 currentFunc.apply(this, args)
21 }
22 }
23
24 if (immediate && !timeout) {
25 /**
26 * there's no need to clear the timeout
27 * since we expect it to resolve and set `timeout = null`
28 */
29 currentFunc.apply(this, args)
30 timeout = setTimeout(later, wait)
31 }
32
33 if (!immediate) {
34 if (timeout) {
35 clearTimeout(timeout)
36 }
37 timeout = setTimeout(later, wait)
38 }
39 }
40
41 debounced.cancel = () => {
42 /* c8 ignore start */
43 if (!timeout) {
44 return
45 }
46 /* c8 ignore end */
47 clearTimeout(timeout)
48 timeout = null
49 }
50
51 debounced.setCallback = (newFunc: (...args: A) => void) => {
52 currentFunc = newFunc
53 }
54
55 return debounced
56}
57
58export default debounce

Callers 2

useTooltipEventsFunction · 0.90
utils.spec.jsFile · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…