All files / molecule/dataCounter/src useMouseHold.js

50% Statements 11/22
25% Branches 2/8
57.14% Functions 4/7
52.63% Lines 10/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x 48x 48x   48x 30x     48x                   48x 30x       30x           48x                  
/* eslint-disable */
import {useRef, useEffect} from 'react'
 
const useMouseHold = (onClickHandler, {interval, delay, disabled}) => {
  const delayedIntervalRef = useRef(null)
  const intervalRef = useRef(null)
 
  useEffect(() => {
    return () => stopHold()
  }, [disabled])
 
  const startHold = event => {
    if (delayedIntervalRef.current) return
    delayedIntervalRef.current = setTimeout(() => {
      if (intervalRef.current) return
      intervalRef.current = setInterval(() => {
        onClickHandler(event)
      }, interval)
    }, delay)
  }
 
  const stopHold = event => {
    Iif (delayedIntervalRef.current) {
      clearTimeout(delayedIntervalRef.current)
      delayedIntervalRef.current = null
    }
    Iif (intervalRef.current) {
      clearInterval(intervalRef.current)
      intervalRef.current = null
    }
  }
 
  return {
    onClick: onClickHandler,
    onMouseDown: startHold,
    onMouseUp: stopHold,
    onMouseLeave: stopHold
  }
}
 
export default useMouseHold