@1hook/use-debounce-fn 0.2.1 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAuBO,SAAS,cAAc,YAA2C,EAAA;AACvE,EAAM,MAAA,UAAA,GAAa,KAAM,CAAA,MAAA,CAAgC,CAAC,CAAA;AAG1D,EAAA,MAAM,CAAC,SAAW,EAAA,YAAY,CAAI,GAAA,KAAA,CAAM,SAAS,KAAK,CAAA;AAEtD,EAAM,MAAA,MAAA,GAAS,KAAM,CAAA,WAAA,CAAY,MAAM;AACrC,IAAA,YAAA,CAAa,KAAK,CAAA;AAClB,IAAA,YAAA,CAAa,WAAW,OAAO,CAAA;AAAA,GACjC,EAAG,EAAE,CAAA;AAEL,EAAA,MAAM,WAAW,KAAM,CAAA,WAAA;AAAA,IACrB,CAAC,EAAe,EAAA,KAAA,GAAgB,YAAiB,KAAA;AAC/C,MAAO,MAAA,EAAA;AACP,MAAA,IAAI,CAAC,KAAO,EAAA;AACV,QAAG,EAAA,EAAA;AACH,QAAA;AAAA;AAEF,MAAA,YAAA,CAAa,IAAI,CAAA;AACjB,MAAW,UAAA,CAAA,OAAA,GAAU,WAAW,MAAM;AACpC,QAAA,YAAA,CAAa,KAAK,CAAA;AAClB,QAAG,EAAA,EAAA;AAAA,SACF,KAAK,CAAA;AAAA,KACV;AAAA,IACA,CAAC,QAAQ,YAAY;AAAA,GACvB;AAGA,EAAA,KAAA,CAAM,SAAU,CAAA,MAAM,MAAQ,EAAA,CAAC,MAAM,CAAC,CAAA;AAEtC,EAAO,OAAA,EAAE,QAAU,EAAA,MAAA,EAAQ,SAAU,EAAA;AACvC","file":"index.js","sourcesContent":["import React from 'react'\n\nexport type UseDebounceFnReturn = {\n /**\n * Debounce a function execution.\n *\n * @param fn - The function to debounce.\n * @param delay - Override the default delay in milliseconds (optional).\n */\n debounce: (fn: () => any, delay?: number) => void\n /**\n * Cancel the debounce.\n */\n cancel: () => void\n /**\n * Whether a debounced call is pending.\n */\n isPending: boolean\n}\n\n/**\n * https://one-hook.vercel.app/docs\n */\nexport function useDebounceFn(defaultDelay: number): UseDebounceFnReturn {\n const timeoutRef = React.useRef<NodeJS.Timeout | number>(0)\n\n // use async transitions in React 19\n const [isPending, setIsPending] = React.useState(false)\n\n const cancel = React.useCallback(() => {\n setIsPending(false)\n clearTimeout(timeoutRef.current)\n }, [])\n\n const debounce = React.useCallback(\n (fn: () => any, delay: number = defaultDelay) => {\n cancel()\n if (!delay) {\n fn()\n return\n }\n setIsPending(true)\n timeoutRef.current = setTimeout(() => {\n setIsPending(false)\n fn()\n }, delay)\n },\n [cancel, defaultDelay],\n )\n\n // clear the debounce on unmount\n React.useEffect(() => cancel, [cancel])\n\n return { debounce, cancel, isPending }\n}\n"]}
|