@tanstack/angular-pacer 0.20.0 → 0.21.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 (94) hide show
  1. package/LICENSE +21 -0
  2. package/dist/async-batcher/index.d.cts +2 -2
  3. package/dist/async-batcher/index.d.ts +2 -2
  4. package/dist/async-batcher/injectAsyncBatcher.cjs +27 -3
  5. package/dist/async-batcher/injectAsyncBatcher.cjs.map +1 -1
  6. package/dist/async-batcher/injectAsyncBatcher.d.cts +25 -3
  7. package/dist/async-batcher/injectAsyncBatcher.d.ts +25 -3
  8. package/dist/async-batcher/injectAsyncBatcher.js +27 -3
  9. package/dist/async-batcher/injectAsyncBatcher.js.map +1 -1
  10. package/dist/async-debouncer/index.d.cts +2 -2
  11. package/dist/async-debouncer/index.d.ts +2 -2
  12. package/dist/async-debouncer/injectAsyncDebouncer.cjs +27 -3
  13. package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +1 -1
  14. package/dist/async-debouncer/injectAsyncDebouncer.d.cts +25 -3
  15. package/dist/async-debouncer/injectAsyncDebouncer.d.ts +25 -3
  16. package/dist/async-debouncer/injectAsyncDebouncer.js +27 -3
  17. package/dist/async-debouncer/injectAsyncDebouncer.js.map +1 -1
  18. package/dist/async-queuer/index.d.cts +2 -2
  19. package/dist/async-queuer/index.d.ts +2 -2
  20. package/dist/async-queuer/injectAsyncQueuer.cjs +27 -3
  21. package/dist/async-queuer/injectAsyncQueuer.cjs.map +1 -1
  22. package/dist/async-queuer/injectAsyncQueuer.d.cts +25 -3
  23. package/dist/async-queuer/injectAsyncQueuer.d.ts +25 -3
  24. package/dist/async-queuer/injectAsyncQueuer.js +27 -3
  25. package/dist/async-queuer/injectAsyncQueuer.js.map +1 -1
  26. package/dist/async-rate-limiter/index.d.cts +2 -2
  27. package/dist/async-rate-limiter/index.d.ts +2 -2
  28. package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +15 -3
  29. package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +1 -1
  30. package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +15 -3
  31. package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +15 -3
  32. package/dist/async-rate-limiter/injectAsyncRateLimiter.js +15 -3
  33. package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +1 -1
  34. package/dist/async-throttler/index.d.cts +2 -2
  35. package/dist/async-throttler/index.d.ts +2 -2
  36. package/dist/async-throttler/injectAsyncThrottler.cjs +27 -3
  37. package/dist/async-throttler/injectAsyncThrottler.cjs.map +1 -1
  38. package/dist/async-throttler/injectAsyncThrottler.d.cts +25 -3
  39. package/dist/async-throttler/injectAsyncThrottler.d.ts +25 -3
  40. package/dist/async-throttler/injectAsyncThrottler.js +27 -3
  41. package/dist/async-throttler/injectAsyncThrottler.js.map +1 -1
  42. package/dist/batcher/index.d.cts +2 -2
  43. package/dist/batcher/index.d.ts +2 -2
  44. package/dist/batcher/injectBatcher.cjs +22 -3
  45. package/dist/batcher/injectBatcher.cjs.map +1 -1
  46. package/dist/batcher/injectBatcher.d.cts +22 -3
  47. package/dist/batcher/injectBatcher.d.ts +22 -3
  48. package/dist/batcher/injectBatcher.js +22 -3
  49. package/dist/batcher/injectBatcher.js.map +1 -1
  50. package/dist/debouncer/index.d.cts +2 -2
  51. package/dist/debouncer/index.d.ts +2 -2
  52. package/dist/debouncer/injectDebouncer.cjs +22 -3
  53. package/dist/debouncer/injectDebouncer.cjs.map +1 -1
  54. package/dist/debouncer/injectDebouncer.d.cts +22 -3
  55. package/dist/debouncer/injectDebouncer.d.ts +22 -3
  56. package/dist/debouncer/injectDebouncer.js +22 -3
  57. package/dist/debouncer/injectDebouncer.js.map +1 -1
  58. package/dist/index.d.cts +11 -11
  59. package/dist/index.d.ts +11 -11
  60. package/dist/queuer/index.d.cts +2 -2
  61. package/dist/queuer/index.d.ts +2 -2
  62. package/dist/queuer/injectQueuer.cjs +22 -3
  63. package/dist/queuer/injectQueuer.cjs.map +1 -1
  64. package/dist/queuer/injectQueuer.d.cts +22 -3
  65. package/dist/queuer/injectQueuer.d.ts +22 -3
  66. package/dist/queuer/injectQueuer.js +22 -3
  67. package/dist/queuer/injectQueuer.js.map +1 -1
  68. package/dist/rate-limiter/index.d.cts +2 -2
  69. package/dist/rate-limiter/index.d.ts +2 -2
  70. package/dist/rate-limiter/injectRateLimiter.cjs +13 -3
  71. package/dist/rate-limiter/injectRateLimiter.cjs.map +1 -1
  72. package/dist/rate-limiter/injectRateLimiter.d.cts +13 -3
  73. package/dist/rate-limiter/injectRateLimiter.d.ts +13 -3
  74. package/dist/rate-limiter/injectRateLimiter.js +13 -3
  75. package/dist/rate-limiter/injectRateLimiter.js.map +1 -1
  76. package/dist/throttler/index.d.cts +2 -2
  77. package/dist/throttler/index.d.ts +2 -2
  78. package/dist/throttler/injectThrottler.cjs +22 -3
  79. package/dist/throttler/injectThrottler.cjs.map +1 -1
  80. package/dist/throttler/injectThrottler.d.cts +22 -3
  81. package/dist/throttler/injectThrottler.d.ts +22 -3
  82. package/dist/throttler/injectThrottler.js +22 -3
  83. package/dist/throttler/injectThrottler.js.map +1 -1
  84. package/package.json +15 -15
  85. package/src/async-batcher/injectAsyncBatcher.ts +43 -4
  86. package/src/async-debouncer/injectAsyncDebouncer.ts +43 -4
  87. package/src/async-queuer/injectAsyncQueuer.ts +43 -4
  88. package/src/async-rate-limiter/injectAsyncRateLimiter.ts +32 -4
  89. package/src/async-throttler/injectAsyncThrottler.ts +43 -4
  90. package/src/batcher/injectBatcher.ts +39 -4
  91. package/src/debouncer/injectDebouncer.ts +39 -4
  92. package/src/queuer/injectQueuer.ts +39 -4
  93. package/src/rate-limiter/injectRateLimiter.ts +28 -4
  94. package/src/throttler/injectThrottler.ts +39 -4
