@tanstack/pacer 0.1.0 → 0.2.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.
Files changed (74) hide show
  1. package/dist/cjs/async-debouncer.cjs +60 -44
  2. package/dist/cjs/async-debouncer.cjs.map +1 -1
  3. package/dist/cjs/async-debouncer.d.cts +37 -24
  4. package/dist/cjs/async-queuer.cjs +149 -125
  5. package/dist/cjs/async-queuer.cjs.map +1 -1
  6. package/dist/cjs/async-queuer.d.cts +65 -48
  7. package/dist/cjs/async-rate-limiter.cjs +63 -46
  8. package/dist/cjs/async-rate-limiter.cjs.map +1 -1
  9. package/dist/cjs/async-rate-limiter.d.cts +39 -27
  10. package/dist/cjs/async-throttler.cjs +70 -47
  11. package/dist/cjs/async-throttler.cjs.map +1 -1
  12. package/dist/cjs/async-throttler.d.cts +43 -25
  13. package/dist/cjs/debouncer.cjs +46 -22
  14. package/dist/cjs/debouncer.cjs.map +1 -1
  15. package/dist/cjs/debouncer.d.cts +25 -11
  16. package/dist/cjs/index.cjs +2 -0
  17. package/dist/cjs/index.cjs.map +1 -1
  18. package/dist/cjs/index.d.cts +2 -0
  19. package/dist/cjs/queuer.cjs +114 -104
  20. package/dist/cjs/queuer.cjs.map +1 -1
  21. package/dist/cjs/queuer.d.cts +53 -40
  22. package/dist/cjs/rate-limiter.cjs +54 -42
  23. package/dist/cjs/rate-limiter.cjs.map +1 -1
  24. package/dist/cjs/rate-limiter.d.cts +37 -45
  25. package/dist/cjs/throttler.cjs +61 -41
  26. package/dist/cjs/throttler.cjs.map +1 -1
  27. package/dist/cjs/throttler.d.cts +35 -22
  28. package/dist/cjs/types.d.cts +12 -0
  29. package/dist/cjs/utils.cjs +13 -0
  30. package/dist/cjs/utils.cjs.map +1 -0
  31. package/dist/cjs/utils.d.cts +1 -0
  32. package/dist/esm/async-debouncer.d.ts +37 -24
  33. package/dist/esm/async-debouncer.js +60 -44
  34. package/dist/esm/async-debouncer.js.map +1 -1
  35. package/dist/esm/async-queuer.d.ts +65 -48
  36. package/dist/esm/async-queuer.js +149 -125
  37. package/dist/esm/async-queuer.js.map +1 -1
  38. package/dist/esm/async-rate-limiter.d.ts +39 -27
  39. package/dist/esm/async-rate-limiter.js +63 -46
  40. package/dist/esm/async-rate-limiter.js.map +1 -1
  41. package/dist/esm/async-throttler.d.ts +43 -25
  42. package/dist/esm/async-throttler.js +70 -47
  43. package/dist/esm/async-throttler.js.map +1 -1
  44. package/dist/esm/debouncer.d.ts +25 -11
  45. package/dist/esm/debouncer.js +46 -22
  46. package/dist/esm/debouncer.js.map +1 -1
  47. package/dist/esm/index.d.ts +2 -0
  48. package/dist/esm/index.js +2 -0
  49. package/dist/esm/index.js.map +1 -1
  50. package/dist/esm/queuer.d.ts +53 -40
  51. package/dist/esm/queuer.js +114 -104
  52. package/dist/esm/queuer.js.map +1 -1
  53. package/dist/esm/rate-limiter.d.ts +37 -45
  54. package/dist/esm/rate-limiter.js +54 -42
  55. package/dist/esm/rate-limiter.js.map +1 -1
  56. package/dist/esm/throttler.d.ts +35 -22
  57. package/dist/esm/throttler.js +61 -41
  58. package/dist/esm/throttler.js.map +1 -1
  59. package/dist/esm/types.d.ts +12 -0
  60. package/dist/esm/utils.d.ts +1 -0
  61. package/dist/esm/utils.js +13 -0
  62. package/dist/esm/utils.js.map +1 -0
  63. package/package.json +8 -1
  64. package/src/async-debouncer.ts +90 -62
  65. package/src/async-queuer.ts +178 -145
  66. package/src/async-rate-limiter.ts +93 -67
  67. package/src/async-throttler.ts +98 -63
  68. package/src/debouncer.ts +71 -35
  69. package/src/index.ts +2 -0
  70. package/src/queuer.ts +135 -118
  71. package/src/rate-limiter.ts +79 -81
  72. package/src/throttler.ts +87 -61
  73. package/src/types.ts +17 -0
  74. package/src/utils.ts +13 -0
