import { useMemo, useState } from "preact/hooks";

export default function useDebounce(func: Function, delay: number) {
  const [id, setId] = useState<number|null>(null)
  return useMemo(
    (...args) => {
      if (id) {
        clearTimeout(id)
      } else {
        setId(
          window.setTimeout(() => {
            setId(null)
            func(...args)
          }, delay)
        )
      }
    }, [func]
  )
}
