@1hook/use-debounce-fn 0.0.1-beta.1 → 0.0.1

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 CHANGED
@@ -1,7 +1,20 @@
1
- declare function useDebounceFn(defaultDelay: number): {
2
- debounce: (callback: () => any, delay?: number) => void;
1
+ type UseDebounceFnReturn = {
2
+ /**
3
+ * Debounce a function execution.
4
+ *
5
+ * @param fn - The function to debounce.
6
+ * @param delay - Override the default delay in milliseconds (optional).
7
+ */
8
+ debounce: (fn: () => any, delay?: number) => void;
9
+ /**
10
+ * Cancel the debounce.
11
+ */
3
12
  cancel: () => void;
13
+ /**
14
+ * Whether a debounced call is pending.
15
+ */
4
16
  isPending: boolean;
5
17
  };
18
+ declare function useDebounceFn(defaultDelay?: number): UseDebounceFnReturn;
6
19
 
7
- export { useDebounceFn };
20
+ export { type UseDebounceFnReturn, useDebounceFn };
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
 
3
3
  // src/index.ts
4
- function useDebounceFn(defaultDelay) {
4
+ function useDebounceFn(defaultDelay = 0) {
5
5
  const timeoutRef = React.useRef(0);
6
6
  const [isPending, setIsPending] = React.useState(false);
7
7
  const cancel = React.useCallback(() => {
@@ -9,16 +9,16 @@ function useDebounceFn(defaultDelay) {
9
9
  clearTimeout(timeoutRef.current);
10
10
  }, []);
11
11
  const debounce = React.useCallback(
12
- (callback, delay = defaultDelay) => {
12
+ (fn, delay = defaultDelay) => {
13
13
  cancel();
14
14
  if (!delay) {
15
- callback();
15
+ fn();
16
16
  return;
17
17
  }
18
18
  setIsPending(true);
19
19
  timeoutRef.current = setTimeout(() => {
20
20
  setIsPending(false);
21
- callback();
21
+ fn();
22
22
  }, delay);
23
23
  },
24
24
  [cancel, defaultDelay]
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAEO,SAAS,cAAc,YAAsB,EAAA;AAClD,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,QAAqB,EAAA,KAAA,GAAgB,YAAiB,KAAA;AACrD,MAAO,MAAA,EAAA;AACP,MAAA,IAAI,CAAC,KAAO,EAAA;AACV,QAAS,QAAA,EAAA;AACT,QAAA;AAAA;AAEF,MAAA,YAAA,CAAa,IAAI,CAAA;AACjB,MAAW,UAAA,CAAA,OAAA,GAAU,WAAW,MAAM;AACpC,QAAA,YAAA,CAAa,KAAK,CAAA;AAClB,QAAS,QAAA,EAAA;AAAA,SACR,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 function useDebounceFn(defaultDelay: number) {\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 (callback: () => any, delay: number = defaultDelay) => {\n cancel()\n if (!delay) {\n callback()\n return\n }\n setIsPending(true)\n timeoutRef.current = setTimeout(() => {\n setIsPending(false)\n callback()\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"]}
1
+ {"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AAoBO,SAAS,aAAA,CAAc,eAAuB,CAAwB,EAAA;AAC3E,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\nexport function useDebounceFn(defaultDelay: number = 0): 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"]}
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1hook/use-debounce-fn",
3
- "version": "0.0.1-beta.1",
3
+ "version": "0.0.1",
4
4
  "files": [
5
5
  "dist",
6
6
  "README.md"
@@ -20,6 +20,22 @@
20
20
  "react-dom": ">=18.0.0 || >=19.0.0"
21
21
  },
22
22
  "tag": "beta",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "https://github.com/L-Blondy/one-hook"
26
+ },
27
+ "homepage": "https://github.com/L-Blondy/one-hook",
28
+ "bugs": {
29
+ "url": "https://github.com/L-Blondy/one-hook/issues"
30
+ },
31
+ "peerDependenciesMeta": {
32
+ "@types/react": {
33
+ "optional": true
34
+ },
35
+ "@types/react-dom": {
36
+ "optional": true
37
+ }
38
+ },
23
39
  "scripts": {
24
40
  "build": "NODE_ENV=production tsup",
25
41
  "dev": "concurrently \"tsup --watch\" \"vitest\"",