@@ -6,17 +6,19 @@ const defaultOptions = {
6
6
  trailing: true,
7
7
  onError: () => {
8
8
  },
9
+ onExecute: () => {
10
+ },
9
11
  wait: 0
10
12
  };
11
13
  class AsyncDebouncer {
12
14
  constructor(fn, initialOptions) {
13
15
  this.fn = fn;
14
- this.abortController = null;
15
- this.executionCount = 0;
16
- this.isExecuting = false;
17
- this.timeoutId = null;
18
- this.canLeadingExecute = true;
19
- this.options = {
16
+ this._abortController = null;
17
+ this._executionCount = 0;
18
+ this._isExecuting = false;
19
+ this._timeoutId = null;
20
+ this._canLeadingExecute = true;
21
+ this._options = {
20
22
  ...defaultOptions,
21
23
  ...initialOptions
22
24
  };
@@ -26,32 +28,17 @@ class AsyncDebouncer {
26
28
  * Returns the new options state
27
29
  */
28
30
  setOptions(newOptions) {
29
- this.options = {
30
- ...this.options,
31
+ this._options = {
32
+ ...this._options,
31
33
  ...newOptions
32
34
  };
33
- return this.options;
35
+ return this._options;
34
36
  }
35
37
  /**
36
- * Returns the number of times the function has been executed
38
+ * Returns the current debouncer options
37
39
  */
38
- getExecutionCount() {
39
- return this.executionCount;
40
- }
41
- /**
42
- * Cancels any pending execution
43
- */
44
- cancel() {
45
- if (this.timeoutId) {
46
- clearTimeout(this.timeoutId);
47
- this.timeoutId = null;
48
- }
49
- if (this.abortController) {
50
- this.abortController.abort();
51
- this.abortController = null;
52
- }
53
- this.lastArgs = void 0;
54
- this.canLeadingExecute = true;
40
+ getOptions() {
41
+ return this._options;
55
42
  }
56
43
  /**
57
44
  * Attempts to execute the debounced function
@@ -59,45 +46,74 @@ class AsyncDebouncer {
59
46
  */
60
47
  async maybeExecute(...args) {
61
48
  this.cancel();
62
- this.lastArgs = args;
63
- if (this.options.leading && this.canLeadingExecute) {
64
- this.canLeadingExecute = false;
49
+ this._lastArgs = args;
50
+ if (this._options.leading && this._canLeadingExecute) {
51
+ this._canLeadingExecute = false;
65
52
  await this.executeFunction(...args);
66
53
  }
67
54
  return new Promise((resolve) => {
68
- this.timeoutId = setTimeout(async () => {
69
- if (this.isExecuting) {
55
+ this._timeoutId = setTimeout(async () => {
56
+ if (this._isExecuting) {
70
57
  resolve();
71
58
  return;
72
59
  }
73
- this.canLeadingExecute = true;
74
- if (this.options.trailing) {
75
- this.abortController = new AbortController();
60
+ this._canLeadingExecute = true;
61
+ if (this._options.trailing) {
62
+ this._abortController = new AbortController();
76
63
  try {
77
- this.isExecuting = true;
78
- if (this.lastArgs) {
79
- await this.executeFunction(...this.lastArgs);
64
+ this._isExecuting = true;
65
+ if (this._lastArgs) {
66
+ await this.executeFunction(...this._lastArgs);
80
67
  }
81
68
  } catch (error) {
82
69
  try {
83
- this.options.onError(error);
70
+ this._options.onError(error);
84
71
  } catch {
72
+ console.error("Error in error handler", error);
85
73
  }
86
74
  } finally {
87
- this.isExecuting = false;
88
- this.abortController = null;
75
+ this._isExecuting = false;
76
+ this._abortController = null;
89
77
  resolve();
90
78
  }
91
79
  } else {
92
80
  resolve();
93
81
  }
94
- }, this.options.wait);
82
+ }, this._options.wait);
95
83
  });
96
84
  }
97
85
  async executeFunction(...args) {
98
- if (!this.options.enabled) return;
99
- this.executionCount++;
86
+ if (!this._options.enabled) return;
87
+ this._executionCount++;
100
88
  await this.fn(...args);
89
+ this._options.onExecute(this);
90
+ }
91
+ /**
92
+ * Cancels any pending execution
93
+ */
94
+ cancel() {
95
+ if (this._timeoutId) {
96
+ clearTimeout(this._timeoutId);
97
+ this._timeoutId = null;
98
+ }
99
+ if (this._abortController) {
100
+ this._abortController.abort();
101
+ this._abortController = null;
102
+ }
103
+ this._lastArgs = void 0;
104
+ this._canLeadingExecute = true;
105
+ }
106
+ /**
107
+ * Returns the number of times the function has been executed
108
+ */
109
+ getExecutionCount() {
110
+ return this._executionCount;
111
+ }
112
+ /**
113
+ * Returns `true` if there is a pending execution
114
+ */
115
+ getIsPending() {
116
+ return this._options.enabled && (this._timeoutId !== null || this._isExecuting);
101
117
  }
102
118
  }
103
119
  function asyncDebounce(fn, initialOptions) {
@@ -1 +1 @@
1
- {"version":3,"file":"async-debouncer.cjs","sources":["../../src/async-debouncer.ts"],"sourcesContent":["/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing\n * Defaults to 0ms\n */\n wait: number\n /**\n * Optional error handler for when the debounced function throws\n */\n onError?: (error: unknown) => void\n}\n\nconst defaultOptions: Required<AsyncDebouncerOptions> = {\n enabled: true,\n leading: false,\n trailing: true,\n onError: () => {},\n wait: 0,\n}\n\n/**\n * A class that creates an async debounced function.\n *\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * @example\n * ```ts\n * const debouncer = new AsyncDebouncer(async (value: string) => {\n * await searchAPI(value);\n * }, { wait: 500 });\n *\n * // Called on each keystroke but only executes after 500ms of no typing\n * inputElement.addEventListener('input', () => {\n * debouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncDebouncer<\n TFn extends (...args: Array<any>) => Promise<any>,\n TArgs extends Parameters<TFn>,\n> {\n private abortController: AbortController | null = null\n private executionCount = 0\n private isExecuting = false\n private lastArgs: TArgs | undefined\n private options: Required<AsyncDebouncerOptions>\n private timeoutId: ReturnType<typeof setTimeout> | null = null\n private canLeadingExecute = true\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncDebouncerOptions,\n ) {\n this.options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the debouncer options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<AsyncDebouncerOptions>,\n ): Required<AsyncDebouncerOptions> {\n this.options = {\n ...this.options,\n ...newOptions,\n }\n return this.options\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this.executionCount\n }\n\n /**\n * Cancels any pending execution\n */\n cancel(): void {\n if (this.timeoutId) {\n clearTimeout(this.timeoutId)\n this.timeoutId = null\n }\n if (this.abortController) {\n this.abortController.abort()\n this.abortController = null\n }\n this.lastArgs = undefined\n this.canLeadingExecute = true\n }\n\n /**\n * Attempts to execute the debounced function\n * If a call is already in progress, it will be queued\n */\n async maybeExecute(...args: TArgs): Promise<void> {\n this.cancel()\n this.lastArgs = args\n\n // Handle leading execution\n if (this.options.leading && this.canLeadingExecute) {\n this.canLeadingExecute = false\n await this.executeFunction(...args)\n }\n\n return new Promise((resolve) => {\n this.timeoutId = setTimeout(async () => {\n if (this.isExecuting) {\n resolve()\n return\n }\n\n this.canLeadingExecute = true\n // Execute trailing only if enabled\n if (this.options.trailing) {\n this.abortController = new AbortController()\n try {\n this.isExecuting = true\n if (this.lastArgs) {\n await this.executeFunction(...this.lastArgs)\n }\n } catch (error) {\n try {\n this.options.onError(error)\n } catch {\n // Ignore errors from error handler\n }\n } finally {\n this.isExecuting = false\n this.abortController = null\n resolve()\n }\n } else {\n resolve()\n }\n }, this.options.wait)\n })\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this.options.enabled) return\n this.executionCount++\n await this.fn(...args)\n }\n}\n\n/**\n * Creates an async debounced function that delays execution until after a specified wait time.\n * The debounced function will only execute once the wait period has elapsed without any new calls.\n * If called again during the wait period, the timer resets and a new wait period begins.\n *\n * @example\n * ```ts\n * const debounced = asyncDebounce(async (value: string) => {\n * await saveToAPI(value);\n * }, { wait: 1000 });\n *\n * // Will only execute once, 1 second after the last call\n * await debounced(\"first\"); // Cancelled\n * await debounced(\"second\"); // Cancelled\n * await debounced(\"third\"); // Executes after 1s\n * ```\n */\nexport function asyncDebounce<\n TFn extends (...args: Array<any>) => Promise<any>,\n>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions, 'enabled'>) {\n const asyncDebounc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,aAAa;AACZ,kBAAA;AACR;AAAA,QAAA;AAGF,aAAK,oBAAoB;AAErB,YAAA,KAAK,QAAQ,UAAU;AACpB,eAAA,kBAAkB,IAAI,gBAAgB;AACvC,cAAA;AACF,iBAAK,cAAc;AACnB,gBAAI,KAAK,UAAU;AACjB,oBAAM,KAAK,gBAAgB,GAAG,KAAK,QAAQ;AAAA,YAAA;AAAA,mBAEtC,OAAO;AACV,gBAAA;AACG,mBAAA,QAAQ,QAAQ,KAAK;AAAA,YAAA,QACpB;AAAA,YAAA;AAAA,UAER,UACA;AACA,iBAAK,cAAc;AACnB,iBAAK,kBAAkB;AACf,oBAAA;AAAA,UAAA;AAAA,QACV,OACK;AACG,kBAAA;AAAA,QAAA;AAAA,MACV,GACC,KAAK,QAAQ,IAAI;AAAA,IAAA,CACrB;AAAA,EAAA;AAAA,EAGH,MAAc,mBAAmB,MAA4B;AACvD,QAAA,CAAC,KAAK,QAAQ,QAAS;AACtB,SAAA;AACC,UAAA,KAAK,GAAG,GAAG,IAAI;AAAA,EAAA;AAEzB;AAmBgB,SAAA,cAEd,IAAS,gBAAwD;AACjE,QAAM,iBAAiB,IAAI,eAAe,IAAI,cAAc;AACrD,SAAA,eAAe,aAAa,KAAK,cAAc;AACxD;;;"}
1
+ {"version":3,"file":"async-debouncer.cjs","sources":["../../src/async-debouncer.ts"],"sourcesContent":["import type { AnyAsyncFunction } from './types'\n\n/**\n * Options for configuring an async debounced function\n */\nexport interface AsyncDebouncerOptions<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n /**\n * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.\n * Defaults to true.\n */\n enabled?: boolean\n /**\n * Whether to execute on the leading edge of the timeout.\n * Defaults to false.\n */\n leading?: boolean\n /**\n * Optional error handler for when the debounced function throws\n */\n onError?: (error: unknown) => void\n /**\n * Optional function to call when the debounced function is executed\n */\n onExecute?: (debouncer: AsyncDebouncer<TFn, TArgs>) => void\n /**\n * Whether to execute on the trailing edge of the timeout.\n * Defaults to true.\n */\n trailing?: boolean\n /**\n * Delay in milliseconds to wait after the last call before executing\n * Defaults to 0ms\n */\n wait: number\n}\n\nconst defaultOptions: Required<AsyncDebouncerOptions<any, any>> = {\n enabled: true,\n leading: false,\n trailing: true,\n onError: () => {},\n onExecute: () => {},\n wait: 0,\n}\n\n/**\n * A class that creates an async debounced function.\n *\n * Debouncing ensures that a function is only executed after a specified delay has passed since its last invocation.\n * Each new invocation resets the delay timer. This is useful for handling frequent events like window resizing\n * or input changes where you only want to execute the handler after the events have stopped occurring.\n *\n * Unlike throttling which allows execution at regular intervals, debouncing prevents any execution until\n * the function stops being called for the specified delay period.\n *\n * @example\n * ```ts\n * const asyncDebouncer = new AsyncDebouncer(async (value: string) => {\n * await searchAPI(value);\n * }, { wait: 500 });\n *\n * // Called on each keystroke but only executes after 500ms of no typing\n * inputElement.addEventListener('input', () => {\n * asyncDebouncer.maybeExecute(inputElement.value);\n * });\n * ```\n */\nexport class AsyncDebouncer<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n> {\n private _abortController: AbortController | null = null\n private _executionCount = 0\n private _isExecuting = false\n private _lastArgs: TArgs | undefined\n private _options: Required<AsyncDebouncerOptions<TFn, TArgs>>\n private _timeoutId: ReturnType<typeof setTimeout> | null = null\n private _canLeadingExecute = true\n\n constructor(\n private fn: TFn,\n initialOptions: AsyncDebouncerOptions<TFn, TArgs>,\n ) {\n this._options = {\n ...defaultOptions,\n ...initialOptions,\n }\n }\n\n /**\n * Updates the debouncer options\n * Returns the new options state\n */\n setOptions(\n newOptions: Partial<AsyncDebouncerOptions<TFn, TArgs>>,\n ): Required<AsyncDebouncerOptions<TFn, TArgs>> {\n this._options = {\n ...this._options,\n ...newOptions,\n }\n return this._options\n }\n\n /**\n * Returns the current debouncer options\n */\n getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>> {\n return this._options\n }\n\n /**\n * Attempts to execute the debounced function\n * If a call is already in progress, it will be queued\n */\n async maybeExecute(...args: TArgs): Promise<void> {\n this.cancel()\n this._lastArgs = args\n\n // Handle leading execution\n if (this._options.leading && this._canLeadingExecute) {\n this._canLeadingExecute = false\n await this.executeFunction(...args)\n }\n\n return new Promise((resolve) => {\n this._timeoutId = setTimeout(async () => {\n if (this._isExecuting) {\n resolve()\n return\n }\n\n this._canLeadingExecute = true\n // Execute trailing only if enabled\n if (this._options.trailing) {\n this._abortController = new AbortController()\n try {\n this._isExecuting = true\n if (this._lastArgs) {\n await this.executeFunction(...this._lastArgs)\n }\n } catch (error) {\n try {\n this._options.onError(error)\n } catch {\n console.error('Error in error handler', error)\n }\n } finally {\n this._isExecuting = false\n this._abortController = null\n resolve()\n }\n } else {\n resolve()\n }\n }, this._options.wait)\n })\n }\n\n private async executeFunction(...args: TArgs): Promise<void> {\n if (!this._options.enabled) return\n this._executionCount++\n await this.fn(...args)\n this._options.onExecute(this)\n }\n\n /**\n * Cancels any pending execution\n */\n cancel(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId)\n this._timeoutId = null\n }\n if (this._abortController) {\n this._abortController.abort()\n this._abortController = null\n }\n this._lastArgs = undefined\n this._canLeadingExecute = true\n }\n\n /**\n * Returns the number of times the function has been executed\n */\n getExecutionCount(): number {\n return this._executionCount\n }\n\n /**\n * Returns `true` if there is a pending execution\n */\n getIsPending(): boolean {\n return (\n this._options.enabled && (this._timeoutId !== null || this._isExecuting)\n )\n }\n}\n\n/**\n * Creates an async debounced function that delays execution until after a specified wait time.\n * The debounced function will only execute once the wait period has elapsed without any new calls.\n * If called again during the wait period, the timer resets and a new wait period begins.\n *\n * @example\n * ```ts\n * const debounced = asyncDebounce(async (value: string) => {\n * await saveToAPI(value);\n * }, { wait: 1000 });\n *\n * // Will only execute once, 1 second after the last call\n * await debounced(\"first\"); // Cancelled\n * await debounced(\"second\"); // Cancelled\n * await debounced(\"third\"); // Executes after 1s\n * ```\n */\nexport function asyncDebounce<\n TFn extends AnyAsyncFunction,\n TArgs extends Parameters<TFn>,\n>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions<TFn, TArgs>, 'enabled'>) {\n const asyncDebouncer = new AsyncDebouncer(fn, initialOptions)\n return asyncDebouncer.maybeExecute.bind(asyncDebouncer)\n}\n"],"names":[],"mappings":";;AAuCA,MAAM,iBAA4D;AAAA,EAChE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,UAAU;AAAA,EACV,SAAS,MAAM;AAAA,EAAC;AAAA,EAChB,WAAW,MAAM;AAAA,EAAC;AAAA,EAClB,MAAM;AACR;AAwBO,MAAM,eAGX;AAAA,EASA,YACU,IACR,gBACA;AAFQ,SAAA,KAAA;AATV,SAAQ,mBAA2C;AACnD,SAAQ,kBAAkB;AAC1B,SAAQ,eAAe;AAGvB,SAAQ,aAAmD;AAC3D,SAAQ,qBAAqB;AAM3B,SAAK,WAAW;AAAA,MACd,GAAG;AAAA,MACH,GAAG;AAAA,IACL;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOF,WACE,YAC6C;AAC7C,SAAK,WAAW;AAAA,MACd,GAAG,KAAK;AAAA,MACR,GAAG;AAAA,IACL;AACA,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA,EAMd,aAA0D;AACxD,WAAO,KAAK;AAAA,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOd,MAAM,gBAAgB,MAA4B;AAChD,SAAK,OAAO;AACZ,SAAK,YAAY;AAGjB,QAAI,KAAK,SAAS,WAAW,KAAK,oBAAoB;AACpD,WAAK,qBAAqB;AACpB,YAAA,KAAK,gBAAgB,GAAG,IAAI;AAAA,IAAA;AAG7B,WAAA,IAAI,QAAQ,CAAC,YAAY;AACzB,WAAA,aAAa,WAAW,YAAY;AACvC,YAAI,KAAK,cAAc;AACb,kBAAA;AACR;AAAA,QAAA;AAGF,aAAK,qBAAqB;AAEtB,YAAA,KAAK,SAAS,UAAU;AACrB,eAAA,mBAAmB,IAAI,gBAAgB;AACx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
4
  */
4
- export interface AsyncDebouncerOptions {
5
+ export interface AsyncDebouncerOptions<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>> {
5
6
  /**
6
7
  * Whether the debouncer is enabled. When disabled, maybeExecute will not trigger any executions.
7
8
  * Defaults to true.
@@ -12,6 +13,14 @@ export interface AsyncDebouncerOptions {
12
13
  * Defaults to false.
13
14
  */
14
15
  leading?: boolean;
16
+ /**
17
+ * Optional error handler for when the debounced function throws
18
+ */
19
+ onError?: (error: unknown) => void;
20
+ /**
21
+ * Optional function to call when the debounced function is executed
22
+ */
23
+ onExecute?: (debouncer: AsyncDebouncer<TFn, TArgs>) => void;
15
24
  /**
16
25
  * Whether to execute on the trailing edge of the timeout.
17
26
  * Defaults to true.
@@ -22,10 +31,6 @@ export interface AsyncDebouncerOptions {
22
31
  * Defaults to 0ms
23
32
  */
24
33
  wait: number;
25
- /**
26
- * Optional error handler for when the debounced function throws
27
- */
28
- onError?: (error: unknown) => void;
29
34
  }
30
35
  /**
31
36
  * A class that creates an async debounced function.
@@ -39,45 +44,53 @@ export interface AsyncDebouncerOptions {
39
44
  *
40
45
  * @example
41
46
  * ```ts
42
- * const debouncer = new AsyncDebouncer(async (value: string) => {
47
+ * const asyncDebouncer = new AsyncDebouncer(async (value: string) => {
43
48
  * await searchAPI(value);
44
49
  * }, { wait: 500 });
45
50
  *
46
51
  * // Called on each keystroke but only executes after 500ms of no typing
47
52
  * inputElement.addEventListener('input', () => {
48
- * debouncer.maybeExecute(inputElement.value);
53
+ * asyncDebouncer.maybeExecute(inputElement.value);
49
54
  * });
50
55
  * ```
51
56
  */
52
- export declare class AsyncDebouncer<TFn extends (...args: Array<any>) => Promise<any>, TArgs extends Parameters<TFn>> {
57
+ export declare class AsyncDebouncer<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>> {
53
58
  private fn;
54
- private abortController;
55
- private executionCount;
56
- private isExecuting;
57
- private lastArgs;
58
- private options;
59
- private timeoutId;
60
- private canLeadingExecute;
61
- constructor(fn: TFn, initialOptions: AsyncDebouncerOptions);
59
+ private _abortController;
60
+ private _executionCount;
61
+ private _isExecuting;
62
+ private _lastArgs;
63
+ private _options;
64
+ private _timeoutId;
65
+ private _canLeadingExecute;
66
+ constructor(fn: TFn, initialOptions: AsyncDebouncerOptions<TFn, TArgs>);
62
67
  /**
63
68
  * Updates the debouncer options
64
69
  * Returns the new options state
65
70
  */
66
- setOptions(newOptions: Partial<AsyncDebouncerOptions>): Required<AsyncDebouncerOptions>;
67
- /**
68
- * Returns the number of times the function has been executed
69
- */
70
- getExecutionCount(): number;
71
+ setOptions(newOptions: Partial<AsyncDebouncerOptions<TFn, TArgs>>): Required<AsyncDebouncerOptions<TFn, TArgs>>;
71
72
  /**
72
- * Cancels any pending execution
73
+ * Returns the current debouncer options
73
74
  */
74
- cancel(): void;
75
+ getOptions(): Required<AsyncDebouncerOptions<TFn, TArgs>>;
75
76
  /**
76
77
  * Attempts to execute the debounced function
77
78
  * If a call is already in progress, it will be queued
78
79
  */
79
80
  maybeExecute(...args: TArgs): Promise<void>;
80
81
  private executeFunction;
82
+ /**
83
+ * Cancels any pending execution
84
+ */
85
+ cancel(): void;
86
+ /**
87
+ * Returns the number of times the function has been executed
88
+ */
89
+ getExecutionCount(): number;
90
+ /**
91
+ * Returns `true` if there is a pending execution
92
+ */
93
+ getIsPending(): boolean;
81
94
  }
82
95
  /**
83
96
  * Creates an async debounced function that delays execution until after a specified wait time.
@@ -96,4 +109,4 @@ export declare class AsyncDebouncer<TFn extends (...args: Array<any>) => Promise
96
109
  * await debounced("third"); // Executes after 1s
97
110
  * ```
98
111
  */
99
- export declare function asyncDebounce<TFn extends (...args: Array<any>) => Promise<any>>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions, 'enabled'>): (...args: Parameters<TFn>) => Promise<void>;
112
+ export declare function asyncDebounce<TFn extends AnyAsyncFunction, TArgs extends Parameters<TFn>>(fn: TFn, initialOptions: Omit<AsyncDebouncerOptions<TFn, TArgs>, 'enabled'>): (...args: TArgs) => Promise<void>;