{"version":3,"file":"use-interval.cjs","names":[],"sources":["../../src/use-interval/use-interval.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from 'react';\n\nexport interface UseIntervalOptions {\n  /** If set, the interval will start automatically when the component is mounted, `false` by default */\n  autoInvoke?: boolean;\n}\n\nexport interface UseIntervalReturnValue {\n  /** Starts the interval */\n  start: () => void;\n\n  /** Stops the interval */\n  stop: () => void;\n\n  /** Toggles the interval */\n  toggle: () => void;\n\n  /** Indicates if the interval is active */\n  active: boolean;\n}\n\nexport function useInterval(\n  fn: () => void,\n  interval: number,\n  { autoInvoke = false }: UseIntervalOptions = {}\n): UseIntervalReturnValue {\n  const [active, setActive] = useState(false);\n  const intervalRef = useRef<number | null>(null);\n  const fnRef = useRef<() => void>(null);\n  const intervalValueRef = useRef(interval);\n  intervalValueRef.current = interval;\n\n  const start = useCallback(() => {\n    setActive((old) => {\n      if (!old && !intervalRef.current) {\n        intervalRef.current = window.setInterval(fnRef.current!, intervalValueRef.current);\n      }\n      return true;\n    });\n  }, []);\n\n  const stop = useCallback(() => {\n    setActive(false);\n    if (intervalRef.current) {\n      window.clearInterval(intervalRef.current);\n    }\n    intervalRef.current = null;\n  }, []);\n\n  const toggle = useCallback(() => {\n    setActive((current) => {\n      if (current) {\n        if (intervalRef.current) {\n          window.clearInterval(intervalRef.current);\n        }\n        intervalRef.current = null;\n        return false;\n      }\n      if (!intervalRef.current) {\n        intervalRef.current = window.setInterval(fnRef.current!, intervalValueRef.current);\n      }\n      return true;\n    });\n  }, []);\n\n  useEffect(() => {\n    fnRef.current = fn;\n    active && start();\n    return stop;\n  }, [fn, active, interval]);\n\n  useEffect(() => {\n    if (autoInvoke) {\n      start();\n    }\n  }, []);\n\n  return { start, stop, toggle, active };\n}\n\nexport namespace useInterval {\n  export type Options = UseIntervalOptions;\n  export type ReturnValue = UseIntervalReturnValue;\n}\n"],"mappings":";;;AAqBA,SAAgB,YACd,IACA,UACA,EAAE,aAAa,UAA8B,CAAC,GACtB;CACxB,MAAM,CAAC,QAAQ,cAAA,GAAA,MAAA,UAAsB,KAAK;CAC1C,MAAM,eAAA,GAAA,MAAA,QAAoC,IAAI;CAC9C,MAAM,SAAA,GAAA,MAAA,QAA2B,IAAI;CACrC,MAAM,oBAAA,GAAA,MAAA,QAA0B,QAAQ;CACxC,iBAAiB,UAAU;CAE3B,MAAM,SAAA,GAAA,MAAA,mBAA0B;EAC9B,WAAW,QAAQ;GACjB,IAAI,CAAC,OAAO,CAAC,YAAY,SACvB,YAAY,UAAU,OAAO,YAAY,MAAM,SAAU,iBAAiB,OAAO;GAEnF,OAAO;EACT,CAAC;CACH,GAAG,CAAC,CAAC;CAEL,MAAM,QAAA,GAAA,MAAA,mBAAyB;EAC7B,UAAU,KAAK;EACf,IAAI,YAAY,SACd,OAAO,cAAc,YAAY,OAAO;EAE1C,YAAY,UAAU;CACxB,GAAG,CAAC,CAAC;CAEL,MAAM,UAAA,GAAA,MAAA,mBAA2B;EAC/B,WAAW,YAAY;GACrB,IAAI,SAAS;IACX,IAAI,YAAY,SACd,OAAO,cAAc,YAAY,OAAO;IAE1C,YAAY,UAAU;IACtB,OAAO;GACT;GACA,IAAI,CAAC,YAAY,SACf,YAAY,UAAU,OAAO,YAAY,MAAM,SAAU,iBAAiB,OAAO;GAEnF,OAAO;EACT,CAAC;CACH,GAAG,CAAC,CAAC;CAEL,CAAA,GAAA,MAAA,iBAAgB;EACd,MAAM,UAAU;EAChB,UAAU,MAAM;EAChB,OAAO;CACT,GAAG;EAAC;EAAI;EAAQ;CAAQ,CAAC;CAEzB,CAAA,GAAA,MAAA,iBAAgB;EACd,IAAI,YACF,MAAM;CAEV,GAAG,CAAC,CAAC;CAEL,OAAO;EAAE;EAAO;EAAM;EAAQ;CAAO;AACvC"}