@@ -1,4 +1,5 @@
1
1
  const require_pacer_context = require('../provider/pacer-context.cjs');
2
+ let _angular_core = require("@angular/core");
2
3
  let _tanstack_angular_store = require("@tanstack/angular-store");
3
4
  let _tanstack_pacer_async_queuer = require("@tanstack/pacer/async-queuer");
4
5
 
@@ -22,6 +23,20 @@ let _tanstack_pacer_async_queuer = require("@tanstack/pacer/async-queuer");
22
23
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
23
24
  * full control over when your component tracks state changes.
24
25
  *
26
+ * ## Cleanup on Destroy
27
+ *
28
+ * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
29
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
30
+ *
31
+ * ```ts
32
+ * const queuer = injectAsyncQueuer(fn, {
33
+ * concurrency: 2,
34
+ * onUnmount: (q) => q.flush()
35
+ * });
36
+ * ```
37
+ *
38
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
39
+ *
25
40
  * @example
26
41
  * ```ts
27
42
  * // Default behavior - no reactive state subscriptions
@@ -45,15 +60,24 @@ let _tanstack_pacer_async_queuer = require("@tanstack/pacer/async-queuer");
45
60
  * ```
46
61
  */
47
62
  function injectAsyncQueuer(fn, options = {}, selector = () => ({})) {
48
- const queuer = new _tanstack_pacer_async_queuer.AsyncQueuer(fn, {
63
+ const mergedOptions = {
49
64
  ...require_pacer_context.injectPacerOptions().asyncQueuer,
50
65
  ...options
51
- });
66
+ };
67
+ const queuer = new _tanstack_pacer_async_queuer.AsyncQueuer(fn, mergedOptions);
52
68
  const state = (0, _tanstack_angular_store.injectStore)(queuer.store, selector);
53
- return {
69
+ const result = {
54
70
  ...queuer,
55
71
  state
56
72
  };
73
+ (0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
74
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
75
+ else {
76
+ queuer.stop();
77
+ queuer.abort();
78
+ }
79
+ });
80
+ return result;
57
81
  }
58
82
 
59
83
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"injectAsyncQueuer.cjs","names":["AsyncQueuer","injectPacerOptions"],"sources":["../../src/async-queuer/injectAsyncQueuer.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<\n AsyncQueuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the async queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncQueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncQueuer instance.\n *\n * This is a lower-level function that provides direct access to the AsyncQueuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The AsyncQueuer processes items asynchronously with support for concurrent execution,\n * promise-based processing, error handling, retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queuer = injectAsyncQueuer(\n * async (item: Data) => {\n * const response = await fetch('/api/process', {\n * method: 'POST',\n * body: JSON.stringify(item)\n * });\n * return response.json();\n * },\n * { concurrency: 2, wait: 1000 }\n * );\n *\n * // Add items\n * queuer.addItem(data1);\n * queuer.addItem(data2);\n *\n * // Access the selected state\n * const { items, isExecuting } = queuer.state();\n * ```\n */\nexport function injectAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n options: AsyncQueuerOptions<TValue> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncQueuer,\n ...options,\n } as AsyncQueuerOptions<TValue>\n\n const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)\n const state = injectStore(queuer.store, selector)\n\n return {\n ...queuer,\n state,\n } as AngularAsyncQueuer<TValue, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,SAAgB,kBACd,IACA,UAAsC,EAAE,EACxC,kBACG,EAAE,GACkC;CAMvC,MAAM,SAAS,IAAIA,yCAAoB,IALjB;EACpB,GAAGC,0CAAoB,CAAC;EACxB,GAAG;EACJ,CAEwD;CACzD,MAAM,iDAAoB,OAAO,OAAO,SAAS;AAEjD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"injectAsyncQueuer.cjs","names":["injectPacerOptions","AsyncQueuer","DestroyRef"],"sources":["../../src/async-queuer/injectAsyncQueuer.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface AngularAsyncQueuerOptions<\n TValue,\n TSelected = {},\n> extends AsyncQueuerOptions<TValue> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the queuer instance.\n * When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n */\n onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void\n}\n\nexport interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<\n AsyncQueuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the async queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncQueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncQueuer instance.\n *\n * This is a lower-level function that provides direct access to the AsyncQueuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The AsyncQueuer processes items asynchronously with support for concurrent execution,\n * promise-based processing, error handling, retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending items instead:\n *\n * ```ts\n * const queuer = injectAsyncQueuer(fn, {\n * concurrency: 2,\n * onUnmount: (q) => q.flush()\n * });\n * ```\n *\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queuer = injectAsyncQueuer(\n * async (item: Data) => {\n * const response = await fetch('/api/process', {\n * method: 'POST',\n * body: JSON.stringify(item)\n * });\n * return response.json();\n * },\n * { concurrency: 2, wait: 1000 }\n * );\n *\n * // Add items\n * queuer.addItem(data1);\n * queuer.addItem(data2);\n *\n * // Access the selected state\n * const { items, isExecuting } = queuer.state();\n * ```\n */\nexport function injectAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n options: AngularAsyncQueuerOptions<TValue, TSelected> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncQueuer,\n ...options,\n } as AngularAsyncQueuerOptions<TValue, TSelected>\n\n const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)\n const state = injectStore(queuer.store, selector)\n\n const result = {\n ...queuer,\n state,\n } as AngularAsyncQueuer<TValue, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n queuer.stop()\n queuer.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+FA,SAAgB,kBACd,IACA,UAAwD,EAAE,EAC1D,kBACG,EAAE,GACkC;CACvC,MAAM,gBAAgB;EACpB,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,SAAS,IAAIC,yCAAoB,IAAI,cAAc;CACzD,MAAM,iDAAoB,OAAO,OAAO,SAAS;CAEjD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,2BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;OAC1B;AACL,UAAO,MAAM;AACb,UAAO,OAAO;;GAEhB;AAEF,QAAO"}
@@ -1,8 +1,16 @@
1
1
  import { Signal } from "@angular/core";
