about summary refs log tree commit diff
path: root/src/lib/hooks/useDedupe.ts
blob: b6ca5abbf63f034b54389fc4a6019a7e7d685366 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react'

export const useDedupe = (timeout = 250) => {
  const canDo = React.useRef(true)

  return React.useCallback(
    (cb: () => unknown) => {
      if (canDo.current) {
        canDo.current = false
        setTimeout(() => {
          canDo.current = true
        }, timeout)
        cb()
        return true
      }
      return false
    },
    [timeout],
  )
}