@hanzogui/use-debounce 2.0.0

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Nate Wienert
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,73 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf,
6
+ __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: !0
11
+ });
12
+ },
13
+ __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
15
+ get: () => from[key],
16
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
+ });
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
+ value: mod,
27
+ enumerable: !0
28
+ }) : target, mod)),
29
+ __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
+ value: !0
31
+ }), mod);
32
+ var index_exports = {};
33
+ __export(index_exports, {
34
+ debounce: () => debounce,
35
+ useDebounce: () => useDebounce,
36
+ useDebounceValue: () => useDebounceValue
37
+ });
38
+ module.exports = __toCommonJS(index_exports);
39
+ var React = __toESM(require("react"), 1);
40
+ function debounce(func, wait, leading) {
41
+ let timeout,
42
+ isCancelled = !1;
43
+ function debounced() {
44
+ isCancelled = !1;
45
+ const args = arguments;
46
+ leading && !timeout && func.apply(this, args), clearTimeout(timeout), timeout = setTimeout(() => {
47
+ timeout = null, leading || isCancelled || func.apply(this, args), isCancelled = !1;
48
+ }, wait);
49
+ }
50
+ return debounced.cancel = () => {
51
+ isCancelled = !0;
52
+ }, debounced;
53
+ }
54
+ const defaultOpts = {
55
+ leading: !1
56
+ };
57
+ function useDebounce(fn, wait, options = defaultOpts, mountArgs = [fn]) {
58
+ const dbEffect = React.useRef(null);
59
+ return React.useEffect(() => () => {
60
+ dbEffect.current?.cancel();
61
+ }, []), React.useMemo(() => (dbEffect.current = debounce(fn, wait, options.leading), dbEffect.current), [options.leading, ...mountArgs]);
62
+ }
63
+ function useDebounceValue(val, amt = 0) {
64
+ const [state, setState] = React.useState(val);
65
+ return React.useEffect(() => {
66
+ const tm = setTimeout(() => {
67
+ setState(prev => prev === val ? prev : val);
68
+ }, amt);
69
+ return () => {
70
+ clearTimeout(tm);
71
+ };
72
+ }, [val]), state;
73
+ }
@@ -0,0 +1,87 @@
1
+ "use strict";
2
+
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf,
8
+ __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all) __defProp(target, name, {
11
+ get: all[name],
12
+ enumerable: !0
13
+ });
14
+ },
15
+ __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: !0
30
+ }) : target, mod)),
31
+ __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: !0
33
+ }), mod);
34
+ var index_exports = {};
35
+ __export(index_exports, {
36
+ debounce: () => debounce,
37
+ useDebounce: () => useDebounce,
38
+ useDebounceValue: () => useDebounceValue
39
+ });
40
+ module.exports = __toCommonJS(index_exports);
41
+ var React = __toESM(require("react"), 1);
42
+ function debounce(func, wait, leading) {
43
+ var timeout,
44
+ isCancelled = !1;
45
+ function debounced() {
46
+ var _this = this;
47
+ isCancelled = !1;
48
+ var args = arguments;
49
+ leading && !timeout && func.apply(this, args), clearTimeout(timeout), timeout = setTimeout(function () {
50
+ timeout = null, leading || isCancelled || func.apply(_this, args), isCancelled = !1;
51
+ }, wait);
52
+ }
53
+ return debounced.cancel = function () {
54
+ isCancelled = !0;
55
+ }, debounced;
56
+ }
57
+ var defaultOpts = {
58
+ leading: !1
59
+ };
60
+ function useDebounce(fn, wait) {
61
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : defaultOpts,
62
+ mountArgs = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : [fn],
63
+ dbEffect = React.useRef(null);
64
+ return React.useEffect(function () {
65
+ return function () {
66
+ var _dbEffect_current;
67
+ (_dbEffect_current = dbEffect.current) === null || _dbEffect_current === void 0 || _dbEffect_current.cancel();
68
+ };
69
+ }, []), React.useMemo(function () {
70
+ return dbEffect.current = debounce(fn, wait, options.leading), dbEffect.current;
71
+ }, [options.leading, ...mountArgs]);
72
+ }
73
+ function useDebounceValue(val) {
74
+ var amt = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0,
75
+ [state, setState] = React.useState(val);
76
+ return React.useEffect(function () {
77
+ var tm = setTimeout(function () {
78
+ setState(function (prev) {
79
+ return prev === val ? prev : val;
80
+ });
81
+ }, amt);
82
+ return function () {
83
+ clearTimeout(tm);
84
+ };
85
+ }, [val]), state;
86
+ }
87
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","__export","debounce","useDebounce","useDebounceValue","module","exports","React","__toESM","require","func","wait","leading","timeout","isCancelled","debounced","_this","args","arguments","apply","clearTimeout","setTimeout","cancel","defaultOpts","fn","options","length","mountArgs","dbEffect","useRef","useEffect","_dbEffect_current","current","useMemo"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAAA;EAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;IAAAC,KAAA;EAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,QAAA,CAAAD,aAAA;EAAAE,QAAA,EAAAA,CAAA,KAAAA,QAAA;EAAAC,WAAA,EAAAA,CAAA,KAAAA,WAAA;EAAAC,gBAAA,EAAAA,CAAA,KAAAA;AAAA;AAAAC,MAAA,CAAAC,OAAuB,GAAAX,YAAA,CAAAK,aAAA;AAQhB,IAAAO,KAAS,GAAAC,OACd,CAAAC,OACA,QACA,IAGA;AACA,SAAIP,QACAA,CAAAQ,IAAA,EAAAC,IAAA,EAAcC,OAAA;EAElB,IAAAC,OAAS;IAAAC,WAAqB;EAC5B,SAAAC,SAAcA,CAAA;IACd,IAAAC,KAAM,OAAO;IACTF,WAAW,GAAC;IAKd,IAAAG,IAAA,GAAAC,SACM;IAIRN,OAAO,KAAAC,OAAA,IAAAH,IAAA,CAAAS,KAAA,OAAAF,IAAA,GAAAG,YAAA,CAAAP,OAAA,GAAAA,OAAA,GAAAQ,UAAA;MACTR,OAAA,SAAAD,OAAA,IAAAE,WAAA,IAAAJ,IAAA,CAAAS,KAAA,CAAAH,KAAA,EAAAC,IAAA,GAAAH,WAAA;IAEA,GAAAH,IAAA;EACE;EACF,OAEOI,SAAA,CAAAO,MAAA;IACTR,WAAA;EAEA,GAAAC,SAAM;AAEC;AAWL,IAAAQ,WAAM;EAENX,OAAA;AAEI;AAAyB,SAExBT,WAEQA,CAAAqB,EAAA,EAAQb,IAAA,EACnB;EAGJ,IAAAc,OAAA,GAAAP,SAAA,CAAAQ,MAAA,QAAAR,SAAA,iBAAAA,SAAA,MAAAK,WAAA;IAAAI,SAAA,GAAAT,SAAA,CAAAQ,MAAA,QAAAR,SAAA,iBAAAA,SAAA,OAMOM,EAAA,CACL;IAAAI,QAAO,GAAOrB,KAAA,CAAAsB,MAAY,MAAM;EAEhC,OAAAtB,KAAA,CAAMuB,SAAA,CAAU,YAAM;IACpB,OAAM,YAAK;MACT,IAAAC,iBACM;MAGR,CAAGA,iBAAG,GAAAH,QAAA,CAAAI,OAAA,cAAAD,iBAAA,eAAAA,iBAAA,CAAAT,MAAA;IAEN;EACE,QAAAf,KAAA,CAAA0B,OAAe;IACjB,OAAAL,QAAA,CAAAI,OAAA,GAAA9B,QAAA,CAAAsB,EAAA,EAAAb,IAAA,EAAAc,OAAA,CAAAb,OAAA,GAAAgB,QAAA,CAAAI,OAAA;EACF,GAAG,CAGLP,OAAA,CAAAb,OAAA,E","ignoreList":[]}
@@ -0,0 +1,37 @@
1
+ import * as React from "react";
2
+ function debounce(func, wait, leading) {
3
+ let timeout,
4
+ isCancelled = !1;
5
+ function debounced() {
6
+ isCancelled = !1;
7
+ const args = arguments;
8
+ leading && !timeout && func.apply(this, args), clearTimeout(timeout), timeout = setTimeout(() => {
9
+ timeout = null, leading || isCancelled || func.apply(this, args), isCancelled = !1;
10
+ }, wait);
11
+ }
12
+ return debounced.cancel = () => {
13
+ isCancelled = !0;
14
+ }, debounced;
15
+ }
16
+ const defaultOpts = {
17
+ leading: !1
18
+ };
19
+ function useDebounce(fn, wait, options = defaultOpts, mountArgs = [fn]) {
20
+ const dbEffect = React.useRef(null);
21
+ return React.useEffect(() => () => {
22
+ dbEffect.current?.cancel();
23
+ }, []), React.useMemo(() => (dbEffect.current = debounce(fn, wait, options.leading), dbEffect.current), [options.leading, ...mountArgs]);
24
+ }
25
+ function useDebounceValue(val, amt = 0) {
26
+ const [state, setState] = React.useState(val);
27
+ return React.useEffect(() => {
28
+ const tm = setTimeout(() => {
29
+ setState(prev => prev === val ? prev : val);
30
+ }, amt);
31
+ return () => {
32
+ clearTimeout(tm);
33
+ };
34
+ }, [val]), state;
35
+ }
36
+ export { debounce, useDebounce, useDebounceValue };
37
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["React","debounce","func","wait","leading","timeout","isCancelled","debounced","args","arguments","apply","clearTimeout","setTimeout","cancel","defaultOpts","useDebounce","fn","options","mountArgs","dbEffect","useRef","useEffect","current","useMemo","useDebounceValue","val","amt","state","setState","useState","tm","prev"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,YAAYA,KAAA,MAAW;AAQhB,SAASC,SACdC,IAAA,EACAC,IAAA,EACAC,OAAA,EAGA;EACA,IAAIC,OAAA;IACAC,WAAA,GAAc;EAElB,SAASC,UAAA,EAAqB;IAC5BD,WAAA,GAAc;IACd,MAAME,IAAA,GAAOC,SAAA;IACTL,OAAA,IAAW,CAACC,OAAA,IACdH,IAAA,CAAKQ,KAAA,CAAM,MAAMF,IAAI,GAEvBG,YAAA,CAAaN,OAAO,GACpBA,OAAA,GAAUO,UAAA,CAAW,MAAM;MACzBP,OAAA,GAAU,MACJD,OAAA,IAAWE,WAAA,IACfJ,IAAA,CAAKQ,KAAA,CAAM,MAAMF,IAAI,GAEvBF,WAAA,GAAc;IAChB,GAAGH,IAAI;EACT;EAEA,OAAAI,SAAA,CAAUM,MAAA,GAAS,MAAM;IACvBP,WAAA,GAAc;EAChB,GAEOC,SAAA;AACT;AAEA,MAAMO,WAAA,GAAc;EAAEV,OAAA,EAAS;AAAM;AAE9B,SAASW,YAMdC,EAAA,EACAb,IAAA,EACAc,OAAA,GAA4BH,WAAA,EAC5BI,SAAA,GAAmB,CAACF,EAAE,GACT;EACb,MAAMG,QAAA,GAAWnB,KAAA,CAAMoB,MAAA,CAA2B,IAAI;EAEtD,OAAApB,KAAA,CAAMqB,SAAA,CAAU,MACP,MAAM;IACXF,QAAA,CAASG,OAAA,EAAST,MAAA,CAAO;EAC3B,GACC,EAAE,GAEEb,KAAA,CAAMuB,OAAA,CAAQ,OACnBJ,QAAA,CAASG,OAAA,GAAUrB,QAAA,CAASe,EAAA,EAAIb,IAAA,EAAMc,OAAA,CAAQb,OAAO,GAC9Ce,QAAA,CAASG,OAAA,GACf,CAACL,OAAA,CAAQb,OAAA,EAAS,GAAGc,SAAS,CAAC;AACpC;AAMO,SAASM,iBAAoBC,GAAA,EAAQC,GAAA,GAAM,GAAM;EACtD,MAAM,CAACC,KAAA,EAAOC,QAAQ,IAAI5B,KAAA,CAAM6B,QAAA,CAASJ,GAAG;EAE5C,OAAAzB,KAAA,CAAMqB,SAAA,CAAU,MAAM;IACpB,MAAMS,EAAA,GAAKlB,UAAA,CAAW,MAAM;MAC1BgB,QAAA,CAAUG,IAAA,IACJA,IAAA,KAASN,GAAA,GAAYM,IAAA,GAClBN,GACR;IACH,GAAGC,GAAG;IAEN,OAAO,MAAM;MACXf,YAAA,CAAamB,EAAE;IACjB;EACF,GAAG,CAACL,GAAG,CAAC,GAEDE,KAAA;AACT","ignoreList":[]}
@@ -0,0 +1,37 @@
1
+ import * as React from "react";
2
+ function debounce(func, wait, leading) {
3
+ let timeout,
4
+ isCancelled = !1;
5
+ function debounced() {
6
+ isCancelled = !1;
7
+ const args = arguments;
8
+ leading && !timeout && func.apply(this, args), clearTimeout(timeout), timeout = setTimeout(() => {
9
+ timeout = null, leading || isCancelled || func.apply(this, args), isCancelled = !1;
10
+ }, wait);
11
+ }
12
+ return debounced.cancel = () => {
13
+ isCancelled = !0;
14
+ }, debounced;
15
+ }
16
+ const defaultOpts = {
17
+ leading: !1
18
+ };
19
+ function useDebounce(fn, wait, options = defaultOpts, mountArgs = [fn]) {
20
+ const dbEffect = React.useRef(null);
21
+ return React.useEffect(() => () => {
22
+ dbEffect.current?.cancel();
23
+ }, []), React.useMemo(() => (dbEffect.current = debounce(fn, wait, options.leading), dbEffect.current), [options.leading, ...mountArgs]);
24
+ }
25
+ function useDebounceValue(val, amt = 0) {
26
+ const [state, setState] = React.useState(val);
27
+ return React.useEffect(() => {
28
+ const tm = setTimeout(() => {
29
+ setState(prev => prev === val ? prev : val);
30
+ }, amt);
31
+ return () => {
32
+ clearTimeout(tm);
33
+ };
34
+ }, [val]), state;
35
+ }
36
+ export { debounce, useDebounce, useDebounceValue };
37
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["React","debounce","func","wait","leading","timeout","isCancelled","debounced","args","arguments","apply","clearTimeout","setTimeout","cancel","defaultOpts","useDebounce","fn","options","mountArgs","dbEffect","useRef","useEffect","current","useMemo","useDebounceValue","val","amt","state","setState","useState","tm","prev"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,YAAYA,KAAA,MAAW;AAQhB,SAASC,SACdC,IAAA,EACAC,IAAA,EACAC,OAAA,EAGA;EACA,IAAIC,OAAA;IACAC,WAAA,GAAc;EAElB,SAASC,UAAA,EAAqB;IAC5BD,WAAA,GAAc;IACd,MAAME,IAAA,GAAOC,SAAA;IACTL,OAAA,IAAW,CAACC,OAAA,IACdH,IAAA,CAAKQ,KAAA,CAAM,MAAMF,IAAI,GAEvBG,YAAA,CAAaN,OAAO,GACpBA,OAAA,GAAUO,UAAA,CAAW,MAAM;MACzBP,OAAA,GAAU,MACJD,OAAA,IAAWE,WAAA,IACfJ,IAAA,CAAKQ,KAAA,CAAM,MAAMF,IAAI,GAEvBF,WAAA,GAAc;IAChB,GAAGH,IAAI;EACT;EAEA,OAAAI,SAAA,CAAUM,MAAA,GAAS,MAAM;IACvBP,WAAA,GAAc;EAChB,GAEOC,SAAA;AACT;AAEA,MAAMO,WAAA,GAAc;EAAEV,OAAA,EAAS;AAAM;AAE9B,SAASW,YAMdC,EAAA,EACAb,IAAA,EACAc,OAAA,GAA4BH,WAAA,EAC5BI,SAAA,GAAmB,CAACF,EAAE,GACT;EACb,MAAMG,QAAA,GAAWnB,KAAA,CAAMoB,MAAA,CAA2B,IAAI;EAEtD,OAAApB,KAAA,CAAMqB,SAAA,CAAU,MACP,MAAM;IACXF,QAAA,CAASG,OAAA,EAAST,MAAA,CAAO;EAC3B,GACC,EAAE,GAEEb,KAAA,CAAMuB,OAAA,CAAQ,OACnBJ,QAAA,CAASG,OAAA,GAAUrB,QAAA,CAASe,EAAA,EAAIb,IAAA,EAAMc,OAAA,CAAQb,OAAO,GAC9Ce,QAAA,CAASG,OAAA,GACf,CAACL,OAAA,CAAQb,OAAA,EAAS,GAAGc,SAAS,CAAC;AACpC;AAMO,SAASM,iBAAoBC,GAAA,EAAQC,GAAA,GAAM,GAAM;EACtD,MAAM,CAACC,KAAA,EAAOC,QAAQ,IAAI5B,KAAA,CAAM6B,QAAA,CAASJ,GAAG;EAE5C,OAAAzB,KAAA,CAAMqB,SAAA,CAAU,MAAM;IACpB,MAAMS,EAAA,GAAKlB,UAAA,CAAW,MAAM;MAC1BgB,QAAA,CAAUG,IAAA,IACJA,IAAA,KAASN,GAAA,GAAYM,IAAA,GAClBN,GACR;IACH,GAAGC,GAAG;IAEN,OAAO,MAAM;MACXf,YAAA,CAAamB,EAAE;IACjB;EACF,GAAG,CAACL,GAAG,CAAC,GAEDE,KAAA;AACT","ignoreList":[]}
@@ -0,0 +1,48 @@
1
+ import * as React from "react";
2
+ function debounce(func, wait, leading) {
3
+ var timeout,
4
+ isCancelled = !1;
5
+ function debounced() {
6
+ var _this = this;
7
+ isCancelled = !1;
8
+ var args = arguments;
9
+ leading && !timeout && func.apply(this, args), clearTimeout(timeout), timeout = setTimeout(function () {
10
+ timeout = null, leading || isCancelled || func.apply(_this, args), isCancelled = !1;
11
+ }, wait);
12
+ }
13
+ return debounced.cancel = function () {
14
+ isCancelled = !0;
15
+ }, debounced;
16
+ }
17
+ var defaultOpts = {
18
+ leading: !1
19
+ };
20
+ function useDebounce(fn, wait) {
21
+ var options = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : defaultOpts,
22
+ mountArgs = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : [fn],
23
+ dbEffect = React.useRef(null);
24
+ return React.useEffect(function () {
25
+ return function () {
26
+ var _dbEffect_current;
27
+ (_dbEffect_current = dbEffect.current) === null || _dbEffect_current === void 0 || _dbEffect_current.cancel();
28
+ };
29
+ }, []), React.useMemo(function () {
30
+ return dbEffect.current = debounce(fn, wait, options.leading), dbEffect.current;
31
+ }, [options.leading, ...mountArgs]);
32
+ }
33
+ function useDebounceValue(val) {
34
+ var amt = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 0,
35
+ [state, setState] = React.useState(val);
36
+ return React.useEffect(function () {
37
+ var tm = setTimeout(function () {
38
+ setState(function (prev) {
39
+ return prev === val ? prev : val;
40
+ });
41
+ }, amt);
42
+ return function () {
43
+ clearTimeout(tm);
44
+ };
45
+ }, [val]), state;
46
+ }
47
+ export { debounce, useDebounce, useDebounceValue };
48
+ //# sourceMappingURL=index.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["React","debounce","func","wait","leading","timeout","isCancelled","debounced","_this","args","arguments","apply","clearTimeout","setTimeout","cancel","defaultOpts","useDebounce","fn","options","length","mountArgs","dbEffect","useRef","useEffect","_dbEffect_current","current","useMemo"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AAAA,YAAYA,KAAA,MAAW;AAQhB,SAASC,SACdC,IAAA,EACAC,IAAA,EACAC,OAAA,EAGA;EACA,IAAIC,OAAA;IACAC,WAAA,GAAc;EAElB,SAASC,UAAA,EAAqB;IAC5B,IAAAC,KAAA,OAAc;IACdF,WAAM,GAAO;IACT,IAAAG,IAAA,GAAWC,SAAC;IAKdN,OAAA,KAAUC,OACJ,IAAAH,IAAA,CAAAS,KAAW,OAAAF,IACf,GAAAG,YAAW,CAAAP,OAAU,CAEvB,EAAAA,OAAA,GAAAQ,UAAc;MAChBR,OAAO,SAAAD,OAAA,IAAAE,WAAA,IAAAJ,IAAA,CAAAS,KAAA,CAAAH,KAAA,EAAAC,IAAA,GAAAH,WAAA;IACT,GAAAH,IAAA;EAEA;EACE,OAAAI,SAAc,CAAAO,MAAA;IAChBR,WAEO;EACT,GAAAC,SAAA;AAEA;AAEO,IAAAQ,WAAS;EAWdX,OAAM;AAEN;AAEI,SAAAY,WAASA,CAAAC,EAAS,EAAAd,IAAO;EAC3B,IACEe,OAEG,GAAMR,SAAQ,CAAAS,MACnB,QAAST,SAAA,CAAU,YAAS,CAAI,GAAAA,SAAM,EAAQ,IAAAK,WACvC;IAAAK,SAAS,GACfV,SAAS,CAAAS,MAAA,GAAS,KAAGT,SAAU,iBAAAA,SAAA,OACpCO,EAAA,CAMO;IAAAI,QAAS,GAAArB,KAAA,CAAAsB,MAAoB,KAAQ;EAC1C,OAAOtB,KAAA,CAAAuB,SAAe,aAAU;IAEhC,mBAAM;MACJ,IAAMC,iBAAgB;MACpB,CAAAA,iBAAU,GACJH,QAAS,CAAAI,OAAY,MAClB,IACR,IAAAD,iBAAA,eAAAA,iBAAA,CAAAV,MAAA;IACH;EAEA,QAAAd,KAAO,CAAA0B,OAAM;IACX,OAAAL,QAAa,CAAAI,OAAE,GAAAxB,QAAA,CAAAgB,EAAA,EAAAd,IAAA,EAAAe,OAAA,CAAAd,OAAA,GAAAiB,QAAA,CAAAI,OAAA;EAAA,GACjB,CACFP,OAAQ,CAAAd,OAED,EACT,GAAAgB,SAAA,C","ignoreList":[]}
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "name": "@hanzogui/use-debounce",
3
+ "version": "2.0.0",
4
+ "gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
5
+ "files": [
6
+ "src",
7
+ "types",
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "sideEffects": false,
12
+ "main": "dist/cjs",
13
+ "module": "dist/esm",
14
+ "types": "./types/index.d.ts",
15
+ "exports": {
16
+ "./package.json": "./package.json",
17
+ ".": {
18
+ "types": "./types/index.d.ts",
19
+ "react-native": "./dist/esm/index.native.js",
20
+ "browser": "./dist/esm/index.mjs",
21
+ "module": "./dist/esm/index.mjs",
22
+ "import": "./dist/esm/index.mjs",
23
+ "require": "./dist/cjs/index.cjs",
24
+ "default": "./dist/esm/index.mjs"
25
+ }
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "scripts": {
31
+ "build": "hanzo-gui-build",
32
+ "watch": "hanzo-gui-build --watch",
33
+ "clean": "hanzo-gui-build clean",
34
+ "clean:build": "hanzo-gui-build clean:build"
35
+ },
36
+ "devDependencies": {
37
+ "@hanzogui/build": "workspace:*",
38
+ "react": ">=19"
39
+ },
40
+ "peerDependencies": {
41
+ "react": ">=19"
42
+ }
43
+ }
package/src/index.ts ADDED
@@ -0,0 +1,90 @@
1
+ import * as React from 'react'
2
+
3
+ type DebounceSettings = {
4
+ leading?: boolean
5
+ }
6
+
7
+ // TODO doesn't return value of the function called :/
8
+
9
+ export function debounce<A extends Function>(
10
+ func: A,
11
+ wait?: number,
12
+ leading?: boolean
13
+ ): A & {
14
+ cancel: () => void
15
+ } {
16
+ let timeout: any
17
+ let isCancelled = false
18
+
19
+ function debounced(this: any) {
20
+ isCancelled = false
21
+ const args = arguments
22
+ if (leading && !timeout) {
23
+ func.apply(this, args)
24
+ }
25
+ clearTimeout(timeout)
26
+ timeout = setTimeout(() => {
27
+ timeout = null
28
+ if (!(leading || isCancelled)) {
29
+ func.apply(this, args)
30
+ }
31
+ isCancelled = false
32
+ }, wait)
33
+ }
34
+
35
+ debounced.cancel = () => {
36
+ isCancelled = true
37
+ }
38
+
39
+ return debounced as any
40
+ }
41
+
42
+ const defaultOpts = { leading: false }
43
+
44
+ export function useDebounce<
45
+ A extends (...args: any) => any | undefined | null,
46
+ DebouncedFn extends A & {
47
+ cancel: () => void
48
+ },
49
+ >(
50
+ fn: A,
51
+ wait: number,
52
+ options: DebounceSettings = defaultOpts,
53
+ mountArgs: any[] = [fn]
54
+ ): DebouncedFn {
55
+ const dbEffect = React.useRef<DebouncedFn | null>(null)
56
+
57
+ React.useEffect(() => {
58
+ return () => {
59
+ dbEffect.current?.cancel()
60
+ }
61
+ }, [])
62
+
63
+ return React.useMemo(() => {
64
+ dbEffect.current = debounce(fn, wait, options.leading) as unknown as DebouncedFn
65
+ return dbEffect.current
66
+ }, [options.leading, ...mountArgs])
67
+ }
68
+
69
+ /**
70
+ * Returns a value once it stops changing after "amt" time.
71
+ * Note: you may need to memo or this will keep re-rendering
72
+ */
73
+ export function useDebounceValue<A>(val: A, amt = 0): A {
74
+ const [state, setState] = React.useState(val)
75
+
76
+ React.useEffect(() => {
77
+ const tm = setTimeout(() => {
78
+ setState((prev) => {
79
+ if (prev === val) return prev
80
+ return val
81
+ })
82
+ }, amt)
83
+
84
+ return () => {
85
+ clearTimeout(tm)
86
+ }
87
+ }, [val])
88
+
89
+ return state
90
+ }
@@ -0,0 +1,20 @@
1
+ type DebounceSettings = {
2
+ leading?: boolean;
3
+ };
4
+ export declare function debounce<A extends Function>(func: A, wait?: number, leading?: boolean): A & {
5
+ cancel: () => void;
6
+ };
7
+ export declare function useDebounce<
8
+ A extends (...args: any) => any | undefined | null,
9
+ DebouncedFn extends A & {
10
+ cancel: () => void;
11
+ }
12
+ >(fn: A, wait: number, options?: DebounceSettings, mountArgs?: any[]): DebouncedFn;
13
+ /**
14
+ * Returns a value once it stops changing after "amt" time.
15
+ * Note: you may need to memo or this will keep re-rendering
16
+ */
17
+ export declare function useDebounceValue<A>(val: A, amt?: number): A;
18
+ export {};
19
+
20
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,11 @@
1
+ {
2
+ "mappings": "KAEK,mBAAmB;CACtB;;AAKF,OAAO,iBAAS,SAAS,UAAU,UACjC,MAAM,GACN,eACA,oBACC,IAAI;CACL;;AA8BF,OAAO,iBAAS;CACd,WAAW,GAAG;CACd,oBAAoB,IAAI;EACtB;;EAGF,IAAI,GACJ,cACA,UAAS,kBACT,oBACC;;;;;AAmBH,OAAO,iBAAS,iBAAiB,GAAG,KAAK,GAAG,eAAU",
3
+ "names": [],
4
+ "sources": [
5
+ "src/index.ts"
6
+ ],
7
+ "version": 3,
8
+ "sourcesContent": [
9
+ "import * as React from 'react'\n\ntype DebounceSettings = {\n leading?: boolean\n}\n\n// TODO doesn't return value of the function called :/\n\nexport function debounce<A extends Function>(\n func: A,\n wait?: number,\n leading?: boolean\n): A & {\n cancel: () => void\n} {\n let timeout: any\n let isCancelled = false\n\n function debounced(this: any) {\n isCancelled = false\n const args = arguments\n if (leading && !timeout) {\n func.apply(this, args)\n }\n clearTimeout(timeout)\n timeout = setTimeout(() => {\n timeout = null\n if (!(leading || isCancelled)) {\n func.apply(this, args)\n }\n isCancelled = false\n }, wait)\n }\n\n debounced.cancel = () => {\n isCancelled = true\n }\n\n return debounced as any\n}\n\nconst defaultOpts = { leading: false }\n\nexport function useDebounce<\n A extends (...args: any) => any | undefined | null,\n DebouncedFn extends A & {\n cancel: () => void\n },\n>(\n fn: A,\n wait: number,\n options: DebounceSettings = defaultOpts,\n mountArgs: any[] = [fn]\n): DebouncedFn {\n const dbEffect = React.useRef<DebouncedFn | null>(null)\n\n React.useEffect(() => {\n return () => {\n dbEffect.current?.cancel()\n }\n }, [])\n\n return React.useMemo(() => {\n dbEffect.current = debounce(fn, wait, options.leading) as unknown as DebouncedFn\n return dbEffect.current\n }, [options.leading, ...mountArgs])\n}\n\n/**\n * Returns a value once it stops changing after \"amt\" time.\n * Note: you may need to memo or this will keep re-rendering\n */\nexport function useDebounceValue<A>(val: A, amt = 0): A {\n const [state, setState] = React.useState(val)\n\n React.useEffect(() => {\n const tm = setTimeout(() => {\n setState((prev) => {\n if (prev === val) return prev\n return val\n })\n }, amt)\n\n return () => {\n clearTimeout(tm)\n }\n }, [val])\n\n return state\n}\n"
10
+ ]
11
+ }