2
- import { Store } from "@tanstack/store";
2
+ import { Store } from "@tanstack/angular-store";
3
3
  import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from "@tanstack/pacer/async-queuer";
4
4
 
5
5
  //#region src/async-queuer/injectAsyncQueuer.d.ts
6
+ interface AngularAsyncQueuerOptions<TValue, TSelected = {}> extends AsyncQueuerOptions<TValue> {
7
+ /**
8
+ * Optional callback invoked when the component is destroyed. Receives the queuer instance.
9
+ * When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.
10
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
11
+ */
12
+ onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void;
13
+ }
6
14
  interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TValue>, 'store'> {
7
15
  /**
8
16
  * Reactive state signal that will be updated when the async queuer state changes
@@ -35,6 +43,20 @@ interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TV
35
43
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
36
44
  * full control over when your component tracks state changes.
37
45
  *
46
+ * ## Cleanup on Destroy
47
+ *
48
+ * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
49
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
50
+ *
51
+ * ```ts
52
+ * const queuer = injectAsyncQueuer(fn, {
53
+ * concurrency: 2,
54
+ * onUnmount: (q) => q.flush()
55
+ * });
56
+ * ```
57
+ *
58
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
59
+ *
38
60
  * @example
39
61
  * ```ts
40
62
  * // Default behavior - no reactive state subscriptions
@@ -57,7 +79,7 @@ interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TV
57
79
  * const { items, isExecuting } = queuer.state();
58
80
  * ```
59
81
  */
60
- declare function injectAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): AngularAsyncQueuer<TValue, TSelected>;
82
+ declare function injectAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AngularAsyncQueuerOptions<TValue, TSelected>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): AngularAsyncQueuer<TValue, TSelected>;
61
83
  //#endregion
62
- export { AngularAsyncQueuer, injectAsyncQueuer };
84
+ export { AngularAsyncQueuer, AngularAsyncQueuerOptions, injectAsyncQueuer };
63
85
  //# sourceMappingURL=injectAsyncQueuer.d.cts.map
@@ -1,8 +1,16 @@
1
1
  import { Signal } from "@angular/core";
2
+ import { Store } from "@tanstack/angular-store";
2
3
  import { AsyncQueuer, AsyncQueuerOptions, AsyncQueuerState } from "@tanstack/pacer/async-queuer";
3
- import { Store } from "@tanstack/store";
4
4
 
5
5
  //#region src/async-queuer/injectAsyncQueuer.d.ts
6
+ interface AngularAsyncQueuerOptions<TValue, TSelected = {}> extends AsyncQueuerOptions<TValue> {
7
+ /**
8
+ * Optional callback invoked when the component is destroyed. Receives the queuer instance.
9
+ * When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.
10
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
11
+ */
12
+ onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void;
13
+ }
6
14
  interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TValue>, 'store'> {
7
15
  /**
8
16
  * Reactive state signal that will be updated when the async queuer state changes
@@ -35,6 +43,20 @@ interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TV
35
43
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
36
44
  * full control over when your component tracks state changes.
37
45
  *
46
+ * ## Cleanup on Destroy
47
+ *
48
+ * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
49
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
50
+ *
51
+ * ```ts
52
+ * const queuer = injectAsyncQueuer(fn, {
53
+ * concurrency: 2,
54
+ * onUnmount: (q) => q.flush()
55
+ * });
56
+ * ```
57
+ *
58
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
59
+ *
38
60
  * @example
39
61
  * ```ts
40
62
  * // Default behavior - no reactive state subscriptions
@@ -57,7 +79,7 @@ interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<AsyncQueuer<TV
57
79
  * const { items, isExecuting } = queuer.state();
58
80
  * ```
59
81
  */
60
- declare function injectAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AsyncQueuerOptions<TValue>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): AngularAsyncQueuer<TValue, TSelected>;
82
+ declare function injectAsyncQueuer<TValue, TSelected = {}>(fn: (value: TValue) => Promise<any>, options?: AngularAsyncQueuerOptions<TValue, TSelected>, selector?: (state: AsyncQueuerState<TValue>) => TSelected): AngularAsyncQueuer<TValue, TSelected>;
61
83
  //#endregion
