@nu-art/ts-common 0.204.99 → 0.204.100
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/package.json +1 -1
- package/utils/ui-tools.d.ts +1 -0
- package/utils/ui-tools.js +36 -1
package/package.json
CHANGED
package/utils/ui-tools.d.ts
CHANGED
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
* // or at least once after 1000ms, regardless of continuous calls.
|
|
29
29
|
*/
|
|
30
30
|
export declare const debounce: <Args extends any[]>(func: (...params: Args) => any | Promise<any>, timeout?: number, maxTimeout?: number) => (...args: Args) => void;
|
|
31
|
+
export declare const queuedDebounce: <Args extends any[]>(func: (...params: Args) => any | Promise<any>, timeout?: number, maxTimeout?: number) => (...args: Args) => void;
|
|
31
32
|
export type AwaitedDebounceInstance<Args extends any[], ReturnValue> = (...args: Args) => Promise<ReturnValue | undefined>;
|
|
32
33
|
type DebounceParams<Args extends any[], ReturnValue = any> = {
|
|
33
34
|
func: (...params: Args) => Promise<ReturnValue>;
|
package/utils/ui-tools.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.awaitedDebounce = exports.debounce = void 0;
|
|
3
|
+
exports.awaitedDebounce = exports.queuedDebounce = exports.debounce = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Creates a debounced function that delays invoking the provided function until after a specified
|
|
6
6
|
* timeout has elapsed since the last time the debounced function was called. It also ensures that
|
|
@@ -49,6 +49,41 @@ const debounce = (func, timeout = 500, maxTimeout = 1000) => {
|
|
|
49
49
|
};
|
|
50
50
|
};
|
|
51
51
|
exports.debounce = debounce;
|
|
52
|
+
const queuedDebounce = (func, timeout = 500, maxTimeout = 1000) => {
|
|
53
|
+
let timer;
|
|
54
|
+
let defaultTimer;
|
|
55
|
+
let running = false;
|
|
56
|
+
const debounceFunc = (...args) => {
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
timer = setTimeout(async () => {
|
|
59
|
+
clearTimeout(defaultTimer);
|
|
60
|
+
defaultTimer = undefined;
|
|
61
|
+
await execFunc(...args);
|
|
62
|
+
}, timeout);
|
|
63
|
+
if (!defaultTimer) {
|
|
64
|
+
defaultTimer = setTimeout(async () => {
|
|
65
|
+
defaultTimer = undefined;
|
|
66
|
+
await execFunc(...args);
|
|
67
|
+
}, maxTimeout);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const execFunc = async (...args) => {
|
|
71
|
+
if (running) {
|
|
72
|
+
clearTimeout(defaultTimer);
|
|
73
|
+
defaultTimer = undefined;
|
|
74
|
+
return debounceFunc(...args);
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
running = true;
|
|
78
|
+
await func(...args);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
running = false;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return debounceFunc;
|
|
85
|
+
};
|
|
86
|
+
exports.queuedDebounce = queuedDebounce;
|
|
52
87
|
const awaitedDebounce = (params) => {
|
|
53
88
|
const timers = {};
|
|
54
89
|
const _clearTimeout = (timer) => {
|