@alwatr/debounce 1.1.3 → 1.1.4

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 CHANGED
@@ -3,6 +3,12 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [1.1.4](https://github.com/Alwatr/nanolib/compare/@alwatr/debounce@1.1.3...@alwatr/debounce@1.1.4) (2025-09-20)
7
+
8
+ ### 🐛 Bug Fixes
9
+
10
+ * bind flush method in Debouncer constructor for proper context handling ([bdbeaed](https://github.com/Alwatr/nanolib/commit/bdbeaeddb2492da65476c374e9707e335cc39726))
11
+
6
12
  ## [1.1.3](https://github.com/Alwatr/nanolib/compare/@alwatr/debounce@1.1.2...@alwatr/debounce@1.1.3) (2025-09-19)
7
13
 
8
14
  **Note:** Version bump only for package @alwatr/debounce
@@ -1 +1 @@
1
- {"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../src/debounce.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,WAAW,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,WAAW;IAKvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAJ5C,OAAO,CAAC,SAAS,CAAC,CAA0B;IAC5C,OAAO,CAAC,gBAAgB,CAAC,CAA0B;IACnD,OAAO,CAAC,UAAU,CAAC,CAAgB;gBAEC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;IAIhE;;;OAGG;IACH,IAAW,SAAS,IAAI,OAAO,CAE9B;IAED;;;;;;;;;;;;;;;;OAgBG;IACI,OAAO,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI;IAwB5C;;;;;;;;;;;;;;;OAeG;IACI,MAAM,IAAI,IAAI;IAUrB;;OAEG;IACH,OAAO,CAAC,SAAS;IAMjB;;;;;;;;;;;;;;;;;OAiBG;IACI,KAAK,IAAI,IAAI;IAOpB;;OAEG;IACH,OAAO,CAAC,QAAQ;CAOjB"}
1
+ {"version":3,"file":"debounce.d.ts","sourceRoot":"","sources":["../src/debounce.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,WAAW,CAAC;AAE/C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,qBAAa,SAAS,CAAC,CAAC,SAAS,WAAW;IAKvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAJ5C,OAAO,CAAC,SAAS,CAAC,CAA0B;IAC5C,OAAO,CAAC,gBAAgB,CAAC,CAA0B;IACnD,OAAO,CAAC,UAAU,CAAC,CAAgB;gBAEC,QAAQ,EAAE,eAAe,CAAC,CAAC,CAAC;IAKhE;;;OAGG;IACH,IAAW,SAAS,IAAI,OAAO,CAE9B;IAED;;;;;;;;;;;;;;;;OAgBG;IACI,OAAO,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,GAAG,IAAI;IAwB5C;;;;;;;;;;;;;;;OAeG;IACI,MAAM,IAAI,IAAI;IAUrB;;OAEG;IACH,OAAO,CAAC,SAAS;IAMjB;;;;;;;;;;;;;;;;;OAiBG;IACI,KAAK,IAAI,IAAI;IAOpB;;OAEG;IACH,OAAO,CAAC,QAAQ;CAOjB"}
package/dist/main.cjs CHANGED
@@ -1,3 +1,3 @@
1
- /* @alwatr/debounce v1.1.3 */
2
- "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{Debouncer:()=>Debouncer,createDebouncer:()=>createDebouncer});module.exports=__toCommonJS(main_exports);var Debouncer=class{constructor(config__){this.config__=config__;this.config__.trailing??=true}get isPending(){return this.timerId__!==void 0}trigger(...args){this.lastArgs__=args;const firstTrigger=!this.isPending;if(firstTrigger){if(this.config__.maxWait){this.maxWaitTimerId__=setTimeout(()=>this.flush,this.config__.maxWait)}if(this.config__.leading===true){this.invoke__()}}else{clearTimeout(this.timerId__)}this.timerId__=setTimeout(()=>{if(this.config__.trailing===true){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 createDebouncer(config){return new Debouncer(config)}0&&(module.exports={Debouncer,createDebouncer});
1
+ /* @alwatr/debounce v1.1.4 */
2
+ "use strict";var __defProp=Object.defineProperty;var __getOwnPropDesc=Object.getOwnPropertyDescriptor;var __getOwnPropNames=Object.getOwnPropertyNames;var __hasOwnProp=Object.prototype.hasOwnProperty;var __export=(target,all)=>{for(var name in all)__defProp(target,name,{get:all[name],enumerable:true})};var __copyProps=(to,from,except,desc)=>{if(from&&typeof from==="object"||typeof from==="function"){for(let key of __getOwnPropNames(from))if(!__hasOwnProp.call(to,key)&&key!==except)__defProp(to,key,{get:()=>from[key],enumerable:!(desc=__getOwnPropDesc(from,key))||desc.enumerable})}return to};var __toCommonJS=mod=>__copyProps(__defProp({},"__esModule",{value:true}),mod);var main_exports={};__export(main_exports,{Debouncer:()=>Debouncer,createDebouncer:()=>createDebouncer});module.exports=__toCommonJS(main_exports);var Debouncer=class{constructor(config__){this.config__=config__;this.config__.trailing??=true;this.flush=this.flush.bind(this)}get isPending(){return this.timerId__!==void 0}trigger(...args){this.lastArgs__=args;const firstTrigger=!this.isPending;if(firstTrigger){if(this.config__.maxWait){this.maxWaitTimerId__=setTimeout(this.flush,this.config__.maxWait)}if(this.config__.leading===true){this.invoke__()}}else{clearTimeout(this.timerId__)}this.timerId__=setTimeout(()=>{if(this.config__.trailing===true){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 createDebouncer(config){return new Debouncer(config)}0&&(module.exports={Debouncer,createDebouncer});
3
3
  //# sourceMappingURL=main.cjs.map
package/dist/main.cjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/main.ts", "../src/debounce.ts"],
4
- "sourcesContent": ["import {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 AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\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 * 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 AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private maxWaitTimerId__?: 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 `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 }\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"],
5
- "mappings": ";qqBAAA,mJCgCO,IAAM,UAAN,KAAuC,CAKrC,YAA6B,SAA8B,CAA9B,uBAClC,KAAK,SAAS,WAAa,IAC7B,CAMA,IAAW,WAAqB,CAC9B,OAAO,KAAK,YAAc,MAC5B,CAmBO,WAAW,KAA2B,CAC3C,KAAK,WAAa,KAClB,MAAM,aAAe,CAAC,KAAK,UAE3B,GAAI,aAAc,CAChB,GAAI,KAAK,SAAS,QAAS,CACzB,KAAK,iBAAmB,WAAW,IAAM,KAAK,MAAO,KAAK,SAAS,OAAO,CAC5E,CACA,GAAI,KAAK,SAAS,UAAY,KAAM,CAClC,KAAK,SAAS,CAChB,CACF,KACK,CACH,aAAa,KAAK,SAAU,CAC9B,CAEA,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,KAAM,CACnC,KAAK,SAAS,CAChB,CACA,KAAK,UAAU,CACjB,EAAG,KAAK,SAAS,KAAK,CACxB,CAkBO,QAAe,CACpB,GAAI,KAAK,UAAW,CAClB,aAAa,KAAK,SAAS,CAC7B,CACA,GAAI,KAAK,iBAAkB,CACzB,aAAa,KAAK,gBAAgB,CACpC,CACA,KAAK,UAAU,CACjB,CAKQ,WAAkB,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,UACd,CAoBO,OAAc,CACnB,GAAI,KAAK,UAAW,CAClB,KAAK,SAAS,CAChB,CACA,KAAK,OAAO,CACd,CAKQ,UAAiB,CACvB,GAAI,KAAK,WAAY,CAEnB,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,MACpB,CACF,CACF,ED9HO,SAAS,gBAAuC,OAA0C,CAC/F,OAAO,IAAI,UAAU,MAAM,CAC7B",
4
+ "sourcesContent": ["import {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 AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\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 * 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 AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private maxWaitTimerId__?: number | NodeJS.Timeout;\n private lastArgs__?: Parameters<F>;\n\n public constructor(private readonly config__: DebouncerConfig<F>) {\n this.config__.trailing ??= true;\n this.flush = this.flush.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 }\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"],
5
+ "mappings": ";qqBAAA,mJCgCO,IAAM,UAAN,KAAuC,CAKrC,YAA6B,SAA8B,CAA9B,uBAClC,KAAK,SAAS,WAAa,KAC3B,KAAK,MAAQ,KAAK,MAAM,KAAK,IAAI,CACnC,CAMA,IAAW,WAAqB,CAC9B,OAAO,KAAK,YAAc,MAC5B,CAmBO,WAAW,KAA2B,CAC3C,KAAK,WAAa,KAClB,MAAM,aAAe,CAAC,KAAK,UAE3B,GAAI,aAAc,CAChB,GAAI,KAAK,SAAS,QAAS,CACzB,KAAK,iBAAmB,WAAW,KAAK,MAAO,KAAK,SAAS,OAAO,CACtE,CACA,GAAI,KAAK,SAAS,UAAY,KAAM,CAClC,KAAK,SAAS,CAChB,CACF,KACK,CACH,aAAa,KAAK,SAAU,CAC9B,CAEA,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,KAAM,CACnC,KAAK,SAAS,CAChB,CACA,KAAK,UAAU,CACjB,EAAG,KAAK,SAAS,KAAK,CACxB,CAkBO,QAAe,CACpB,GAAI,KAAK,UAAW,CAClB,aAAa,KAAK,SAAS,CAC7B,CACA,GAAI,KAAK,iBAAkB,CACzB,aAAa,KAAK,gBAAgB,CACpC,CACA,KAAK,UAAU,CACjB,CAKQ,WAAkB,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,UACd,CAoBO,OAAc,CACnB,GAAI,KAAK,UAAW,CAClB,KAAK,SAAS,CAChB,CACA,KAAK,OAAO,CACd,CAKQ,UAAiB,CACvB,GAAI,KAAK,WAAY,CAEnB,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,MACpB,CACF,CACF,ED/HO,SAAS,gBAAuC,OAA0C,CAC/F,OAAO,IAAI,UAAU,MAAM,CAC7B",
6
6
  "names": []
7
7
  }
package/dist/main.mjs CHANGED
@@ -1,3 +1,3 @@
1
- /* @alwatr/debounce v1.1.3 */
2
- var Debouncer=class{constructor(config__){this.config__=config__;this.config__.trailing??=true}get isPending(){return this.timerId__!==void 0}trigger(...args){this.lastArgs__=args;const firstTrigger=!this.isPending;if(firstTrigger){if(this.config__.maxWait){this.maxWaitTimerId__=setTimeout(()=>this.flush,this.config__.maxWait)}if(this.config__.leading===true){this.invoke__()}}else{clearTimeout(this.timerId__)}this.timerId__=setTimeout(()=>{if(this.config__.trailing===true){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 createDebouncer(config){return new Debouncer(config)}export{Debouncer,createDebouncer};
1
+ /* @alwatr/debounce v1.1.4 */
2
+ var Debouncer=class{constructor(config__){this.config__=config__;this.config__.trailing??=true;this.flush=this.flush.bind(this)}get isPending(){return this.timerId__!==void 0}trigger(...args){this.lastArgs__=args;const firstTrigger=!this.isPending;if(firstTrigger){if(this.config__.maxWait){this.maxWaitTimerId__=setTimeout(this.flush,this.config__.maxWait)}if(this.config__.leading===true){this.invoke__()}}else{clearTimeout(this.timerId__)}this.timerId__=setTimeout(()=>{if(this.config__.trailing===true){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 createDebouncer(config){return new Debouncer(config)}export{Debouncer,createDebouncer};
3
3
  //# sourceMappingURL=main.mjs.map
package/dist/main.mjs.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/debounce.ts", "../src/main.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 * 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 AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private maxWaitTimerId__?: 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 `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 }\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", "import {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 AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"],
5
- "mappings": ";AAgCO,IAAM,UAAN,KAAuC,CAKrC,YAA6B,SAA8B,CAA9B,uBAClC,KAAK,SAAS,WAAa,IAC7B,CAMA,IAAW,WAAqB,CAC9B,OAAO,KAAK,YAAc,MAC5B,CAmBO,WAAW,KAA2B,CAC3C,KAAK,WAAa,KAClB,MAAM,aAAe,CAAC,KAAK,UAE3B,GAAI,aAAc,CAChB,GAAI,KAAK,SAAS,QAAS,CACzB,KAAK,iBAAmB,WAAW,IAAM,KAAK,MAAO,KAAK,SAAS,OAAO,CAC5E,CACA,GAAI,KAAK,SAAS,UAAY,KAAM,CAClC,KAAK,SAAS,CAChB,CACF,KACK,CACH,aAAa,KAAK,SAAU,CAC9B,CAEA,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,KAAM,CACnC,KAAK,SAAS,CAChB,CACA,KAAK,UAAU,CACjB,EAAG,KAAK,SAAS,KAAK,CACxB,CAkBO,QAAe,CACpB,GAAI,KAAK,UAAW,CAClB,aAAa,KAAK,SAAS,CAC7B,CACA,GAAI,KAAK,iBAAkB,CACzB,aAAa,KAAK,gBAAgB,CACpC,CACA,KAAK,UAAU,CACjB,CAKQ,WAAkB,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,UACd,CAoBO,OAAc,CACnB,GAAI,KAAK,UAAW,CAClB,KAAK,SAAS,CAChB,CACA,KAAK,OAAO,CACd,CAKQ,UAAiB,CACvB,GAAI,KAAK,WAAY,CAEnB,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,MACpB,CACF,CACF,EC9HO,SAAS,gBAAuC,OAA0C,CAC/F,OAAO,IAAI,UAAU,MAAM,CAC7B",
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 * 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 AnyFunction> {\n private timerId__?: number | NodeJS.Timeout;\n private maxWaitTimerId__?: number | NodeJS.Timeout;\n private lastArgs__?: Parameters<F>;\n\n public constructor(private readonly config__: DebouncerConfig<F>) {\n this.config__.trailing ??= true;\n this.flush = this.flush.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 }\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", "import {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 AnyFunction>(config: DebouncerConfig<F>): Debouncer<F> {\n return new Debouncer(config);\n}\n"],
5
+ "mappings": ";AAgCO,IAAM,UAAN,KAAuC,CAKrC,YAA6B,SAA8B,CAA9B,uBAClC,KAAK,SAAS,WAAa,KAC3B,KAAK,MAAQ,KAAK,MAAM,KAAK,IAAI,CACnC,CAMA,IAAW,WAAqB,CAC9B,OAAO,KAAK,YAAc,MAC5B,CAmBO,WAAW,KAA2B,CAC3C,KAAK,WAAa,KAClB,MAAM,aAAe,CAAC,KAAK,UAE3B,GAAI,aAAc,CAChB,GAAI,KAAK,SAAS,QAAS,CACzB,KAAK,iBAAmB,WAAW,KAAK,MAAO,KAAK,SAAS,OAAO,CACtE,CACA,GAAI,KAAK,SAAS,UAAY,KAAM,CAClC,KAAK,SAAS,CAChB,CACF,KACK,CACH,aAAa,KAAK,SAAU,CAC9B,CAEA,KAAK,UAAY,WAAW,IAAM,CAChC,GAAI,KAAK,SAAS,WAAa,KAAM,CACnC,KAAK,SAAS,CAChB,CACA,KAAK,UAAU,CACjB,EAAG,KAAK,SAAS,KAAK,CACxB,CAkBO,QAAe,CACpB,GAAI,KAAK,UAAW,CAClB,aAAa,KAAK,SAAS,CAC7B,CACA,GAAI,KAAK,iBAAkB,CACzB,aAAa,KAAK,gBAAgB,CACpC,CACA,KAAK,UAAU,CACjB,CAKQ,WAAkB,CACxB,OAAO,KAAK,UACZ,OAAO,KAAK,iBACZ,OAAO,KAAK,UACd,CAoBO,OAAc,CACnB,GAAI,KAAK,UAAW,CAClB,KAAK,SAAS,CAChB,CACA,KAAK,OAAO,CACd,CAKQ,UAAiB,CACvB,GAAI,KAAK,WAAY,CAEnB,KAAK,SAAS,KAAK,MAAM,KAAK,SAAS,YAAa,KAAK,UAAU,EACnE,KAAK,WAAa,MACpB,CACF,CACF,EC/HO,SAAS,gBAAuC,OAA0C,CAC/F,OAAO,IAAI,UAAU,MAAM,CAC7B",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@alwatr/debounce",
3
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.1.3",
4
+ "version": "1.1.4",
5
5
  "author": "S. Ali Mihandoost <ali.mihandoost@gmail.com>",
6
6
  "bugs": "https://github.com/Alwatr/nanolib/issues",
7
7
  "devDependencies": {
@@ -78,5 +78,5 @@
78
78
  },
79
79
  "type": "module",
80
80
  "types": "./dist/main.d.ts",
81
- "gitHead": "7af0290f1dc91cce15d1324c026354a81c7e64ca"
81
+ "gitHead": "530eaaa32d4d226c6509b2ff16c6e2283a1d7b48"
82
82
  }