62
- export { AngularAsyncQueuer, injectAsyncQueuer };
84
+ export { AngularAsyncQueuer, AngularAsyncQueuerOptions, injectAsyncQueuer };
63
85
  //# sourceMappingURL=injectAsyncQueuer.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import { injectPacerOptions } from "../provider/pacer-context.js";
2
+ import { DestroyRef, inject } from "@angular/core";
2
3
  import { injectStore } from "@tanstack/angular-store";
3
4
  import { AsyncQueuer } from "@tanstack/pacer/async-queuer";
4
5
 
@@ -22,6 +23,20 @@ import { AsyncQueuer } from "@tanstack/pacer/async-queuer";
22
23
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
23
24
  * full control over when your component tracks state changes.
24
25
  *
26
+ * ## Cleanup on Destroy
27
+ *
28
+ * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
29
+ * Use the `onUnmount` option to customize this. For example, to flush pending items instead:
30
+ *
31
+ * ```ts
32
+ * const queuer = injectAsyncQueuer(fn, {
33
+ * concurrency: 2,
34
+ * onUnmount: (q) => q.flush()
35
+ * });
36
+ * ```
37
+ *
38
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
39
+ *
25
40
  * @example
26
41
  * ```ts
27
42
  * // Default behavior - no reactive state subscriptions
@@ -45,15 +60,24 @@ import { AsyncQueuer } from "@tanstack/pacer/async-queuer";
45
60
  * ```
46
61
  */
