@alwatr/debounce 9.31.0 → 9.33.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/README.md +7 -7
- package/dist/dev/main.js +5 -0
- package/dist/dev/main.js.map +11 -0
- package/dist/main.js +2 -2
- package/dist/main.js.map +1 -1
- package/package.json +11 -8
package/README.md
CHANGED
|
@@ -100,13 +100,13 @@ A factory function that creates a new `Debouncer` instance. It's the recommended
|
|
|
100
100
|
|
|
101
101
|
This is the configuration object passed to `createDebouncer` or the `Debouncer` constructor.
|
|
102
102
|
|
|
103
|
-
| Property | Type | Description
|
|
104
|
-
| :------------ | :---------------------- |
|
|
105
|
-
| `func` | `F extends AnyFunction` | **(Required)** The function to be debounced.
|
|
106
|
-
| `delay` | `number` | **(Required)** The debounce delay in milliseconds.
|
|
103
|
+
| Property | Type | Description | Default |
|
|
104
|
+
| :------------ | :---------------------- | :--------------------------------------------------------------------- | :---------- |
|
|
105
|
+
| `func` | `F extends AnyFunction` | **(Required)** The function to be debounced. | - |
|
|
106
|
+
| `delay` | `number` | **(Required)** The debounce delay in milliseconds. | - |
|
|
107
107
|
| `thisContext` | `ThisParameterType<F>` | The `this` context for the `func`. Essential when using class methods. | `undefined` |
|
|
108
|
-
| `leading` | `boolean` | If `true`, executes the function on the leading edge.
|
|
109
|
-
| `trailing` | `boolean` | If `true`, executes the function on the trailing edge.
|
|
108
|
+
| `leading` | `boolean` | If `true`, executes the function on the leading edge. | `false` |
|
|
109
|
+
| `trailing` | `boolean` | If `true`, executes the function on the trailing edge. | `true` |
|
|
110
110
|
|
|
111
111
|
### `Debouncer` Instance
|
|
112
112
|
|
|
@@ -340,7 +340,7 @@ debouncer.trigger('علی');
|
|
|
340
340
|
| :------------ | :---------------------- | :-------------------------------------------------------------------- | :---------- |
|
|
341
341
|
| `func` | `F extends AnyFunction` | **(الزامی)** تابعی که باید دیبانس شود. | - |
|
|
342
342
|
| `delay` | `number` | **(الزامی)** تأخیر دیبانس بر حسب میلیثانیه. | - |
|
|
343
|
-
| `thisContext` | `ThisParameterType<F>` | کانتکست `this` برای `func`. هنگام استفاده از متدهای کلاس ضروری است.
|
|
343
|
+
| `thisContext` | `ThisParameterType<F>` | کانتکست `this` برای `func`. هنگام استفاده از متدهای کلاس ضروری است. | `undefined` |
|
|
344
344
|
| `leading` | `boolean` | اگر `true` باشد، تابع در لبه بالارونده (leading edge) اجرا میشود. | `false` |
|
|
345
345
|
| `trailing` | `boolean` | اگر `true` باشد، تابع در لبه پایینرونده (trailing edge) اجرا میشود. | `true` |
|
|
346
346
|
|
package/dist/dev/main.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/* 📦 @alwatr/debounce v9.33.1 */
|
|
2
|
+
class z{config__;timerId__;maxWaitTimerId__;lastArgs__;constructor(q){this.config__=q;this.config__.trailing??=!0,this.flush=this.flush.bind(this),this.trigger=this.trigger.bind(this),this.cancel=this.cancel.bind(this)}get isPending(){return this.timerId__!==void 0}trigger(...q){if(this.lastArgs__=q,!this.isPending){if(this.config__.maxWait)this.maxWaitTimerId__=setTimeout(this.flush,this.config__.maxWait);if(this.config__.leading===!0)this.invoke__()}else clearTimeout(this.timerId__);this.timerId__=setTimeout(()=>{if(this.config__.trailing===!0)this.invoke__();this.cleanup__()},this.config__.delay)}cancel(){if(this.timerId__)clearTimeout(this.timerId__);if(this.maxWaitTimerId__)clearTimeout(this.maxWaitTimerId__);this.cleanup__()}cleanup__(){delete this.timerId__,delete this.maxWaitTimerId__,delete this.lastArgs__}flush(){if(this.isPending)this.invoke__();this.cancel()}invoke__(){if(this.lastArgs__)this.config__.func.apply(this.config__.thisContext,this.lastArgs__),this.lastArgs__=void 0}}function G(q){return new z(q)}export{G as createDebouncer,z as Debouncer};
|
|
3
|
+
|
|
4
|
+
//# debugId=0A0890FA8B27299B64756E2164756E21
|
|
5
|
+
//# sourceMappingURL=main.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/debounce.ts", "../../src/main.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import type {AnyFunc} from '@alwatr/type-helper';\nimport type {DebouncerConfig} from './type.js';\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 * func: (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 * func: () => 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 AnyFunc> {\n private timerId__?: number | NodeJS.Timeout;\n private maxWaitTimerId__?: number | NodeJS.Timeout;\n private lastArgs__?: Parameters<F>;\n\n constructor(private readonly config__: DebouncerConfig<F>) {\n this.config__.trailing ??= true;\n this.flush = this.flush.bind(this);\n this.trigger = this.trigger.bind(this);\n this.cancel = this.cancel.bind(this);\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 `func`.\n *\n * @example\n * ```typescript\n * const debouncer = new Debouncer({\n * func: (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; // its an array even if triggered without any args\n const firstTrigger = !this.isPending;\n\n if (firstTrigger) {\n if (this.config__.maxWait) {\n this.maxWaitTimerId__ = setTimeout(this.flush, this.config__.maxWait);\n }\n if (this.config__.leading === true) {\n this.invoke__();\n }\n } else {\n clearTimeout(this.timerId__!);\n }\n\n this.timerId__ = setTimeout(() => {\n if (this.config__.trailing === true) {\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 * func: () => 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.timerId__) {\n clearTimeout(this.timerId__);\n }\n if (this.maxWaitTimerId__) {\n clearTimeout(this.maxWaitTimerId__);\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.maxWaitTimerId__;\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 * func: () => 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.invoke__();\n }\n this.cancel();\n }\n\n /**\n * The core execution logic.\n */\n private invoke__(): void {\n if (this.lastArgs__) {\n // only call if we have new args (skip trailing call if leading already called)\n this.config__.func.apply(this.config__.thisContext, this.lastArgs__);\n this.lastArgs__ = undefined;\n }\n }\n}\n",
|
|
6
|
+
"import type {AnyFunc} from '@alwatr/type-helper';\nimport {Debouncer} from './debounce.js';\n\nimport type {DebouncerConfig} from './type.js';\n\nexport * from './debounce.js';\nexport type * from './type.js';\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 * func: (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 * func: obj.log,\n * thisContext: obj,\n * delay: 200,\n * });\n * debouncerWithContext.trigger('test'); // Logs 'Obj: test'\n * ```\n */\nexport function createDebouncer<F extends AnyFunc>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AAiCO,MAAM,CAA6B,CAKX,SAJrB,UACA,iBACA,WAER,WAAW,CAAkB,EAA8B,CAA9B,gBAC3B,KAAK,SAAS,WAAa,GAC3B,KAAK,MAAQ,KAAK,MAAM,KAAK,IAAI,EACjC,KAAK,QAAU,KAAK,QAAQ,KAAK,IAAI,EACrC,KAAK,OAAS,KAAK,OAAO,KAAK,IAAI,KAO1B,UAAS,EAAY,CAC9B,OAAO,KAAK,YAAc,OAoBrB,OAAO,IAAI,EAA2B,CAI3C,GAHA,KAAK,WAAa,EACG,CAAC,KAAK,UAET,CAChB,GAAI,KAAK,SAAS,QAChB,KAAK,iBAAmB,WAAW,KAAK,MAAO,KAAK,SAAS,OAAO,EAEtE,GAAI,KAAK,SAAS,UAAY,GAC5B,KAAK,SAAS,EAGhB,kBAAa,KAAK,SAAU,EAG9B,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,GAC7B,KAAK,SAAS,EAEhB,KAAK,UAAU,GACd,KAAK,SAAS,KAAK,EAmBjB,MAAM,EAAS,CACpB,GAAI,KAAK,UACP,aAAa,KAAK,SAAS,EAE7B,GAAI,KAAK,iBACP,aAAa,KAAK,gBAAgB,EAEpC,KAAK,UAAU,EAMT,SAAS,EAAS,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,WAqBP,KAAK,EAAS,CACnB,GAAI,KAAK,UACP,KAAK,SAAS,EAEhB,KAAK,OAAO,EAMN,QAAQ,EAAS,CACvB,GAAI,KAAK,WAEP,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,OAGxB,CChIO,SAAS,CAAkC,CAAC,EAA0C,CAC3F,OAAO,IAAI,EAAU,CAAM",
|
|
9
|
+
"debugId": "0A0890FA8B27299B64756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
package/dist/main.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
/* 📦 @alwatr/debounce v9.
|
|
1
|
+
/* 📦 @alwatr/debounce v9.33.1 */
|
|
2
2
|
class z{config__;timerId__;maxWaitTimerId__;lastArgs__;constructor(q){this.config__=q;this.config__.trailing??=!0,this.flush=this.flush.bind(this),this.trigger=this.trigger.bind(this),this.cancel=this.cancel.bind(this)}get isPending(){return this.timerId__!==void 0}trigger(...q){if(this.lastArgs__=q,!this.isPending){if(this.config__.maxWait)this.maxWaitTimerId__=setTimeout(this.flush,this.config__.maxWait);if(this.config__.leading===!0)this.invoke__()}else clearTimeout(this.timerId__);this.timerId__=setTimeout(()=>{if(this.config__.trailing===!0)this.invoke__();this.cleanup__()},this.config__.delay)}cancel(){if(this.timerId__)clearTimeout(this.timerId__);if(this.maxWaitTimerId__)clearTimeout(this.maxWaitTimerId__);this.cleanup__()}cleanup__(){delete this.timerId__,delete this.maxWaitTimerId__,delete this.lastArgs__}flush(){if(this.isPending)this.invoke__();this.cancel()}invoke__(){if(this.lastArgs__)this.config__.func.apply(this.config__.thisContext,this.lastArgs__),this.lastArgs__=void 0}}function G(q){return new z(q)}export{G as createDebouncer,z as Debouncer};
|
|
3
3
|
|
|
4
|
-
//# debugId=
|
|
4
|
+
//# debugId=0A0890FA8B27299B64756E2164756E21
|
|
5
5
|
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
CHANGED
|
@@ -6,6 +6,6 @@
|
|
|
6
6
|
"import type {AnyFunc} from '@alwatr/type-helper';\nimport {Debouncer} from './debounce.js';\n\nimport type {DebouncerConfig} from './type.js';\n\nexport * from './debounce.js';\nexport type * from './type.js';\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 * func: (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 * func: obj.log,\n * thisContext: obj,\n * delay: 200,\n * });\n * debouncerWithContext.trigger('test'); // Logs 'Obj: test'\n * ```\n */\nexport function createDebouncer<F extends AnyFunc>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"
|
|
7
7
|
],
|
|
8
8
|
"mappings": ";AAiCO,MAAM,CAA6B,CAKX,SAJrB,UACA,iBACA,WAER,WAAW,CAAkB,EAA8B,CAA9B,gBAC3B,KAAK,SAAS,WAAa,GAC3B,KAAK,MAAQ,KAAK,MAAM,KAAK,IAAI,EACjC,KAAK,QAAU,KAAK,QAAQ,KAAK,IAAI,EACrC,KAAK,OAAS,KAAK,OAAO,KAAK,IAAI,KAO1B,UAAS,EAAY,CAC9B,OAAO,KAAK,YAAc,OAoBrB,OAAO,IAAI,EAA2B,CAI3C,GAHA,KAAK,WAAa,EACG,CAAC,KAAK,UAET,CAChB,GAAI,KAAK,SAAS,QAChB,KAAK,iBAAmB,WAAW,KAAK,MAAO,KAAK,SAAS,OAAO,EAEtE,GAAI,KAAK,SAAS,UAAY,GAC5B,KAAK,SAAS,EAGhB,kBAAa,KAAK,SAAU,EAG9B,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,GAC7B,KAAK,SAAS,EAEhB,KAAK,UAAU,GACd,KAAK,SAAS,KAAK,EAmBjB,MAAM,EAAS,CACpB,GAAI,KAAK,UACP,aAAa,KAAK,SAAS,EAE7B,GAAI,KAAK,iBACP,aAAa,KAAK,gBAAgB,EAEpC,KAAK,UAAU,EAMT,SAAS,EAAS,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,WAqBP,KAAK,EAAS,CACnB,GAAI,KAAK,UACP,KAAK,SAAS,EAEhB,KAAK,OAAO,EAMN,QAAQ,EAAS,CACvB,GAAI,KAAK,WAEP,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,OAGxB,CChIO,SAAS,CAAkC,CAAC,EAA0C,CAC3F,OAAO,IAAI,EAAU,CAAM",
|
|
9
|
-
"debugId": "
|
|
9
|
+
"debugId": "0A0890FA8B27299B64756E2164756E21",
|
|
10
10
|
"names": []
|
|
11
11
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alwatr/debounce",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.33.1",
|
|
4
4
|
"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.",
|
|
5
5
|
"license": "MPL-2.0",
|
|
6
6
|
"author": "S. Ali Mihandoost <ali.mihandoost@gmail.com> (https://ali.mihandoost.com)",
|
|
@@ -15,32 +15,35 @@
|
|
|
15
15
|
"exports": {
|
|
16
16
|
".": {
|
|
17
17
|
"types": "./dist/main.d.ts",
|
|
18
|
+
"development": "./dist/dev/main.js",
|
|
18
19
|
"import": "./dist/main.js",
|
|
19
20
|
"default": "./dist/main.js"
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"sideEffects": false,
|
|
23
24
|
"devDependencies": {
|
|
24
|
-
"@alwatr/nano-build": "9.
|
|
25
|
-
"@alwatr/standard": "9.
|
|
26
|
-
"@alwatr/type-helper": "9.
|
|
25
|
+
"@alwatr/nano-build": "9.33.1",
|
|
26
|
+
"@alwatr/standard": "9.33.0",
|
|
27
|
+
"@alwatr/type-helper": "9.33.1",
|
|
27
28
|
"@types/node": "^24.13.1",
|
|
28
29
|
"typescript": "^6.0.3"
|
|
29
30
|
},
|
|
30
31
|
"scripts": {
|
|
31
32
|
"b": "bun run build",
|
|
32
33
|
"build": "bun run build:ts && bun run build:es",
|
|
33
|
-
"build:es": "
|
|
34
|
+
"build:es": "bun run build:es:dev && bun run build:es:prod",
|
|
35
|
+
"build:es:dev": "nano-build --preset=module --outdir=dist/dev src/main.ts",
|
|
36
|
+
"build:es:prod": "NODE_ENV=production nano-build --preset=module --outdir=dist src/main.ts",
|
|
34
37
|
"build:ts": "tsc --build",
|
|
35
38
|
"cl": "bun run clean",
|
|
36
39
|
"clean": "rm -rfv dist *.tsbuildinfo",
|
|
37
40
|
"format": "prettier --write \"src/**/*.ts\"",
|
|
38
41
|
"lint": "eslint src/ --ext .ts",
|
|
39
42
|
"t": "bun run test",
|
|
40
|
-
"test": "
|
|
43
|
+
"test": "bun test",
|
|
41
44
|
"w": "bun run watch",
|
|
42
45
|
"watch": "bun run watch:ts & bun run watch:es",
|
|
43
|
-
"watch:es": "bun run build:es --watch",
|
|
46
|
+
"watch:es": "bun run build:es:dev --watch",
|
|
44
47
|
"watch:ts": "bun run build:ts --watch --preserveWatchOutput"
|
|
45
48
|
},
|
|
46
49
|
"files": [
|
|
@@ -77,5 +80,5 @@
|
|
|
77
80
|
"util",
|
|
78
81
|
"utility"
|
|
79
82
|
],
|
|
80
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "84fba5b7b428188be17aaaaf062b0b7eaae96555"
|
|
81
84
|
}
|