@tiny-codes/react-easy 1.4.15 → 1.4.16
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/CHANGELOG.md +10 -0
- package/es/hooks/index.d.ts +3 -2
- package/es/hooks/index.js +3 -2
- package/es/hooks/index.js.map +1 -1
- package/es/hooks/useDebounce.d.ts +37 -0
- package/es/hooks/useDebounce.js +92 -0
- package/es/hooks/useDebounce.js.map +1 -0
- package/lib/hooks/index.d.ts +3 -2
- package/lib/hooks/index.js +5 -2
- package/lib/hooks/index.js.map +2 -2
- package/lib/hooks/useDebounce.d.ts +37 -0
- package/lib/hooks/useDebounce.js +71 -0
- package/lib/hooks/useDebounce.js.map +7 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/es/hooks/index.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
export { default as useAudioPlayer } from './useAudioPlayer';
|
|
2
|
+
export { default as useDebounce } from './useDebounce';
|
|
2
3
|
export { default as useRefValue } from './useRefValue';
|
|
3
4
|
export { default as useRefFunction } from './useRefFunction';
|
|
4
5
|
export * from './useSSE';
|
|
5
6
|
export { default as useSSE } from './useSSE';
|
|
6
7
|
export * from './useStompSocket';
|
|
7
8
|
export { default as useStompSocket } from './useStompSocket';
|
|
9
|
+
export * from './useUserMedia';
|
|
10
|
+
export { default as useUserMedia } from './useUserMedia';
|
|
8
11
|
export type { ValidatorRuleMap } from './useValidators';
|
|
9
12
|
export { default as useValidators } from './useValidators';
|
|
10
13
|
export { default as useValidator } from './useValidator';
|
|
11
14
|
export type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';
|
|
12
15
|
export { default as useValidatorBuilder } from './useValidatorBuilder';
|
|
13
|
-
export * from './useUserMedia';
|
|
14
|
-
export { default as useUserMedia } from './useUserMedia';
|
package/es/hooks/index.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
export { default as useAudioPlayer } from "./useAudioPlayer";
|
|
2
|
+
export { default as useDebounce } from "./useDebounce";
|
|
2
3
|
export { default as useRefValue } from "./useRefValue";
|
|
3
4
|
export { default as useRefFunction } from "./useRefFunction";
|
|
4
5
|
export * from "./useSSE";
|
|
5
6
|
export { default as useSSE } from "./useSSE";
|
|
6
7
|
export * from "./useStompSocket";
|
|
7
8
|
export { default as useStompSocket } from "./useStompSocket";
|
|
9
|
+
export * from "./useUserMedia";
|
|
10
|
+
export { default as useUserMedia } from "./useUserMedia";
|
|
8
11
|
export { default as useValidators } from "./useValidators";
|
|
9
12
|
export { default as useValidator } from "./useValidator";
|
|
10
13
|
export { default as useValidatorBuilder } from "./useValidatorBuilder";
|
|
11
|
-
export * from "./useUserMedia";
|
|
12
|
-
export { default as useUserMedia } from "./useUserMedia";
|
|
13
14
|
//# sourceMappingURL=index.js.map
|
package/es/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["default","useAudioPlayer","useRefValue","useRefFunction","useSSE","useStompSocket","useValidators","useValidator","useValidatorBuilder"
|
|
1
|
+
{"version":3,"names":["default","useAudioPlayer","useDebounce","useRefValue","useRefFunction","useSSE","useStompSocket","useUserMedia","useValidators","useValidator","useValidatorBuilder"],"sources":["../../src/hooks/index.ts"],"sourcesContent":["export { default as useAudioPlayer } from './useAudioPlayer';\nexport { default as useDebounce } from './useDebounce';\nexport { default as useRefValue } from './useRefValue';\nexport { default as useRefFunction } from './useRefFunction';\nexport * from './useSSE';\nexport { default as useSSE } from './useSSE';\nexport * from './useStompSocket';\nexport { default as useStompSocket } from './useStompSocket';\nexport * from './useUserMedia';\nexport { default as useUserMedia } from './useUserMedia';\nexport type { ValidatorRuleMap } from './useValidators';\nexport { default as useValidators } from './useValidators';\nexport { default as useValidator } from './useValidator';\nexport type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';\nexport { default as useValidatorBuilder } from './useValidatorBuilder';\n"],"mappings":"AAAA,SAASA,OAAO,IAAIC,cAAc;AAClC,SAASD,OAAO,IAAIE,WAAW;AAC/B,SAASF,OAAO,IAAIG,WAAW;AAC/B,SAASH,OAAO,IAAII,cAAc;AAClC;AACA,SAASJ,OAAO,IAAIK,MAAM;AAC1B;AACA,SAASL,OAAO,IAAIM,cAAc;AAClC;AACA,SAASN,OAAO,IAAIO,YAAY;AAEhC,SAASP,OAAO,IAAIQ,aAAa;AACjC,SAASR,OAAO,IAAIS,YAAY;AAEhC,SAAST,OAAO,IAAIU,mBAAmB"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface UseDebounceOptions {
|
|
3
|
+
/**
|
|
4
|
+
* - **EN** Whether to execute at the start of the wait period. Default is `false`.
|
|
5
|
+
* - **CN** 是否在等待周期开始时执行,默认值为 `false`
|
|
6
|
+
*/
|
|
7
|
+
leading?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* - **EN** Regular debounce interval in milliseconds. Default is `0`, meaning no debounce.
|
|
10
|
+
* - **CN** 常规防抖间隔 (ms),默认值为 `0`, 表示不进行防抖
|
|
11
|
+
*/
|
|
12
|
+
wait?: number;
|
|
13
|
+
/**
|
|
14
|
+
* - **EN** Maximum wait time in milliseconds. Default is `0`, meaning no maximum wait.
|
|
15
|
+
* - **CN** 最大等待时间 (ms),默认值为 `0`, 表示不限制最大等待时间
|
|
16
|
+
*/
|
|
17
|
+
maxWait?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* - **EN** Debounce Hook with dual trigger mechanisms:
|
|
21
|
+
*
|
|
22
|
+
* 1. Traditional debounce: Executes after a specified interval without new calls.
|
|
23
|
+
* 2. Max wait: Forces execution after exceeding a specified maximum wait time.
|
|
24
|
+
* - **CN** 防抖 Hook:具有两种触发机制:
|
|
25
|
+
*
|
|
26
|
+
* 1. 传统防抖:等待指定时间内无新调用后执行
|
|
27
|
+
* 2. 最大等待:超过指定最大等待时间后强制执行
|
|
28
|
+
*
|
|
29
|
+
* @param fn The function to debounce | 需要防抖的函数
|
|
30
|
+
* @param deps Dependency array, re-creates the debounced function when dependencies change |
|
|
31
|
+
* 依赖项数组,当依赖变化时重新创建防抖函数
|
|
32
|
+
* @param options Configuration options | 配置选项
|
|
33
|
+
*
|
|
34
|
+
* @returns The debounced function | 防抖处理后的函数
|
|
35
|
+
*/
|
|
36
|
+
declare function useDebounce<T extends (...args: any[]) => unknown>(fn: T, deps: React.DependencyList, options?: UseDebounceOptions): (...args: Parameters<T>) => void;
|
|
37
|
+
export default useDebounce;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
2
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
4
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
5
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
6
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
7
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
8
|
+
/**
|
|
9
|
+
* - **EN** Debounce Hook with dual trigger mechanisms:
|
|
10
|
+
*
|
|
11
|
+
* 1. Traditional debounce: Executes after a specified interval without new calls.
|
|
12
|
+
* 2. Max wait: Forces execution after exceeding a specified maximum wait time.
|
|
13
|
+
* - **CN** 防抖 Hook:具有两种触发机制:
|
|
14
|
+
*
|
|
15
|
+
* 1. 传统防抖:等待指定时间内无新调用后执行
|
|
16
|
+
* 2. 最大等待:超过指定最大等待时间后强制执行
|
|
17
|
+
*
|
|
18
|
+
* @param fn The function to debounce | 需要防抖的函数
|
|
19
|
+
* @param deps Dependency array, re-creates the debounced function when dependencies change |
|
|
20
|
+
* 依赖项数组,当依赖变化时重新创建防抖函数
|
|
21
|
+
* @param options Configuration options | 配置选项
|
|
22
|
+
*
|
|
23
|
+
* @returns The debounced function | 防抖处理后的函数
|
|
24
|
+
*/
|
|
25
|
+
function useDebounce(fn, deps) {
|
|
26
|
+
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
|
|
27
|
+
var _options$wait = options.wait,
|
|
28
|
+
wait = _options$wait === void 0 ? 0 : _options$wait,
|
|
29
|
+
_options$maxWait = options.maxWait,
|
|
30
|
+
maxWait = _options$maxWait === void 0 ? 0 : _options$maxWait,
|
|
31
|
+
_options$leading = options.leading,
|
|
32
|
+
leading = _options$leading === void 0 ? false : _options$leading;
|
|
33
|
+
var timeoutRef = useRef(null);
|
|
34
|
+
var fnRef = useRef(fn);
|
|
35
|
+
var lastExecutedTimeRef = useRef(0); // The last execution timestamp
|
|
36
|
+
var lastArgsRef = useRef([]); // The last call arguments
|
|
37
|
+
|
|
38
|
+
// Update the latest function reference
|
|
39
|
+
useEffect(function () {
|
|
40
|
+
fnRef.current = fn;
|
|
41
|
+
}, [fn]);
|
|
42
|
+
|
|
43
|
+
// Cleanup on unmount
|
|
44
|
+
useEffect(function () {
|
|
45
|
+
return function () {
|
|
46
|
+
if (timeoutRef.current) {
|
|
47
|
+
clearTimeout(timeoutRef.current);
|
|
48
|
+
timeoutRef.current = null;
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
}, []);
|
|
52
|
+
|
|
53
|
+
// The actual execution function
|
|
54
|
+
var executeFunction = useCallback(function () {
|
|
55
|
+
timeoutRef.current = null;
|
|
56
|
+
lastExecutedTimeRef.current = Date.now();
|
|
57
|
+
fnRef.current.apply(fnRef, _toConsumableArray(lastArgsRef.current));
|
|
58
|
+
}, []);
|
|
59
|
+
|
|
60
|
+
// The debounced function
|
|
61
|
+
return useCallback(function () {
|
|
62
|
+
var now = Date.now();
|
|
63
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
64
|
+
args[_key] = arguments[_key];
|
|
65
|
+
}
|
|
66
|
+
lastArgsRef.current = args;
|
|
67
|
+
|
|
68
|
+
// If leading is true and it's the first call, execute immediately
|
|
69
|
+
if (leading && timeoutRef.current === null && now - lastExecutedTimeRef.current >= wait) {
|
|
70
|
+
executeFunction();
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// 1. Clear the existing timer
|
|
75
|
+
if (timeoutRef.current !== null) {
|
|
76
|
+
clearTimeout(timeoutRef.current);
|
|
77
|
+
timeoutRef.current = null;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// 2. Check if the maximum wait time has been exceeded, if so, execute immediately
|
|
81
|
+
if (maxWait > 0 && now - lastExecutedTimeRef.current >= maxWait) {
|
|
82
|
+
executeFunction();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 3. Set a new debounce timer
|
|
87
|
+
timeoutRef.current = setTimeout(executeFunction, wait);
|
|
88
|
+
}, // eslint-disable-next-line @tiny-codes/react-hooks/exhaustive-deps
|
|
89
|
+
[wait, maxWait, executeFunction, leading].concat(_toConsumableArray(deps)));
|
|
90
|
+
}
|
|
91
|
+
export default useDebounce;
|
|
92
|
+
//# sourceMappingURL=useDebounce.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallback","useEffect","useRef","useDebounce","fn","deps","options","arguments","length","undefined","_options$wait","wait","_options$maxWait","maxWait","_options$leading","leading","timeoutRef","fnRef","lastExecutedTimeRef","lastArgsRef","current","clearTimeout","executeFunction","Date","now","apply","_toConsumableArray","_len","args","Array","_key","setTimeout","concat"],"sources":["../../src/hooks/useDebounce.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from 'react';\n\nexport interface UseDebounceOptions {\n /**\n * - **EN** Whether to execute at the start of the wait period. Default is `false`.\n * - **CN** 是否在等待周期开始时执行,默认值为 `false`\n */\n leading?: boolean;\n /**\n * - **EN** Regular debounce interval in milliseconds. Default is `0`, meaning no debounce.\n * - **CN** 常规防抖间隔 (ms),默认值为 `0`, 表示不进行防抖\n */\n wait?: number;\n /**\n * - **EN** Maximum wait time in milliseconds. Default is `0`, meaning no maximum wait.\n * - **CN** 最大等待时间 (ms),默认值为 `0`, 表示不限制最大等待时间\n */\n maxWait?: number;\n}\n/**\n * - **EN** Debounce Hook with dual trigger mechanisms:\n *\n * 1. Traditional debounce: Executes after a specified interval without new calls.\n * 2. Max wait: Forces execution after exceeding a specified maximum wait time.\n * - **CN** 防抖 Hook:具有两种触发机制:\n *\n * 1. 传统防抖:等待指定时间内无新调用后执行\n * 2. 最大等待:超过指定最大等待时间后强制执行\n *\n * @param fn The function to debounce | 需要防抖的函数\n * @param deps Dependency array, re-creates the debounced function when dependencies change |\n * 依赖项数组,当依赖变化时重新创建防抖函数\n * @param options Configuration options | 配置选项\n *\n * @returns The debounced function | 防抖处理后的函数\n */\nfunction useDebounce<T extends (...args: any[]) => unknown>(\n fn: T,\n deps: React.DependencyList,\n options: UseDebounceOptions = {}\n): (...args: Parameters<T>) => void {\n const { wait = 0, maxWait = 0, leading = false } = options;\n const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n const fnRef = useRef<T>(fn);\n const lastExecutedTimeRef = useRef<number>(0); // The last execution timestamp\n const lastArgsRef = useRef<unknown[]>([]); // The last call arguments\n\n // Update the latest function reference\n useEffect(() => {\n fnRef.current = fn;\n }, [fn]);\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n };\n }, []);\n\n // The actual execution function\n const executeFunction = useCallback(() => {\n timeoutRef.current = null;\n lastExecutedTimeRef.current = Date.now();\n fnRef.current(...lastArgsRef.current);\n }, []);\n\n // The debounced function\n return useCallback(\n (...args: Parameters<T>) => {\n const now = Date.now();\n lastArgsRef.current = args;\n\n // If leading is true and it's the first call, execute immediately\n if (leading && timeoutRef.current === null && now - lastExecutedTimeRef.current >= wait) {\n executeFunction();\n return;\n }\n\n // 1. Clear the existing timer\n if (timeoutRef.current !== null) {\n clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n\n // 2. Check if the maximum wait time has been exceeded, if so, execute immediately\n if (maxWait > 0 && now - lastExecutedTimeRef.current >= maxWait) {\n executeFunction();\n return;\n }\n\n // 3. Set a new debounce timer\n timeoutRef.current = setTimeout(executeFunction, wait);\n },\n // eslint-disable-next-line @tiny-codes/react-hooks/exhaustive-deps\n [wait, maxWait, executeFunction, leading, ...deps]\n );\n}\n\nexport default useDebounce;\n"],"mappings":"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"}
|
package/lib/hooks/index.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
export { default as useAudioPlayer } from './useAudioPlayer';
|
|
2
|
+
export { default as useDebounce } from './useDebounce';
|
|
2
3
|
export { default as useRefValue } from './useRefValue';
|
|
3
4
|
export { default as useRefFunction } from './useRefFunction';
|
|
4
5
|
export * from './useSSE';
|
|
5
6
|
export { default as useSSE } from './useSSE';
|
|
6
7
|
export * from './useStompSocket';
|
|
7
8
|
export { default as useStompSocket } from './useStompSocket';
|
|
9
|
+
export * from './useUserMedia';
|
|
10
|
+
export { default as useUserMedia } from './useUserMedia';
|
|
8
11
|
export type { ValidatorRuleMap } from './useValidators';
|
|
9
12
|
export { default as useValidators } from './useValidators';
|
|
10
13
|
export { default as useValidator } from './useValidator';
|
|
11
14
|
export type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';
|
|
12
15
|
export { default as useValidatorBuilder } from './useValidatorBuilder';
|
|
13
|
-
export * from './useUserMedia';
|
|
14
|
-
export { default as useUserMedia } from './useUserMedia';
|
package/lib/hooks/index.js
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var hooks_exports = {};
|
|
32
32
|
__export(hooks_exports, {
|
|
33
33
|
useAudioPlayer: () => import_useAudioPlayer.default,
|
|
34
|
+
useDebounce: () => import_useDebounce.default,
|
|
34
35
|
useRefFunction: () => import_useRefFunction.default,
|
|
35
36
|
useRefValue: () => import_useRefValue.default,
|
|
36
37
|
useSSE: () => import_useSSE.default,
|
|
@@ -42,20 +43,22 @@ __export(hooks_exports, {
|
|
|
42
43
|
});
|
|
43
44
|
module.exports = __toCommonJS(hooks_exports);
|
|
44
45
|
var import_useAudioPlayer = __toESM(require("./useAudioPlayer"));
|
|
46
|
+
var import_useDebounce = __toESM(require("./useDebounce"));
|
|
45
47
|
var import_useRefValue = __toESM(require("./useRefValue"));
|
|
46
48
|
var import_useRefFunction = __toESM(require("./useRefFunction"));
|
|
47
49
|
__reExport(hooks_exports, require("./useSSE"), module.exports);
|
|
48
50
|
var import_useSSE = __toESM(require("./useSSE"));
|
|
49
51
|
__reExport(hooks_exports, require("./useStompSocket"), module.exports);
|
|
50
52
|
var import_useStompSocket = __toESM(require("./useStompSocket"));
|
|
53
|
+
__reExport(hooks_exports, require("./useUserMedia"), module.exports);
|
|
54
|
+
var import_useUserMedia = __toESM(require("./useUserMedia"));
|
|
51
55
|
var import_useValidators = __toESM(require("./useValidators"));
|
|
52
56
|
var import_useValidator = __toESM(require("./useValidator"));
|
|
53
57
|
var import_useValidatorBuilder = __toESM(require("./useValidatorBuilder"));
|
|
54
|
-
__reExport(hooks_exports, require("./useUserMedia"), module.exports);
|
|
55
|
-
var import_useUserMedia = __toESM(require("./useUserMedia"));
|
|
56
58
|
// Annotate the CommonJS export names for ESM import in node:
|
|
57
59
|
0 && (module.exports = {
|
|
58
60
|
useAudioPlayer,
|
|
61
|
+
useDebounce,
|
|
59
62
|
useRefFunction,
|
|
60
63
|
useRefValue,
|
|
61
64
|
useSSE,
|
package/lib/hooks/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/hooks/index.ts"],
|
|
4
|
-
"sourcesContent": ["export { default as useAudioPlayer } from './useAudioPlayer';\nexport { default as useRefValue } from './useRefValue';\nexport { default as useRefFunction } from './useRefFunction';\nexport * from './useSSE';\nexport { default as useSSE } from './useSSE';\nexport * from './useStompSocket';\nexport { default as useStompSocket } from './useStompSocket';\nexport type { ValidatorRuleMap } from './useValidators';\nexport { default as useValidators } from './useValidators';\nexport { default as useValidator } from './useValidator';\nexport type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';\nexport { default as useValidatorBuilder } from './useValidatorBuilder';\
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA0C;AAC1C,yBAAuC;AACvC,4BAA0C;AAC1C,0BAAc,
|
|
4
|
+
"sourcesContent": ["export { default as useAudioPlayer } from './useAudioPlayer';\nexport { default as useDebounce } from './useDebounce';\nexport { default as useRefValue } from './useRefValue';\nexport { default as useRefFunction } from './useRefFunction';\nexport * from './useSSE';\nexport { default as useSSE } from './useSSE';\nexport * from './useStompSocket';\nexport { default as useStompSocket } from './useStompSocket';\nexport * from './useUserMedia';\nexport { default as useUserMedia } from './useUserMedia';\nexport type { ValidatorRuleMap } from './useValidators';\nexport { default as useValidators } from './useValidators';\nexport { default as useValidator } from './useValidator';\nexport type { Validator, RuleRegExpFlags, ValidatorRule, BuilderOptions } from './useValidatorBuilder';\nexport { default as useValidatorBuilder } from './useValidatorBuilder';\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,4BAA0C;AAC1C,yBAAuC;AACvC,yBAAuC;AACvC,4BAA0C;AAC1C,0BAAc,qBAJd;AAKA,oBAAkC;AAClC,0BAAc,6BANd;AAOA,4BAA0C;AAC1C,0BAAc,2BARd;AASA,0BAAwC;AAExC,2BAAyC;AACzC,0BAAwC;AAExC,iCAA+C;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface UseDebounceOptions {
|
|
3
|
+
/**
|
|
4
|
+
* - **EN** Whether to execute at the start of the wait period. Default is `false`.
|
|
5
|
+
* - **CN** 是否在等待周期开始时执行,默认值为 `false`
|
|
6
|
+
*/
|
|
7
|
+
leading?: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* - **EN** Regular debounce interval in milliseconds. Default is `0`, meaning no debounce.
|
|
10
|
+
* - **CN** 常规防抖间隔 (ms),默认值为 `0`, 表示不进行防抖
|
|
11
|
+
*/
|
|
12
|
+
wait?: number;
|
|
13
|
+
/**
|
|
14
|
+
* - **EN** Maximum wait time in milliseconds. Default is `0`, meaning no maximum wait.
|
|
15
|
+
* - **CN** 最大等待时间 (ms),默认值为 `0`, 表示不限制最大等待时间
|
|
16
|
+
*/
|
|
17
|
+
maxWait?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* - **EN** Debounce Hook with dual trigger mechanisms:
|
|
21
|
+
*
|
|
22
|
+
* 1. Traditional debounce: Executes after a specified interval without new calls.
|
|
23
|
+
* 2. Max wait: Forces execution after exceeding a specified maximum wait time.
|
|
24
|
+
* - **CN** 防抖 Hook:具有两种触发机制:
|
|
25
|
+
*
|
|
26
|
+
* 1. 传统防抖:等待指定时间内无新调用后执行
|
|
27
|
+
* 2. 最大等待:超过指定最大等待时间后强制执行
|
|
28
|
+
*
|
|
29
|
+
* @param fn The function to debounce | 需要防抖的函数
|
|
30
|
+
* @param deps Dependency array, re-creates the debounced function when dependencies change |
|
|
31
|
+
* 依赖项数组,当依赖变化时重新创建防抖函数
|
|
32
|
+
* @param options Configuration options | 配置选项
|
|
33
|
+
*
|
|
34
|
+
* @returns The debounced function | 防抖处理后的函数
|
|
35
|
+
*/
|
|
36
|
+
declare function useDebounce<T extends (...args: any[]) => unknown>(fn: T, deps: React.DependencyList, options?: UseDebounceOptions): (...args: Parameters<T>) => void;
|
|
37
|
+
export default useDebounce;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/hooks/useDebounce.ts
|
|
20
|
+
var useDebounce_exports = {};
|
|
21
|
+
__export(useDebounce_exports, {
|
|
22
|
+
default: () => useDebounce_default
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(useDebounce_exports);
|
|
25
|
+
var import_react = require("react");
|
|
26
|
+
function useDebounce(fn, deps, options = {}) {
|
|
27
|
+
const { wait = 0, maxWait = 0, leading = false } = options;
|
|
28
|
+
const timeoutRef = (0, import_react.useRef)(null);
|
|
29
|
+
const fnRef = (0, import_react.useRef)(fn);
|
|
30
|
+
const lastExecutedTimeRef = (0, import_react.useRef)(0);
|
|
31
|
+
const lastArgsRef = (0, import_react.useRef)([]);
|
|
32
|
+
(0, import_react.useEffect)(() => {
|
|
33
|
+
fnRef.current = fn;
|
|
34
|
+
}, [fn]);
|
|
35
|
+
(0, import_react.useEffect)(() => {
|
|
36
|
+
return () => {
|
|
37
|
+
if (timeoutRef.current) {
|
|
38
|
+
clearTimeout(timeoutRef.current);
|
|
39
|
+
timeoutRef.current = null;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
}, []);
|
|
43
|
+
const executeFunction = (0, import_react.useCallback)(() => {
|
|
44
|
+
timeoutRef.current = null;
|
|
45
|
+
lastExecutedTimeRef.current = Date.now();
|
|
46
|
+
fnRef.current(...lastArgsRef.current);
|
|
47
|
+
}, []);
|
|
48
|
+
return (0, import_react.useCallback)(
|
|
49
|
+
(...args) => {
|
|
50
|
+
const now = Date.now();
|
|
51
|
+
lastArgsRef.current = args;
|
|
52
|
+
if (leading && timeoutRef.current === null && now - lastExecutedTimeRef.current >= wait) {
|
|
53
|
+
executeFunction();
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (timeoutRef.current !== null) {
|
|
57
|
+
clearTimeout(timeoutRef.current);
|
|
58
|
+
timeoutRef.current = null;
|
|
59
|
+
}
|
|
60
|
+
if (maxWait > 0 && now - lastExecutedTimeRef.current >= maxWait) {
|
|
61
|
+
executeFunction();
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
timeoutRef.current = setTimeout(executeFunction, wait);
|
|
65
|
+
},
|
|
66
|
+
// eslint-disable-next-line @tiny-codes/react-hooks/exhaustive-deps
|
|
67
|
+
[wait, maxWait, executeFunction, leading, ...deps]
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
var useDebounce_default = useDebounce;
|
|
71
|
+
//# sourceMappingURL=useDebounce.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/hooks/useDebounce.ts"],
|
|
4
|
+
"sourcesContent": ["import { useCallback, useEffect, useRef } from 'react';\n\nexport interface UseDebounceOptions {\n /**\n * - **EN** Whether to execute at the start of the wait period. Default is `false`.\n * - **CN** 是否在等待周期开始时执行,默认值为 `false`\n */\n leading?: boolean;\n /**\n * - **EN** Regular debounce interval in milliseconds. Default is `0`, meaning no debounce.\n * - **CN** 常规防抖间隔 (ms),默认值为 `0`, 表示不进行防抖\n */\n wait?: number;\n /**\n * - **EN** Maximum wait time in milliseconds. Default is `0`, meaning no maximum wait.\n * - **CN** 最大等待时间 (ms),默认值为 `0`, 表示不限制最大等待时间\n */\n maxWait?: number;\n}\n/**\n * - **EN** Debounce Hook with dual trigger mechanisms:\n *\n * 1. Traditional debounce: Executes after a specified interval without new calls.\n * 2. Max wait: Forces execution after exceeding a specified maximum wait time.\n * - **CN** 防抖 Hook:具有两种触发机制:\n *\n * 1. 传统防抖:等待指定时间内无新调用后执行\n * 2. 最大等待:超过指定最大等待时间后强制执行\n *\n * @param fn The function to debounce | 需要防抖的函数\n * @param deps Dependency array, re-creates the debounced function when dependencies change |\n * 依赖项数组,当依赖变化时重新创建防抖函数\n * @param options Configuration options | 配置选项\n *\n * @returns The debounced function | 防抖处理后的函数\n */\nfunction useDebounce<T extends (...args: any[]) => unknown>(\n fn: T,\n deps: React.DependencyList,\n options: UseDebounceOptions = {}\n): (...args: Parameters<T>) => void {\n const { wait = 0, maxWait = 0, leading = false } = options;\n const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n const fnRef = useRef<T>(fn);\n const lastExecutedTimeRef = useRef<number>(0); // The last execution timestamp\n const lastArgsRef = useRef<unknown[]>([]); // The last call arguments\n\n // Update the latest function reference\n useEffect(() => {\n fnRef.current = fn;\n }, [fn]);\n\n // Cleanup on unmount\n useEffect(() => {\n return () => {\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n };\n }, []);\n\n // The actual execution function\n const executeFunction = useCallback(() => {\n timeoutRef.current = null;\n lastExecutedTimeRef.current = Date.now();\n fnRef.current(...lastArgsRef.current);\n }, []);\n\n // The debounced function\n return useCallback(\n (...args: Parameters<T>) => {\n const now = Date.now();\n lastArgsRef.current = args;\n\n // If leading is true and it's the first call, execute immediately\n if (leading && timeoutRef.current === null && now - lastExecutedTimeRef.current >= wait) {\n executeFunction();\n return;\n }\n\n // 1. Clear the existing timer\n if (timeoutRef.current !== null) {\n clearTimeout(timeoutRef.current);\n timeoutRef.current = null;\n }\n\n // 2. Check if the maximum wait time has been exceeded, if so, execute immediately\n if (maxWait > 0 && now - lastExecutedTimeRef.current >= maxWait) {\n executeFunction();\n return;\n }\n\n // 3. Set a new debounce timer\n timeoutRef.current = setTimeout(executeFunction, wait);\n },\n // eslint-disable-next-line @tiny-codes/react-hooks/exhaustive-deps\n [wait, maxWait, executeFunction, leading, ...deps]\n );\n}\n\nexport default useDebounce;\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA+C;AAoC/C,SAAS,YACP,IACA,MACA,UAA8B,CAAC,GACG;AAClC,QAAM,EAAE,OAAO,GAAG,UAAU,GAAG,UAAU,MAAM,IAAI;AACnD,QAAM,iBAAa,qBAA6C,IAAI;AACpE,QAAM,YAAQ,qBAAU,EAAE;AAC1B,QAAM,0BAAsB,qBAAe,CAAC;AAC5C,QAAM,kBAAc,qBAAkB,CAAC,CAAC;AAGxC,8BAAU,MAAM;AACd,UAAM,UAAU;AAAA,EAClB,GAAG,CAAC,EAAE,CAAC;AAGP,8BAAU,MAAM;AACd,WAAO,MAAM;AACX,UAAI,WAAW,SAAS;AACtB,qBAAa,WAAW,OAAO;AAC/B,mBAAW,UAAU;AAAA,MACvB;AAAA,IACF;AAAA,EACF,GAAG,CAAC,CAAC;AAGL,QAAM,sBAAkB,0BAAY,MAAM;AACxC,eAAW,UAAU;AACrB,wBAAoB,UAAU,KAAK,IAAI;AACvC,UAAM,QAAQ,GAAG,YAAY,OAAO;AAAA,EACtC,GAAG,CAAC,CAAC;AAGL,aAAO;AAAA,IACL,IAAI,SAAwB;AAC1B,YAAM,MAAM,KAAK,IAAI;AACrB,kBAAY,UAAU;AAGtB,UAAI,WAAW,WAAW,YAAY,QAAQ,MAAM,oBAAoB,WAAW,MAAM;AACvF,wBAAgB;AAChB;AAAA,MACF;AAGA,UAAI,WAAW,YAAY,MAAM;AAC/B,qBAAa,WAAW,OAAO;AAC/B,mBAAW,UAAU;AAAA,MACvB;AAGA,UAAI,UAAU,KAAK,MAAM,oBAAoB,WAAW,SAAS;AAC/D,wBAAgB;AAChB;AAAA,MACF;AAGA,iBAAW,UAAU,WAAW,iBAAiB,IAAI;AAAA,IACvD;AAAA;AAAA,IAEA,CAAC,MAAM,SAAS,iBAAiB,SAAS,GAAG,IAAI;AAAA,EACnD;AACF;AAEA,IAAO,sBAAQ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|