47
62
  function injectAsyncQueuer(fn, options = {}, selector = () => ({})) {
48
- const queuer = new AsyncQueuer(fn, {
63
+ const mergedOptions = {
49
64
  ...injectPacerOptions().asyncQueuer,
50
65
  ...options
51
- });
66
+ };
67
+ const queuer = new AsyncQueuer(fn, mergedOptions);
52
68
  const state = injectStore(queuer.store, selector);
53
- return {
69
+ const result = {
54
70
  ...queuer,
55
71
  state
56
72
  };
73
+ inject(DestroyRef, { optional: true })?.onDestroy(() => {
74
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
75
+ else {
76
+ queuer.stop();
77
+ queuer.abort();
78
+ }
79
+ });
80
+ return result;
57
81
  }
58
82
 
59
83
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"injectAsyncQueuer.js","names":[],"sources":["../../src/async-queuer/injectAsyncQueuer.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<\n AsyncQueuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the async queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncQueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncQueuer instance.\n *\n * This is a lower-level function that provides direct access to the AsyncQueuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The AsyncQueuer processes items asynchronously with support for concurrent execution,\n * promise-based processing, error handling, retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queuer = injectAsyncQueuer(\n * async (item: Data) => {\n * const response = await fetch('/api/process', {\n * method: 'POST',\n * body: JSON.stringify(item)\n * });\n * return response.json();\n * },\n * { concurrency: 2, wait: 1000 }\n * );\n *\n * // Add items\n * queuer.addItem(data1);\n * queuer.addItem(data2);\n *\n * // Access the selected state\n * const { items, isExecuting } = queuer.state();\n * ```\n */\nexport function injectAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n options: AsyncQueuerOptions<TValue> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncQueuer,\n ...options,\n } as AsyncQueuerOptions<TValue>\n\n const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)\n const state = injectStore(queuer.store, selector)\n\n return {\n ...queuer,\n state,\n } as AngularAsyncQueuer<TValue, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoEA,SAAgB,kBACd,IACA,UAAsC,EAAE,EACxC,kBACG,EAAE,GACkC;CAMvC,MAAM,SAAS,IAAI,YAAoB,IALjB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ,CAEwD;CACzD,MAAM,QAAQ,YAAY,OAAO,OAAO,SAAS;AAEjD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"injectAsyncQueuer.js","names":[],"sources":["../../src/async-queuer/injectAsyncQueuer.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncQueuer } from '@tanstack/pacer/async-queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type {\n AsyncQueuerOptions,\n AsyncQueuerState,\n} from '@tanstack/pacer/async-queuer'\n\nexport interface AngularAsyncQueuerOptions<\n TValue,\n TSelected = {},\n> extends AsyncQueuerOptions<TValue> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the queuer instance.\n * When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n */\n onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void\n}\n\nexport interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<\n AsyncQueuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the async queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncQueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncQueuer instance.\n *\n * This is a lower-level function that provides direct access to the AsyncQueuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The AsyncQueuer processes items asynchronously with support for concurrent execution,\n * promise-based processing, error handling, retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function stops the queuer and aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending items instead:\n *\n * ```ts\n * const queuer = injectAsyncQueuer(fn, {\n * concurrency: 2,\n * onUnmount: (q) => q.flush()\n * });\n * ```\n *\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queuer = injectAsyncQueuer(\n * async (item: Data) => {\n * const response = await fetch('/api/process', {\n * method: 'POST',\n * body: JSON.stringify(item)\n * });\n * return response.json();\n * },\n * { concurrency: 2, wait: 1000 }\n * );\n *\n * // Add items\n * queuer.addItem(data1);\n * queuer.addItem(data2);\n *\n * // Access the selected state\n * const { items, isExecuting } = queuer.state();\n * ```\n */\nexport function injectAsyncQueuer<TValue, TSelected = {}>(\n fn: (value: TValue) => Promise<any>,\n options: AngularAsyncQueuerOptions<TValue, TSelected> = {},\n selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncQueuer,\n ...options,\n } as AngularAsyncQueuerOptions<TValue, TSelected>\n\n const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)\n const state = injectStore(queuer.store, selector)\n\n const result = {\n ...queuer,\n state,\n } as AngularAsyncQueuer<TValue, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n queuer.stop()\n queuer.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+FA,SAAgB,kBACd,IACA,UAAwD,EAAE,EAC1D,kBACG,EAAE,GACkC;CACvC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,SAAS,IAAI,YAAoB,IAAI,cAAc;CACzD,MAAM,QAAQ,YAAY,OAAO,OAAO,SAAS;CAEjD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,CADmB,OAAO,YAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;OAC1B;AACL,UAAO,MAAM;AACb,UAAO,OAAO;;GAEhB;AAEF,QAAO"}
@@ -1,4 +1,4 @@
1
1
  import { injectAsyncRateLimitedCallback } from "./injectAsyncRateLimitedCallback.cjs";
2
- import { AngularAsyncRateLimiter, injectAsyncRateLimiter } from "./injectAsyncRateLimiter.cjs";
2
+ import { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimiter } from "./injectAsyncRateLimiter.cjs";
3
3
  export * from "@tanstack/pacer/async-rate-limiter";
4
- export { AngularAsyncRateLimiter, injectAsyncRateLimitedCallback, injectAsyncRateLimiter };
4
+ export { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimitedCallback, injectAsyncRateLimiter };
@@ -1,4 +1,4 @@
1
1
  import { injectAsyncRateLimitedCallback } from "./injectAsyncRateLimitedCallback.js";
2
- import { AngularAsyncRateLimiter, injectAsyncRateLimiter } from "./injectAsyncRateLimiter.js";
2
+ import { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimiter } from "./injectAsyncRateLimiter.js";
3
3
  export * from "@tanstack/pacer/async-rate-limiter";
4
- export { AngularAsyncRateLimiter, injectAsyncRateLimitedCallback, injectAsyncRateLimiter };
4
+ export { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimitedCallback, injectAsyncRateLimiter };
@@ -1,4 +1,5 @@
1
1
  const require_pacer_context = require('../provider/pacer-context.cjs');
2
+ let _angular_core = require("@angular/core");
2
3
  let _tanstack_angular_store = require("@tanstack/angular-store");
3
4
  let _tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-limiter");
4
5
 
@@ -22,6 +23,11 @@ let _tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-lim
22
23
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
23
24
  * full control over when your component tracks state changes.
24
25
  *
26
+ * ## Cleanup on Destroy
27
+ *
28
+ * By default, the function aborts in-flight work when the component is destroyed.
29
+ * Use the `onUnmount` option to customize this.
30
+ *
25
31
  * @example
26
32
  * ```ts
27
33
  * // Default behavior - no reactive state subscriptions
@@ -41,15 +47,21 @@ let _tanstack_pacer_async_rate_limiter = require("@tanstack/pacer/async-rate-lim
41
47
  * ```
42
48
  */
43
49
  function injectAsyncRateLimiter(fn, options, selector = () => ({})) {
44
- const rateLimiter = new _tanstack_pacer_async_rate_limiter.AsyncRateLimiter(fn, {
50
+ const mergedOptions = {
45
51
  ...require_pacer_context.injectPacerOptions().asyncRateLimiter,
46
52
  ...options
47
- });
53
+ };
54
+ const rateLimiter = new _tanstack_pacer_async_rate_limiter.AsyncRateLimiter(fn, mergedOptions);
48
55
  const state = (0, _tanstack_angular_store.injectStore)(rateLimiter.store, selector);
49
- return {
56
+ const result = {
50
57
  ...rateLimiter,
51
58
  state
52
59
  };
60
+ (0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
61
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
62
+ else rateLimiter.abort();
63
+ });
64
+ return result;
53
65
  }
54
66
 
55
67
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"injectAsyncRateLimiter.cjs","names":["AsyncRateLimiter","injectPacerOptions"],"sources":["../../src/async-rate-limiter/injectAsyncRateLimiter.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface AngularAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncRateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async rate limiting functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectAsyncRateLimiter(\n * async (id: string) => {\n * const response = await fetch(`/api/data/${id}`);\n * return response.json();\n * },\n * { limit: 5, window: 60000, windowType: 'sliding' }\n * );\n *\n * // In an event handler\n * const handleRequest = async (id: string) => {\n * const result = await rateLimiter.maybeExecute(id);\n * console.log('Result:', result);\n * };\n * ```\n */\nexport function injectAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AsyncRateLimiterOptions<TFn>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncRateLimiter,\n ...options,\n } as AsyncRateLimiterOptions<TFn>\n\n const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)\n const state = injectStore(rateLimiter.store, selector)\n\n return {\n ...rateLimiter,\n state,\n } as AngularAsyncRateLimiter<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACoC;CAMzC,MAAM,cAAc,IAAIA,oDAAsB,IALxB;EACpB,GAAGC,0CAAoB,CAAC;EACxB,GAAG;EACJ,CAE+D;CAChE,MAAM,iDAAoB,YAAY,OAAO,SAAS;AAEtD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"injectAsyncRateLimiter.cjs","names":["injectPacerOptions","AsyncRateLimiter","DestroyRef"],"sources":["../../src/async-rate-limiter/injectAsyncRateLimiter.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface AngularAsyncRateLimiterOptions<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends AsyncRateLimiterOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.\n * When provided, replaces the default cleanup (abort).\n */\n onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void\n}\n\nexport interface AngularAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncRateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async rate limiting functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectAsyncRateLimiter(\n * async (id: string) => {\n * const response = await fetch(`/api/data/${id}`);\n * return response.json();\n * },\n * { limit: 5, window: 60000, windowType: 'sliding' }\n * );\n *\n * // In an event handler\n * const handleRequest = async (id: string) => {\n * const result = await rateLimiter.maybeExecute(id);\n * console.log('Result:', result);\n * };\n * ```\n */\nexport function injectAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AngularAsyncRateLimiterOptions<TFn, TSelected>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncRateLimiter,\n ...options,\n } as AngularAsyncRateLimiterOptions<TFn, TSelected>\n\n const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)\n const state = injectStore(rateLimiter.store, selector)\n\n const result = {\n ...rateLimiter,\n state,\n } as AngularAsyncRateLimiter<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n rateLimiter.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkFA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACoC;CACzC,MAAM,gBAAgB;EACpB,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,cAAc,IAAIC,oDAAsB,IAAI,cAAc;CAChE,MAAM,iDAAoB,YAAY,OAAO,SAAS;CAEtD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,2BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;MAE/B,aAAY,OAAO;GAErB;AAEF,QAAO"}
@@ -1,9 +1,16 @@
1
1
  import { Signal } from "@angular/core";
2
- import { Store } from "@tanstack/store";
2
+ import { Store } from "@tanstack/angular-store";
3
3
  import { AnyAsyncFunction } from "@tanstack/pacer/types";
4
4
  import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from "@tanstack/pacer/async-rate-limiter";
5
5
 
6
6
  //#region src/async-rate-limiter/injectAsyncRateLimiter.d.ts
7
+ interface AngularAsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TSelected = {}> extends AsyncRateLimiterOptions<TFn> {
8
+ /**
9
+ * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
10
+ * When provided, replaces the default cleanup (abort).
11
+ */
12
+ onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void;
13
+ }
7
14
  interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
8
15
  /**
9
16
  * Reactive state signal that will be updated when the async rate limiter state changes
@@ -36,6 +43,11 @@ interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>
36
43
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
37
44
  * full control over when your component tracks state changes.
38
45
  *
46
+ * ## Cleanup on Destroy
47
+ *
48
+ * By default, the function aborts in-flight work when the component is destroyed.
49
+ * Use the `onUnmount` option to customize this.
50
+ *
39
51
  * @example
40
52
  * ```ts
41
53
  * // Default behavior - no reactive state subscriptions
@@ -54,7 +66,7 @@ interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>
54
66
  * };
55
67
  * ```
56
68
  */
57
- declare function injectAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): AngularAsyncRateLimiter<TFn, TSelected>;
69
+ declare function injectAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AngularAsyncRateLimiterOptions<TFn, TSelected>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): AngularAsyncRateLimiter<TFn, TSelected>;
58
70
  //#endregion
