@1hook/use-debounce-fn 0.0.1-beta.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 +7 -0
- package/dist/index.js +32 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js.gz +0 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
function useDebounceFn(defaultDelay) {
|
|
5
|
+
const timeoutRef = React.useRef(0);
|
|
6
|
+
const [isPending, setIsPending] = React.useState(false);
|
|
7
|
+
const cancel = React.useCallback(() => {
|
|
8
|
+
setIsPending(false);
|
|
9
|
+
clearTimeout(timeoutRef.current);
|
|
10
|
+
}, []);
|
|
11
|
+
const debounce = React.useCallback(
|
|
12
|
+
(callback, delay = defaultDelay) => {
|
|
13
|
+
cancel();
|
|
14
|
+
if (!delay) {
|
|
15
|
+
callback();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
setIsPending(true);
|
|
19
|
+
timeoutRef.current = setTimeout(() => {
|
|
20
|
+
setIsPending(false);
|
|
21
|
+
callback();
|
|
22
|
+
}, delay);
|
|
23
|
+
},
|
|
24
|
+
[cancel, defaultDelay]
|
|
25
|
+
);
|
|
26
|
+
React.useEffect(() => cancel, [cancel]);
|
|
27
|
+
return { debounce, cancel, isPending };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { useDebounceFn };
|
|
31
|
+
//# sourceMappingURL=index.js.map
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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"]}
|
|
Binary file
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@1hook/use-debounce-fn",
|
|
3
|
+
"version": "0.0.1-beta.1",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist",
|
|
6
|
+
"README.md"
|
|
7
|
+
],
|
|
8
|
+
"type": "module",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"peerDependencies": {
|
|
17
|
+
"@types/react": ">=18.0.0 || >=19.0.0",
|
|
18
|
+
"@types/react-dom": ">=18.0.0 || >=19.0.0",
|
|
19
|
+
"react": ">=18.0.0 || >=19.0.0",
|
|
20
|
+
"react-dom": ">=18.0.0 || >=19.0.0"
|
|
21
|
+
},
|
|
22
|
+
"tag": "beta",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "NODE_ENV=production tsup",
|
|
25
|
+
"dev": "concurrently \"tsup --watch\" \"vitest\"",
|
|
26
|
+
"test": "vitest run && tsc --noEmit",
|
|
27
|
+
"lint": "eslint . && tsc --noEmit",
|
|
28
|
+
"lint:eslint": "eslint .",
|
|
29
|
+
"lint:typescript": "tsc --noEmit"
|
|
30
|
+
}
|
|
31
|
+
}
|