@alwatr/debounce 1.0.0-rc.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/CHANGELOG.md +18 -0
- package/LICENSE +373 -0
- package/README.md +468 -0
- package/dist/debounce.d.ts +133 -0
- package/dist/debounce.d.ts.map +1 -0
- package/dist/main.cjs +145 -0
- package/dist/main.cjs.map +7 -0
- package/dist/main.d.ts +3 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.mjs +118 -0
- package/dist/main.mjs.map +7 -0
- package/dist/type.d.ts +41 -0
- package/dist/type.d.ts.map +1 -0
- package/package.json +81 -0
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/* @alwatr/debounce v1.0.0-rc.0 */
|
|
2
|
+
"use strict";
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
|
|
21
|
+
// src/main.ts
|
|
22
|
+
var main_exports = {};
|
|
23
|
+
__export(main_exports, {
|
|
24
|
+
Debouncer: () => Debouncer,
|
|
25
|
+
createDebouncer: () => createDebouncer
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(main_exports);
|
|
28
|
+
|
|
29
|
+
// src/debounce.ts
|
|
30
|
+
var Debouncer = class {
|
|
31
|
+
constructor(config__) {
|
|
32
|
+
this.config__ = config__;
|
|
33
|
+
this.config__.trailing ??= true;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Checks if there is a pending execution scheduled.
|
|
37
|
+
* Returns true if a timer is active, indicating a debounced call is waiting.
|
|
38
|
+
*/
|
|
39
|
+
get isPending() {
|
|
40
|
+
return this.timerId__ !== void 0;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Triggers the debounced function with the stored `thisContext`.
|
|
44
|
+
* @param args The arguments to pass to the callback.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* const debouncer = new Debouncer({
|
|
49
|
+
* callback: (value: number) => console.log('Value:', value),
|
|
50
|
+
* delay: 500,
|
|
51
|
+
* });
|
|
52
|
+
* debouncer.trigger(42); // Logs after 500ms if not triggered again
|
|
53
|
+
*
|
|
54
|
+
* // Edge case: Rapid triggers only execute the last one
|
|
55
|
+
* debouncer.trigger(1);
|
|
56
|
+
* debouncer.trigger(2); // Only 2 will execute after delay
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
trigger(...args) {
|
|
60
|
+
this.lastArgs__ = args;
|
|
61
|
+
const wasPending = this.isPending;
|
|
62
|
+
if (wasPending) {
|
|
63
|
+
clearTimeout(this.timerId__);
|
|
64
|
+
}
|
|
65
|
+
if (this.config__.leading === true && !wasPending) {
|
|
66
|
+
this.invoke__();
|
|
67
|
+
}
|
|
68
|
+
this.timerId__ = setTimeout(() => {
|
|
69
|
+
if (this.config__.trailing === true && wasPending) {
|
|
70
|
+
this.invoke__();
|
|
71
|
+
}
|
|
72
|
+
this.cleanup__();
|
|
73
|
+
}, this.config__.delay);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Cancels any pending debounced execution and cleans up internal state.
|
|
77
|
+
* Useful for stopping execution when the operation is no longer needed (e.g., component unmount).
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```typescript
|
|
81
|
+
* const debouncer = new Debouncer({
|
|
82
|
+
* callback: () => console.log('Executed'),
|
|
83
|
+
* delay: 1000,
|
|
84
|
+
* });
|
|
85
|
+
* debouncer.trigger();
|
|
86
|
+
* debouncer.cancel(); // Prevents execution
|
|
87
|
+
*
|
|
88
|
+
* // Note: After cancel, isPending becomes false
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
cancel() {
|
|
92
|
+
if (this.isPending) {
|
|
93
|
+
clearTimeout(this.timerId__);
|
|
94
|
+
}
|
|
95
|
+
this.cleanup__();
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Cleans up internal state by deleting timer and arguments.
|
|
99
|
+
*/
|
|
100
|
+
cleanup__() {
|
|
101
|
+
delete this.timerId__;
|
|
102
|
+
delete this.lastArgs__;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Immediately executes the pending function if one exists.
|
|
106
|
+
* Bypasses the delay and cleans up state. If no pending call, does nothing.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* ```typescript
|
|
110
|
+
* const debouncer = new Debouncer({
|
|
111
|
+
* callback: () => console.log('Flushed'),
|
|
112
|
+
* delay: 1000,
|
|
113
|
+
* });
|
|
114
|
+
* debouncer.trigger();
|
|
115
|
+
* setTimeout(() => debouncer.flush(), 500); // Executes immediately
|
|
116
|
+
*
|
|
117
|
+
* // Edge case: Flush after cancel does nothing
|
|
118
|
+
* debouncer.cancel();
|
|
119
|
+
* debouncer.flush(); // No execution
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
flush() {
|
|
123
|
+
if (this.isPending) {
|
|
124
|
+
this.cancel();
|
|
125
|
+
this.invoke__();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* The core execution logic.
|
|
130
|
+
*/
|
|
131
|
+
invoke__() {
|
|
132
|
+
if (this.lastArgs__) {
|
|
133
|
+
this.config__.callback.apply(this.config__.thisContext, this.lastArgs__);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
function createDebouncer(config) {
|
|
138
|
+
return new Debouncer(config);
|
|
139
|
+
}
|
|
140
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
141
|
+
0 && (module.exports = {
|
|
142
|
+
Debouncer,
|
|
143
|
+
createDebouncer
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=main.cjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/main.ts", "../src/debounce.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './debounce.js';\nexport type * from './type.js';\n", "import type {DebouncerConfig} from './type.ts';\n\n/**\n * A powerful and type-safe Debouncer class.\n * \n * It encapsulates the debouncing logic, state, and provides a rich control API.\n * Debouncing delays function execution until after a specified delay has passed since the last invocation.\n * Useful for optimizing performance in scenarios like search inputs, resize events, or API calls.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: (text: string) => console.log('Searching:', text),\n * delay: 300,\n * leading: false,\n * trailing: true,\n * });\n * \n * // Debounce search input\n * debouncer.trigger('hello');\n * debouncer.trigger('hello world'); // Only 'hello world' will log after 300ms\n * \n * // Advanced: With leading edge\n * const leadingDebouncer = new Debouncer({\n * callback: () => console.log('Immediate and delayed'),\n * delay: 500,\n * leading: true,\n * trailing: true,\n * });\n * leadingDebouncer.trigger(); // Logs immediately, then again after 500ms if not cancelled\n * ```\n */\nexport class Debouncer<F extends AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private lastArgs__?: Parameters<F>;\n\n public constructor(private readonly config__: DebouncerConfig<F>) {\n this.config__.trailing ??= true;\n }\n\n /**\n * Checks if there is a pending execution scheduled.\n * Returns true if a timer is active, indicating a debounced call is waiting.\n */\n public get isPending(): boolean {\n return this.timerId__ !== undefined;\n }\n\n /**\n * Triggers the debounced function with the stored `thisContext`.\n * @param args The arguments to pass to the callback.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: (value: number) => console.log('Value:', value),\n * delay: 500,\n * });\n * debouncer.trigger(42); // Logs after 500ms if not triggered again\n * \n * // Edge case: Rapid triggers only execute the last one\n * debouncer.trigger(1);\n * debouncer.trigger(2); // Only 2 will execute after delay\n * ```\n */\n public trigger(...args: Parameters<F>): void {\n this.lastArgs__ = args;\n const wasPending = this.isPending;\n\n if (wasPending) {\n clearTimeout(this.timerId__!);\n }\n\n if (this.config__.leading === true && !wasPending) {\n this.invoke__();\n }\n\n this.timerId__ = setTimeout(() => {\n if (this.config__.trailing === true && wasPending) {\n this.invoke__();\n }\n this.cleanup__();\n }, this.config__.delay);\n }\n\n /**\n * Cancels any pending debounced execution and cleans up internal state.\n * Useful for stopping execution when the operation is no longer needed (e.g., component unmount).\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: () => console.log('Executed'),\n * delay: 1000,\n * });\n * debouncer.trigger();\n * debouncer.cancel(); // Prevents execution\n * \n * // Note: After cancel, isPending becomes false\n * ```\n */\n public cancel(): void {\n if (this.isPending) {\n clearTimeout(this.timerId__!);\n }\n this.cleanup__();\n }\n\n /**\n * Cleans up internal state by deleting timer and arguments.\n */\n private cleanup__(): void {\n delete this.timerId__;\n delete this.lastArgs__;\n }\n\n /**\n * Immediately executes the pending function if one exists.\n * Bypasses the delay and cleans up state. If no pending call, does nothing.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: () => console.log('Flushed'),\n * delay: 1000,\n * });\n * debouncer.trigger();\n * setTimeout(() => debouncer.flush(), 500); // Executes immediately\n * \n * // Edge case: Flush after cancel does nothing\n * debouncer.cancel();\n * debouncer.flush(); // No execution\n * ```\n */\n public flush(): void {\n if (this.isPending) {\n this.cancel();\n this.invoke__();\n }\n }\n\n /**\n * The core execution logic.\n */\n private invoke__(): void {\n if (this.lastArgs__) {\n // `thisContext` is now read directly from the stored config.\n this.config__.callback.apply(this.config__.thisContext, this.lastArgs__);\n }\n }\n}\n\n/**\n * Factory function for creating a Debouncer instance for better type inference.\n * @param config Configuration for the debouncer.\n * \n * @example\n * ```typescript\n * const debouncer = createDebouncer({\n * callback: (text: string) => console.log('Searching:', text),\n * delay: 300,\n * leading: false,\n * trailing: true,\n * });\n * \n * // Debounce search input\n * debouncer.trigger('hello');\n * debouncer.trigger('hello world'); // Only 'hello world' will log after 300ms\n * \n * // With custom thisContext\n * const obj = { log: (msg: string) => console.log('Obj:', msg) };\n * const debouncerWithContext = createDebouncer({\n * callback: obj.log,\n * thisContext: obj,\n * delay: 200,\n * });\n * debouncerWithContext.trigger('test'); // Logs 'Obj: test'\n * ```\n */\nexport function createDebouncer<F extends AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACgCO,IAAM,YAAN,MAAuC;AAAA,EAIrC,YAA6B,UAA8B;AAA9B;AAClC,SAAK,SAAS,aAAa;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,YAAqB;AAC9B,WAAO,KAAK,cAAc;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBO,WAAW,MAA2B;AAC3C,SAAK,aAAa;AAClB,UAAM,aAAa,KAAK;AAExB,QAAI,YAAY;AACd,mBAAa,KAAK,SAAU;AAAA,IAC9B;AAEA,QAAI,KAAK,SAAS,YAAY,QAAQ,CAAC,YAAY;AACjD,WAAK,SAAS;AAAA,IAChB;AAEA,SAAK,YAAY,WAAW,MAAM;AAChC,UAAI,KAAK,SAAS,aAAa,QAAQ,YAAY;AACjD,aAAK,SAAS;AAAA,MAChB;AACA,WAAK,UAAU;AAAA,IACjB,GAAG,KAAK,SAAS,KAAK;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBO,SAAe;AACpB,QAAI,KAAK,WAAW;AAClB,mBAAa,KAAK,SAAU;AAAA,IAC9B;AACA,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKQ,YAAkB;AACxB,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBO,QAAc;AACnB,QAAI,KAAK,WAAW;AAClB,WAAK,OAAO;AACZ,WAAK,SAAS;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,WAAiB;AACvB,QAAI,KAAK,YAAY;AAEnB,WAAK,SAAS,SAAS,MAAM,KAAK,SAAS,aAAa,KAAK,UAAU;AAAA,IACzE;AAAA,EACF;AACF;AA6BO,SAAS,gBAAuC,QAA0C;AAC/F,SAAO,IAAI,UAAU,MAAM;AAC7B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,mBAAmB,WAAW,CAAC"}
|
package/dist/main.mjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/* @alwatr/debounce v1.0.0-rc.0 */
|
|
2
|
+
|
|
3
|
+
// src/debounce.ts
|
|
4
|
+
var Debouncer = class {
|
|
5
|
+
constructor(config__) {
|
|
6
|
+
this.config__ = config__;
|
|
7
|
+
this.config__.trailing ??= true;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Checks if there is a pending execution scheduled.
|
|
11
|
+
* Returns true if a timer is active, indicating a debounced call is waiting.
|
|
12
|
+
*/
|
|
13
|
+
get isPending() {
|
|
14
|
+
return this.timerId__ !== void 0;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Triggers the debounced function with the stored `thisContext`.
|
|
18
|
+
* @param args The arguments to pass to the callback.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* const debouncer = new Debouncer({
|
|
23
|
+
* callback: (value: number) => console.log('Value:', value),
|
|
24
|
+
* delay: 500,
|
|
25
|
+
* });
|
|
26
|
+
* debouncer.trigger(42); // Logs after 500ms if not triggered again
|
|
27
|
+
*
|
|
28
|
+
* // Edge case: Rapid triggers only execute the last one
|
|
29
|
+
* debouncer.trigger(1);
|
|
30
|
+
* debouncer.trigger(2); // Only 2 will execute after delay
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
trigger(...args) {
|
|
34
|
+
this.lastArgs__ = args;
|
|
35
|
+
const wasPending = this.isPending;
|
|
36
|
+
if (wasPending) {
|
|
37
|
+
clearTimeout(this.timerId__);
|
|
38
|
+
}
|
|
39
|
+
if (this.config__.leading === true && !wasPending) {
|
|
40
|
+
this.invoke__();
|
|
41
|
+
}
|
|
42
|
+
this.timerId__ = setTimeout(() => {
|
|
43
|
+
if (this.config__.trailing === true && wasPending) {
|
|
44
|
+
this.invoke__();
|
|
45
|
+
}
|
|
46
|
+
this.cleanup__();
|
|
47
|
+
}, this.config__.delay);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Cancels any pending debounced execution and cleans up internal state.
|
|
51
|
+
* Useful for stopping execution when the operation is no longer needed (e.g., component unmount).
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const debouncer = new Debouncer({
|
|
56
|
+
* callback: () => console.log('Executed'),
|
|
57
|
+
* delay: 1000,
|
|
58
|
+
* });
|
|
59
|
+
* debouncer.trigger();
|
|
60
|
+
* debouncer.cancel(); // Prevents execution
|
|
61
|
+
*
|
|
62
|
+
* // Note: After cancel, isPending becomes false
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
cancel() {
|
|
66
|
+
if (this.isPending) {
|
|
67
|
+
clearTimeout(this.timerId__);
|
|
68
|
+
}
|
|
69
|
+
this.cleanup__();
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Cleans up internal state by deleting timer and arguments.
|
|
73
|
+
*/
|
|
74
|
+
cleanup__() {
|
|
75
|
+
delete this.timerId__;
|
|
76
|
+
delete this.lastArgs__;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Immediately executes the pending function if one exists.
|
|
80
|
+
* Bypasses the delay and cleans up state. If no pending call, does nothing.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```typescript
|
|
84
|
+
* const debouncer = new Debouncer({
|
|
85
|
+
* callback: () => console.log('Flushed'),
|
|
86
|
+
* delay: 1000,
|
|
87
|
+
* });
|
|
88
|
+
* debouncer.trigger();
|
|
89
|
+
* setTimeout(() => debouncer.flush(), 500); // Executes immediately
|
|
90
|
+
*
|
|
91
|
+
* // Edge case: Flush after cancel does nothing
|
|
92
|
+
* debouncer.cancel();
|
|
93
|
+
* debouncer.flush(); // No execution
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
flush() {
|
|
97
|
+
if (this.isPending) {
|
|
98
|
+
this.cancel();
|
|
99
|
+
this.invoke__();
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* The core execution logic.
|
|
104
|
+
*/
|
|
105
|
+
invoke__() {
|
|
106
|
+
if (this.lastArgs__) {
|
|
107
|
+
this.config__.callback.apply(this.config__.thisContext, this.lastArgs__);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
function createDebouncer(config) {
|
|
112
|
+
return new Debouncer(config);
|
|
113
|
+
}
|
|
114
|
+
export {
|
|
115
|
+
Debouncer,
|
|
116
|
+
createDebouncer
|
|
117
|
+
};
|
|
118
|
+
//# sourceMappingURL=main.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/debounce.ts"],
|
|
4
|
+
"sourcesContent": ["import type {DebouncerConfig} from './type.ts';\n\n/**\n * A powerful and type-safe Debouncer class.\n * \n * It encapsulates the debouncing logic, state, and provides a rich control API.\n * Debouncing delays function execution until after a specified delay has passed since the last invocation.\n * Useful for optimizing performance in scenarios like search inputs, resize events, or API calls.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: (text: string) => console.log('Searching:', text),\n * delay: 300,\n * leading: false,\n * trailing: true,\n * });\n * \n * // Debounce search input\n * debouncer.trigger('hello');\n * debouncer.trigger('hello world'); // Only 'hello world' will log after 300ms\n * \n * // Advanced: With leading edge\n * const leadingDebouncer = new Debouncer({\n * callback: () => console.log('Immediate and delayed'),\n * delay: 500,\n * leading: true,\n * trailing: true,\n * });\n * leadingDebouncer.trigger(); // Logs immediately, then again after 500ms if not cancelled\n * ```\n */\nexport class Debouncer<F extends AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private lastArgs__?: Parameters<F>;\n\n public constructor(private readonly config__: DebouncerConfig<F>) {\n this.config__.trailing ??= true;\n }\n\n /**\n * Checks if there is a pending execution scheduled.\n * Returns true if a timer is active, indicating a debounced call is waiting.\n */\n public get isPending(): boolean {\n return this.timerId__ !== undefined;\n }\n\n /**\n * Triggers the debounced function with the stored `thisContext`.\n * @param args The arguments to pass to the callback.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: (value: number) => console.log('Value:', value),\n * delay: 500,\n * });\n * debouncer.trigger(42); // Logs after 500ms if not triggered again\n * \n * // Edge case: Rapid triggers only execute the last one\n * debouncer.trigger(1);\n * debouncer.trigger(2); // Only 2 will execute after delay\n * ```\n */\n public trigger(...args: Parameters<F>): void {\n this.lastArgs__ = args;\n const wasPending = this.isPending;\n\n if (wasPending) {\n clearTimeout(this.timerId__!);\n }\n\n if (this.config__.leading === true && !wasPending) {\n this.invoke__();\n }\n\n this.timerId__ = setTimeout(() => {\n if (this.config__.trailing === true && wasPending) {\n this.invoke__();\n }\n this.cleanup__();\n }, this.config__.delay);\n }\n\n /**\n * Cancels any pending debounced execution and cleans up internal state.\n * Useful for stopping execution when the operation is no longer needed (e.g., component unmount).\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: () => console.log('Executed'),\n * delay: 1000,\n * });\n * debouncer.trigger();\n * debouncer.cancel(); // Prevents execution\n * \n * // Note: After cancel, isPending becomes false\n * ```\n */\n public cancel(): void {\n if (this.isPending) {\n clearTimeout(this.timerId__!);\n }\n this.cleanup__();\n }\n\n /**\n * Cleans up internal state by deleting timer and arguments.\n */\n private cleanup__(): void {\n delete this.timerId__;\n delete this.lastArgs__;\n }\n\n /**\n * Immediately executes the pending function if one exists.\n * Bypasses the delay and cleans up state. If no pending call, does nothing.\n * \n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * callback: () => console.log('Flushed'),\n * delay: 1000,\n * });\n * debouncer.trigger();\n * setTimeout(() => debouncer.flush(), 500); // Executes immediately\n * \n * // Edge case: Flush after cancel does nothing\n * debouncer.cancel();\n * debouncer.flush(); // No execution\n * ```\n */\n public flush(): void {\n if (this.isPending) {\n this.cancel();\n this.invoke__();\n }\n }\n\n /**\n * The core execution logic.\n */\n private invoke__(): void {\n if (this.lastArgs__) {\n // `thisContext` is now read directly from the stored config.\n this.config__.callback.apply(this.config__.thisContext, this.lastArgs__);\n }\n }\n}\n\n/**\n * Factory function for creating a Debouncer instance for better type inference.\n * @param config Configuration for the debouncer.\n * \n * @example\n * ```typescript\n * const debouncer = createDebouncer({\n * callback: (text: string) => console.log('Searching:', text),\n * delay: 300,\n * leading: false,\n * trailing: true,\n * });\n * \n * // Debounce search input\n * debouncer.trigger('hello');\n * debouncer.trigger('hello world'); // Only 'hello world' will log after 300ms\n * \n * // With custom thisContext\n * const obj = { log: (msg: string) => console.log('Obj:', msg) };\n * const debouncerWithContext = createDebouncer({\n * callback: obj.log,\n * thisContext: obj,\n * delay: 200,\n * });\n * debouncerWithContext.trigger('test'); // Logs 'Obj: test'\n * ```\n */\nexport function createDebouncer<F extends AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"],
|
|
5
|
+
"mappings": ";;;AAgCO,IAAM,YAAN,MAAuC;AAAA,EAIrC,YAA6B,UAA8B;AAA9B;AAClC,SAAK,SAAS,aAAa;AAAA,EAC7B;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,IAAW,YAAqB;AAC9B,WAAO,KAAK,cAAc;AAAA,EAC5B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBO,WAAW,MAA2B;AAC3C,SAAK,aAAa;AAClB,UAAM,aAAa,KAAK;AAExB,QAAI,YAAY;AACd,mBAAa,KAAK,SAAU;AAAA,IAC9B;AAEA,QAAI,KAAK,SAAS,YAAY,QAAQ,CAAC,YAAY;AACjD,WAAK,SAAS;AAAA,IAChB;AAEA,SAAK,YAAY,WAAW,MAAM;AAChC,UAAI,KAAK,SAAS,aAAa,QAAQ,YAAY;AACjD,aAAK,SAAS;AAAA,MAChB;AACA,WAAK,UAAU;AAAA,IACjB,GAAG,KAAK,SAAS,KAAK;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAkBO,SAAe;AACpB,QAAI,KAAK,WAAW;AAClB,mBAAa,KAAK,SAAU;AAAA,IAC9B;AACA,SAAK,UAAU;AAAA,EACjB;AAAA;AAAA;AAAA;AAAA,EAKQ,YAAkB;AACxB,WAAO,KAAK;AACZ,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAoBO,QAAc;AACnB,QAAI,KAAK,WAAW;AAClB,WAAK,OAAO;AACZ,WAAK,SAAS;AAAA,IAChB;AAAA,EACF;AAAA;AAAA;AAAA;AAAA,EAKQ,WAAiB;AACvB,QAAI,KAAK,YAAY;AAEnB,WAAK,SAAS,SAAS,MAAM,KAAK,SAAS,aAAa,KAAK,UAAU;AAAA,IACzE;AAAA,EACF;AACF;AA6BO,SAAS,gBAAuC,QAA0C;AAC/F,SAAO,IAAI,UAAU,MAAM;AAC7B;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/dist/type.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A single configuration object for creating a Debouncer.
|
|
3
|
+
* This groups all settings for a cleaner API.
|
|
4
|
+
*
|
|
5
|
+
* Key notes:
|
|
6
|
+
* - `leading` and `trailing` control execution timing: leading executes immediately on first trigger, trailing after delay.
|
|
7
|
+
* - If both are true, execution happens on first trigger and last trigger (after delay).
|
|
8
|
+
* - `thisContext` ensures the callback is bound to the correct `this` value, useful in class methods or event handlers.
|
|
9
|
+
* - `delay` must be a positive number.
|
|
10
|
+
*/
|
|
11
|
+
export interface DebouncerConfig<F extends AnyFunction> {
|
|
12
|
+
/**
|
|
13
|
+
* The function to be executed after the delay.
|
|
14
|
+
* Can be any function type, with type safety enforced by generics.
|
|
15
|
+
*/
|
|
16
|
+
callback: F;
|
|
17
|
+
/**
|
|
18
|
+
* The `this` context to be used when invoking the callback.
|
|
19
|
+
* If provided, it will be stored and used for all invocations.
|
|
20
|
+
* Omit if the callback doesn't rely on `this` or uses arrow functions.
|
|
21
|
+
*/
|
|
22
|
+
thisContext?: ThisParameterType<F>;
|
|
23
|
+
/**
|
|
24
|
+
* The delay in milliseconds before the function is executed.
|
|
25
|
+
* Must be a positive integer; affects performance and responsiveness.
|
|
26
|
+
*/
|
|
27
|
+
delay: number;
|
|
28
|
+
/**
|
|
29
|
+
* If `true`, the function is called on the leading edge of the timeout.
|
|
30
|
+
* Useful for immediate feedback (e.g., button press).
|
|
31
|
+
* @default false
|
|
32
|
+
*/
|
|
33
|
+
leading?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* If `true`, the function is called on the trailing edge of the timeout.
|
|
36
|
+
* Ensures the last call is executed after inactivity.
|
|
37
|
+
* @default true
|
|
38
|
+
*/
|
|
39
|
+
trailing?: boolean;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../src/type.ts"],"names":[],"mappings":"AAEA;;;;;;;;;GASG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,SAAS,WAAW;IACpD;;;OAGG;IACH,QAAQ,EAAE,CAAC,CAAC;IAEZ;;;;OAIG;IACH,WAAW,CAAC,EAAE,iBAAiB,CAAC,CAAC,CAAC,CAAC;IAEnC;;;OAGG;IACH,KAAK,EAAE,MAAM,CAAC;IAEd;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB"}
|
package/package.json
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alwatr/debounce",
|
|
3
|
+
"description": "A powerful, modern, and type-safe debouncer utility designed for high-performance applications. It's framework-agnostic, works seamlessly in both Node.js and browsers, and provides a rich API for fine-grained control over function execution.",
|
|
4
|
+
"version": "1.0.0-rc.0",
|
|
5
|
+
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
|
|
6
|
+
"bugs": "https://github.com/Alwatr/nanolib/issues",
|
|
7
|
+
"devDependencies": {
|
|
8
|
+
"@alwatr/nano-build": "6.1.2",
|
|
9
|
+
"@alwatr/prettier-config": "5.0.3",
|
|
10
|
+
"@alwatr/tsconfig-base": "6.0.1",
|
|
11
|
+
"@alwatr/type-helper": "6.0.2",
|
|
12
|
+
"@types/node": "^22.18.3",
|
|
13
|
+
"typescript": "^5.9.2"
|
|
14
|
+
},
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/main.d.ts",
|
|
18
|
+
"import": "./dist/main.mjs",
|
|
19
|
+
"require": "./dist/main.cjs"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"**/*.{js,mjs,cjs,map,d.ts,html,md,LEGAL.txt}",
|
|
24
|
+
"LICENSE",
|
|
25
|
+
"!demo/**/*"
|
|
26
|
+
],
|
|
27
|
+
"homepage": "https://github.com/Alwatr/nanolib/tree/next/packages/debounce#readme",
|
|
28
|
+
"keywords": [
|
|
29
|
+
"debounce",
|
|
30
|
+
"debouncing",
|
|
31
|
+
"typescript",
|
|
32
|
+
"type-safe",
|
|
33
|
+
"javascript",
|
|
34
|
+
"esm",
|
|
35
|
+
"module",
|
|
36
|
+
"browser",
|
|
37
|
+
"node",
|
|
38
|
+
"nodejs",
|
|
39
|
+
"cross-platform",
|
|
40
|
+
"universal",
|
|
41
|
+
"utility",
|
|
42
|
+
"util",
|
|
43
|
+
"leading",
|
|
44
|
+
"trailing",
|
|
45
|
+
"lifecycle",
|
|
46
|
+
"memory-leak",
|
|
47
|
+
"tree-shakable",
|
|
48
|
+
"framework-agnostic",
|
|
49
|
+
"alwatr",
|
|
50
|
+
"nanolib"
|
|
51
|
+
],
|
|
52
|
+
"license": "MPL-2.0",
|
|
53
|
+
"main": "./dist/main.cjs",
|
|
54
|
+
"module": "./dist/main.mjs",
|
|
55
|
+
"prettier": "@alwatr/prettier-config",
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "https://github.com/Alwatr/nanolib",
|
|
62
|
+
"directory": "packages/debounce"
|
|
63
|
+
},
|
|
64
|
+
"scripts": {
|
|
65
|
+
"b": "yarn run build",
|
|
66
|
+
"build": "yarn run build:ts && yarn run build:es",
|
|
67
|
+
"build:es": "nano-build --preset=module",
|
|
68
|
+
"build:ts": "tsc --build",
|
|
69
|
+
"c": "yarn run clean",
|
|
70
|
+
"cb": "yarn run clean && yarn run build",
|
|
71
|
+
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
72
|
+
"d": "yarn run build:es && yarn node --enable-source-maps --trace-warnings",
|
|
73
|
+
"w": "yarn run watch",
|
|
74
|
+
"watch": "yarn run watch:ts & yarn run watch:es",
|
|
75
|
+
"watch:es": "yarn run build:es --watch",
|
|
76
|
+
"watch:ts": "yarn run build:ts --watch --preserveWatchOutput"
|
|
77
|
+
},
|
|
78
|
+
"type": "module",
|
|
79
|
+
"types": "./dist/main.d.ts",
|
|
80
|
+
"gitHead": "4da18811162df49c118acd71086cdbe38b27f250"
|
|
81
|
+
}
|