59
- export { AngularAsyncRateLimiter, injectAsyncRateLimiter };
71
+ export { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimiter };
60
72
  //# sourceMappingURL=injectAsyncRateLimiter.d.cts.map
@@ -1,9 +1,16 @@
1
1
  import { Signal } from "@angular/core";
2
+ import { Store } from "@tanstack/angular-store";
2
3
  import { AsyncRateLimiter, AsyncRateLimiterOptions, AsyncRateLimiterState } from "@tanstack/pacer/async-rate-limiter";
3
4
  import { AnyAsyncFunction } from "@tanstack/pacer/types";
4
- import { Store } from "@tanstack/store";
5
5
 
6
6
  //#region src/async-rate-limiter/injectAsyncRateLimiter.d.ts
7
+ interface AngularAsyncRateLimiterOptions<TFn extends AnyAsyncFunction, TSelected = {}> extends AsyncRateLimiterOptions<TFn> {
8
+ /**
9
+ * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
10
+ * When provided, replaces the default cleanup (abort).
11
+ */
12
+ onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void;
13
+ }
7
14
  interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
8
15
  /**
9
16
  * Reactive state signal that will be updated when the async rate limiter state changes
@@ -36,6 +43,11 @@ interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>
36
43
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
37
44
  * full control over when your component tracks state changes.
38
45
  *
46
+ * ## Cleanup on Destroy
47
+ *
48
+ * By default, the function aborts in-flight work when the component is destroyed.
49
+ * Use the `onUnmount` option to customize this.
50
+ *
39
51
  * @example
40
52
  * ```ts
41
53
  * // Default behavior - no reactive state subscriptions
@@ -54,7 +66,7 @@ interface AngularAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>
54
66
  * };
55
67
  * ```
56
68
  */
57
- declare function injectAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AsyncRateLimiterOptions<TFn>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): AngularAsyncRateLimiter<TFn, TSelected>;
69
+ declare function injectAsyncRateLimiter<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AngularAsyncRateLimiterOptions<TFn, TSelected>, selector?: (state: AsyncRateLimiterState<TFn>) => TSelected): AngularAsyncRateLimiter<TFn, TSelected>;
58
70
  //#endregion
59
- export { AngularAsyncRateLimiter, injectAsyncRateLimiter };
71
+ export { AngularAsyncRateLimiter, AngularAsyncRateLimiterOptions, injectAsyncRateLimiter };
60
72
  //# sourceMappingURL=injectAsyncRateLimiter.d.ts.map
@@ -1,4 +1,5 @@
1
1
  import { injectPacerOptions } from "../provider/pacer-context.js";
2
+ import { DestroyRef, inject } from "@angular/core";
2
3
  import { injectStore } from "@tanstack/angular-store";
3
4
  import { AsyncRateLimiter } from "@tanstack/pacer/async-rate-limiter";
4
5
 
@@ -22,6 +23,11 @@ import { AsyncRateLimiter } from "@tanstack/pacer/async-rate-limiter";
22
23
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
23
24
  * full control over when your component tracks state changes.
24
25
  *
26
+ * ## Cleanup on Destroy
27
+ *
28
+ * By default, the function aborts in-flight work when the component is destroyed.
29
+ * Use the `onUnmount` option to customize this.
30
+ *
25
31
  * @example
26
32
  * ```ts
27
33
  * // Default behavior - no reactive state subscriptions
@@ -41,15 +47,21 @@ import { AsyncRateLimiter } from "@tanstack/pacer/async-rate-limiter";
41
47
  * ```
42
48
  */
43
49
  function injectAsyncRateLimiter(fn, options, selector = () => ({})) {
44
- const rateLimiter = new AsyncRateLimiter(fn, {
50
+ const mergedOptions = {
45
51
  ...injectPacerOptions().asyncRateLimiter,
46
52
  ...options
47
- });
53
+ };
54
+ const rateLimiter = new AsyncRateLimiter(fn, mergedOptions);
48
55
  const state = injectStore(rateLimiter.store, selector);
49
- return {
56
+ const result = {
50
57
  ...rateLimiter,
51
58
  state
52
59
  };
60
+ inject(DestroyRef, { optional: true })?.onDestroy(() => {
61
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
62
+ else rateLimiter.abort();
63
+ });
64
+ return result;
53
65
  }
54
66
 
55
67
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"injectAsyncRateLimiter.js","names":[],"sources":["../../src/async-rate-limiter/injectAsyncRateLimiter.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface AngularAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncRateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async rate limiting functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectAsyncRateLimiter(\n * async (id: string) => {\n * const response = await fetch(`/api/data/${id}`);\n * return response.json();\n * },\n * { limit: 5, window: 60000, windowType: 'sliding' }\n * );\n *\n * // In an event handler\n * const handleRequest = async (id: string) => {\n * const result = await rateLimiter.maybeExecute(id);\n * console.log('Result:', result);\n * };\n * ```\n */\nexport function injectAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AsyncRateLimiterOptions<TFn>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncRateLimiter,\n ...options,\n } as AsyncRateLimiterOptions<TFn>\n\n const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)\n const state = injectStore(rateLimiter.store, selector)\n\n return {\n ...rateLimiter,\n state,\n } as AngularAsyncRateLimiter<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACoC;CAMzC,MAAM,cAAc,IAAI,iBAAsB,IALxB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ,CAE+D;CAChE,MAAM,QAAQ,YAAY,YAAY,OAAO,SAAS;AAEtD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"injectAsyncRateLimiter.js","names":[],"sources":["../../src/async-rate-limiter/injectAsyncRateLimiter.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncRateLimiterOptions,\n AsyncRateLimiterState,\n} from '@tanstack/pacer/async-rate-limiter'\n\nexport interface AngularAsyncRateLimiterOptions<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends AsyncRateLimiterOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.\n * When provided, replaces the default cleanup (abort).\n */\n onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void\n}\n\nexport interface AngularAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncRateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncRateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async rate limiting functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectAsyncRateLimiter(\n * async (id: string) => {\n * const response = await fetch(`/api/data/${id}`);\n * return response.json();\n * },\n * { limit: 5, window: 60000, windowType: 'sliding' }\n * );\n *\n * // In an event handler\n * const handleRequest = async (id: string) => {\n * const result = await rateLimiter.maybeExecute(id);\n * console.log('Result:', result);\n * };\n * ```\n */\nexport function injectAsyncRateLimiter<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AngularAsyncRateLimiterOptions<TFn, TSelected>,\n selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncRateLimiter,\n ...options,\n } as AngularAsyncRateLimiterOptions<TFn, TSelected>\n\n const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)\n const state = injectStore(rateLimiter.store, selector)\n\n const result = {\n ...rateLimiter,\n state,\n } as AngularAsyncRateLimiter<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n rateLimiter.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkFA,SAAgB,uBAId,IACA,SACA,kBACG,EAAE,GACoC;CACzC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,cAAc,IAAI,iBAAsB,IAAI,cAAc;CAChE,MAAM,QAAQ,YAAY,YAAY,OAAO,SAAS;CAEtD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,CADmB,OAAO,YAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;MAE/B,aAAY,OAAO;GAErB;AAEF,QAAO"}
@@ -1,4 +1,4 @@
1
1
  import { injectAsyncThrottledCallback } from "./injectAsyncThrottledCallback.cjs";
2
- import { AngularAsyncThrottler, injectAsyncThrottler } from "./injectAsyncThrottler.cjs";
2
+ import { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottler } from "./injectAsyncThrottler.cjs";
3
3
  export * from "@tanstack/pacer/async-throttler";
4
- export { AngularAsyncThrottler, injectAsyncThrottledCallback, injectAsyncThrottler };
4
+ export { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottledCallback, injectAsyncThrottler };
@@ -1,4 +1,4 @@
1
1
  import { injectAsyncThrottledCallback } from "./injectAsyncThrottledCallback.js";
2
- import { AngularAsyncThrottler, injectAsyncThrottler } from "./injectAsyncThrottler.js";
2
+ import { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottler } from "./injectAsyncThrottler.js";
3
3
  export * from "@tanstack/pacer/async-throttler";
4
- export { AngularAsyncThrottler, injectAsyncThrottledCallback, injectAsyncThrottler };
4
+ export { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottledCallback, injectAsyncThrottler };
@@ -1,4 +1,5 @@
1
1
  const require_pacer_context = require('../provider/pacer-context.cjs');
2
+ let _angular_core = require("@angular/core");
2
3
  let _tanstack_angular_store = require("@tanstack/angular-store");
3
4
  let _tanstack_pacer_async_throttler = require("@tanstack/pacer/async-throttler");
4
5
 
@@ -24,6 +25,20 @@ let _tanstack_pacer_async_throttler = require("@tanstack/pacer/async-throttler")
24
25
  * tracking by providing a selector function. This prevents unnecessary updates and gives you
25
26
  * full control over when your component tracks state changes.
26
27
  *
28
+ * ## Cleanup on Destroy
29
+ *
30
+ * By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
31
+ * Use the `onUnmount` option to customize this. For example, to flush pending work instead:
32
+ *
33
+ * ```ts
34
+ * const throttler = injectAsyncThrottler(fn, {
35
+ * wait: 1000,
36
+ * onUnmount: (t) => t.flush()
37
+ * });
38
+ * ```
39
+ *
40
+ * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
41
+ *
27
42
  * @example
28
43
  * ```ts
29
44
  * // Default behavior - no reactive state subscriptions
@@ -46,15 +61,24 @@ let _tanstack_pacer_async_throttler = require("@tanstack/pacer/async-throttler")
46
61
  * ```
47
62
  */
48
63
  function injectAsyncThrottler(fn, options, selector = () => ({})) {
49
- const throttler = new _tanstack_pacer_async_throttler.AsyncThrottler(fn, {
64
+ const mergedOptions = {
50
65
  ...require_pacer_context.injectPacerOptions().asyncThrottler,
51
66
  ...options
52
- });
67
+ };
68
+ const throttler = new _tanstack_pacer_async_throttler.AsyncThrottler(fn, mergedOptions);
53
69
  const state = (0, _tanstack_angular_store.injectStore)(throttler.store, selector);
54
- return {
70
+ const result = {
55
71
  ...throttler,
56
72
  state
57
73
  };
74
+ (0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
75
+ if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
76
+ else {
77
+ throttler.cancel();
78
+ throttler.abort();
79
+ }
80
+ });
81
+ return result;
58
82
  }
59
83
 
60
84
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"injectAsyncThrottler.cjs","names":["AsyncThrottler","injectPacerOptions"],"sources":["../../src/async-throttler/injectAsyncThrottler.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface AngularAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncThrottler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncThrottler instance.\n *\n * This is a lower-level function that provides direct access to the AsyncThrottler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async throttling functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * The throttler will execute the function at most once within the specified wait time.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectAsyncThrottler(\n * async (data: Data) => {\n * const response = await fetch('/api/update', {\n * method: 'POST',\n * body: JSON.stringify(data)\n * });\n * return response.json();\n * },\n * { wait: 1000 }\n * );\n *\n * // In an event handler\n * const handleUpdate = async (data: Data) => {\n * const result = await throttler.maybeExecute(data);\n * console.log('Update result:', result);\n * };\n * ```\n */\nexport function injectAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AsyncThrottlerOptions<TFn>,\n selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncThrottler,\n ...options,\n } as AsyncThrottlerOptions<TFn>\n\n const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)\n const state = injectStore(throttler.store, selector)\n\n return {\n ...throttler,\n state,\n } as AngularAsyncThrottler<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsEA,SAAgB,qBAId,IACA,SACA,kBACG,EAAE,GACkC;CAMvC,MAAM,YAAY,IAAIA,+CAAoB,IALpB;EACpB,GAAGC,0CAAoB,CAAC;EACxB,GAAG;EACJ,CAE2D;CAC5D,MAAM,iDAAoB,UAAU,OAAO,SAAS;AAEpD,QAAO;EACL,GAAG;EACH;EACD"}
1
+ {"version":3,"file":"injectAsyncThrottler.cjs","names":["injectPacerOptions","AsyncThrottler","DestroyRef"],"sources":["../../src/async-throttler/injectAsyncThrottler.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface AngularAsyncThrottlerOptions<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends AsyncThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n */\n onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void\n}\n\nexport interface AngularAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncThrottler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncThrottler instance.\n *\n * This is a lower-level function that provides direct access to the AsyncThrottler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async throttling functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * The throttler will execute the function at most once within the specified wait time.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```ts\n * const throttler = injectAsyncThrottler(fn, {\n * wait: 1000,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectAsyncThrottler(\n * async (data: Data) => {\n * const response = await fetch('/api/update', {\n * method: 'POST',\n * body: JSON.stringify(data)\n * });\n * return response.json();\n * },\n * { wait: 1000 }\n * );\n *\n * // In an event handler\n * const handleUpdate = async (data: Data) => {\n * const result = await throttler.maybeExecute(data);\n * console.log('Update result:', result);\n * };\n * ```\n */\nexport function injectAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AngularAsyncThrottlerOptions<TFn, TSelected>,\n selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncThrottler,\n ...options,\n } as AngularAsyncThrottlerOptions<TFn, TSelected>\n\n const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)\n const state = injectStore(throttler.store, selector)\n\n const result = {\n ...throttler,\n state,\n } as AngularAsyncThrottler<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n throttler.cancel()\n throttler.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiGA,SAAgB,qBAId,IACA,SACA,kBACG,EAAE,GACkC;CACvC,MAAM,gBAAgB;EACpB,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAIC,+CAAoB,IAAI,cAAc;CAC5D,MAAM,iDAAoB,UAAU,OAAO,SAAS;CAEpD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,2BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;OAC1B;AACL,aAAU,QAAQ;AAClB,aAAU,OAAO;;GAEnB;AAEF,QAAO"}