@tanstack/angular-pacer 0.20.0 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +7 -7
- package/dist/async-batcher/index.d.cts +2 -2
- package/dist/async-batcher/index.d.ts +2 -2
- package/dist/async-batcher/injectAsyncBatcher.cjs +27 -3
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +1 -1
- package/dist/async-batcher/injectAsyncBatcher.d.cts +25 -3
- package/dist/async-batcher/injectAsyncBatcher.d.ts +25 -3
- package/dist/async-batcher/injectAsyncBatcher.js +27 -3
- package/dist/async-batcher/injectAsyncBatcher.js.map +1 -1
- package/dist/async-debouncer/index.d.cts +2 -2
- package/dist/async-debouncer/index.d.ts +2 -2
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +27 -3
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +1 -1
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +25 -3
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +25 -3
- package/dist/async-debouncer/injectAsyncDebouncer.js +27 -3
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +1 -1
- package/dist/async-queuer/index.d.cts +2 -2
- package/dist/async-queuer/index.d.ts +2 -2
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +1 -1
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +5 -10
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +5 -10
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +1 -1
- package/dist/async-queuer/injectAsyncQueuer.cjs +27 -3
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +1 -1
- package/dist/async-queuer/injectAsyncQueuer.d.cts +25 -3
- package/dist/async-queuer/injectAsyncQueuer.d.ts +25 -3
- package/dist/async-queuer/injectAsyncQueuer.js +27 -3
- package/dist/async-queuer/injectAsyncQueuer.js.map +1 -1
- package/dist/async-rate-limiter/index.d.cts +2 -2
- package/dist/async-rate-limiter/index.d.ts +2 -2
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +15 -3
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +1 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +15 -3
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +15 -3
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +15 -3
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +1 -1
- package/dist/async-throttler/index.d.cts +2 -2
- package/dist/async-throttler/index.d.ts +2 -2
- package/dist/async-throttler/injectAsyncThrottler.cjs +27 -3
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +1 -1
- package/dist/async-throttler/injectAsyncThrottler.d.cts +25 -3
- package/dist/async-throttler/injectAsyncThrottler.d.ts +25 -3
- package/dist/async-throttler/injectAsyncThrottler.js +27 -3
- package/dist/async-throttler/injectAsyncThrottler.js.map +1 -1
- package/dist/batcher/index.d.cts +2 -2
- package/dist/batcher/index.d.ts +2 -2
- package/dist/batcher/injectBatcher.cjs +22 -3
- package/dist/batcher/injectBatcher.cjs.map +1 -1
- package/dist/batcher/injectBatcher.d.cts +22 -3
- package/dist/batcher/injectBatcher.d.ts +22 -3
- package/dist/batcher/injectBatcher.js +22 -3
- package/dist/batcher/injectBatcher.js.map +1 -1
- package/dist/debouncer/index.d.cts +2 -2
- package/dist/debouncer/index.d.ts +2 -2
- package/dist/debouncer/injectDebouncedSignal.cjs.map +1 -1
- package/dist/debouncer/injectDebouncedSignal.d.cts +5 -10
- package/dist/debouncer/injectDebouncedSignal.d.ts +5 -10
- package/dist/debouncer/injectDebouncedSignal.js.map +1 -1
- package/dist/debouncer/injectDebouncer.cjs +22 -3
- package/dist/debouncer/injectDebouncer.cjs.map +1 -1
- package/dist/debouncer/injectDebouncer.d.cts +22 -3
- package/dist/debouncer/injectDebouncer.d.ts +22 -3
- package/dist/debouncer/injectDebouncer.js +22 -3
- package/dist/debouncer/injectDebouncer.js.map +1 -1
- package/dist/index.d.cts +11 -11
- package/dist/index.d.ts +11 -11
- package/dist/queuer/index.d.cts +2 -2
- package/dist/queuer/index.d.ts +2 -2
- package/dist/queuer/injectQueuedSignal.cjs.map +1 -1
- package/dist/queuer/injectQueuedSignal.d.cts +5 -10
- package/dist/queuer/injectQueuedSignal.d.ts +5 -10
- package/dist/queuer/injectQueuedSignal.js.map +1 -1
- package/dist/queuer/injectQueuer.cjs +22 -3
- package/dist/queuer/injectQueuer.cjs.map +1 -1
- package/dist/queuer/injectQueuer.d.cts +22 -3
- package/dist/queuer/injectQueuer.d.ts +22 -3
- package/dist/queuer/injectQueuer.js +22 -3
- package/dist/queuer/injectQueuer.js.map +1 -1
- package/dist/rate-limiter/index.d.cts +2 -2
- package/dist/rate-limiter/index.d.ts +2 -2
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +1 -1
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +5 -10
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +5 -10
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +1 -1
- package/dist/rate-limiter/injectRateLimiter.cjs +13 -3
- package/dist/rate-limiter/injectRateLimiter.cjs.map +1 -1
- package/dist/rate-limiter/injectRateLimiter.d.cts +13 -3
- package/dist/rate-limiter/injectRateLimiter.d.ts +13 -3
- package/dist/rate-limiter/injectRateLimiter.js +13 -3
- package/dist/rate-limiter/injectRateLimiter.js.map +1 -1
- package/dist/throttler/index.d.cts +2 -2
- package/dist/throttler/index.d.ts +2 -2
- package/dist/throttler/injectThrottledSignal.cjs.map +1 -1
- package/dist/throttler/injectThrottledSignal.d.cts +5 -10
- package/dist/throttler/injectThrottledSignal.d.ts +5 -10
- package/dist/throttler/injectThrottledSignal.js.map +1 -1
- package/dist/throttler/injectThrottler.cjs +22 -3
- package/dist/throttler/injectThrottler.cjs.map +1 -1
- package/dist/throttler/injectThrottler.d.cts +22 -3
- package/dist/throttler/injectThrottler.d.ts +22 -3
- package/dist/throttler/injectThrottler.js +22 -3
- package/dist/throttler/injectThrottler.js.map +1 -1
- package/package.json +15 -15
- package/src/async-batcher/injectAsyncBatcher.ts +43 -4
- package/src/async-debouncer/injectAsyncDebouncer.ts +43 -4
- package/src/async-queuer/injectAsyncQueuedSignal.ts +4 -12
- package/src/async-queuer/injectAsyncQueuer.ts +43 -4
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +32 -4
- package/src/async-throttler/injectAsyncThrottler.ts +43 -4
- package/src/batcher/injectBatcher.ts +39 -4
- package/src/debouncer/injectDebouncedSignal.ts +4 -11
- package/src/debouncer/injectDebouncer.ts +39 -4
- package/src/queuer/injectQueuedSignal.ts +4 -9
- package/src/queuer/injectQueuer.ts +39 -4
- package/src/rate-limiter/injectRateLimitedSignal.ts +4 -11
- package/src/rate-limiter/injectRateLimiter.ts +28 -4
- package/src/throttler/injectThrottledSignal.ts +4 -11
- package/src/throttler/injectThrottler.ts +39 -4
|
@@ -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 { AnyFunction } from "@tanstack/pacer/types";
|
|
4
4
|
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
5
5
|
|
|
6
6
|
//#region src/throttler/injectThrottler.d.ts
|
|
7
|
+
interface AngularThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
*/
|
|
12
|
+
onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void;
|
|
13
|
+
}
|
|
7
14
|
interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
15
|
/**
|
|
9
16
|
* Reactive state signal that will be updated when the throttler state changes
|
|
@@ -49,6 +56,18 @@ interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
49
56
|
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
50
57
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
51
58
|
*
|
|
59
|
+
* ## Cleanup on Destroy
|
|
60
|
+
*
|
|
61
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
62
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const throttler = injectThrottler(fn, {
|
|
66
|
+
* wait: 100,
|
|
67
|
+
* onUnmount: (t) => t.flush()
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
52
71
|
* @example
|
|
53
72
|
* ```ts
|
|
54
73
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -73,7 +92,7 @@ interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
73
92
|
* const { isPending } = throttler.state();
|
|
74
93
|
* ```
|
|
75
94
|
*/
|
|
76
|
-
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
95
|
+
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): AngularThrottler<TFn, TSelected>;
|
|
77
96
|
//#endregion
|
|
78
|
-
export { AngularThrottler, injectThrottler };
|
|
97
|
+
export { AngularThrottler, AngularThrottlerOptions, injectThrottler };
|
|
79
98
|
//# sourceMappingURL=injectThrottler.d.cts.map
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
2
3
|
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
3
4
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Store } from "@tanstack/store";
|
|
5
5
|
|
|
6
6
|
//#region src/throttler/injectThrottler.d.ts
|
|
7
|
+
interface AngularThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
*/
|
|
12
|
+
onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void;
|
|
13
|
+
}
|
|
7
14
|
interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
15
|
/**
|
|
9
16
|
* Reactive state signal that will be updated when the throttler state changes
|
|
@@ -49,6 +56,18 @@ interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
49
56
|
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
50
57
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
51
58
|
*
|
|
59
|
+
* ## Cleanup on Destroy
|
|
60
|
+
*
|
|
61
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
62
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
63
|
+
*
|
|
64
|
+
* ```ts
|
|
65
|
+
* const throttler = injectThrottler(fn, {
|
|
66
|
+
* wait: 100,
|
|
67
|
+
* onUnmount: (t) => t.flush()
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
52
71
|
* @example
|
|
53
72
|
* ```ts
|
|
54
73
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -73,7 +92,7 @@ interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
73
92
|
* const { isPending } = throttler.state();
|
|
74
93
|
* ```
|
|
75
94
|
*/
|
|
76
|
-
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
95
|
+
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): AngularThrottler<TFn, TSelected>;
|
|
77
96
|
//#endregion
|
|
78
|
-
export { AngularThrottler, injectThrottler };
|
|
97
|
+
export { AngularThrottler, AngularThrottlerOptions, injectThrottler };
|
|
79
98
|
//# sourceMappingURL=injectThrottler.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 { Throttler } from "@tanstack/pacer/throttler";
|
|
4
5
|
|
|
@@ -35,6 +36,18 @@ import { Throttler } from "@tanstack/pacer/throttler";
|
|
|
35
36
|
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
36
37
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
37
38
|
*
|
|
39
|
+
* ## Cleanup on Destroy
|
|
40
|
+
*
|
|
41
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
42
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* const throttler = injectThrottler(fn, {
|
|
46
|
+
* wait: 100,
|
|
47
|
+
* onUnmount: (t) => t.flush()
|
|
48
|
+
* });
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
38
51
|
* @example
|
|
39
52
|
* ```ts
|
|
40
53
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -60,15 +73,21 @@ import { Throttler } from "@tanstack/pacer/throttler";
|
|
|
60
73
|
* ```
|
|
61
74
|
*/
|
|
62
75
|
function injectThrottler(fn, options, selector = () => ({})) {
|
|
63
|
-
const
|
|
76
|
+
const mergedOptions = {
|
|
64
77
|
...injectPacerOptions().throttler,
|
|
65
78
|
...options
|
|
66
|
-
}
|
|
79
|
+
};
|
|
80
|
+
const throttler = new Throttler(fn, mergedOptions);
|
|
67
81
|
const state = injectStore(throttler.store, selector);
|
|
68
|
-
|
|
82
|
+
const result = {
|
|
69
83
|
...throttler,
|
|
70
84
|
state
|
|
71
85
|
};
|
|
86
|
+
inject(DestroyRef, { optional: true })?.onDestroy(() => {
|
|
87
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
88
|
+
else throttler.cancel();
|
|
89
|
+
});
|
|
90
|
+
return result;
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottler.js","names":[],"sources":["../../src/throttler/injectThrottler.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { Throttler } from '@tanstack/pacer/throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface AngularThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the 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<ThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages a Throttler instance.\n *\n * This is a lower-level function that provides direct access to the Throttler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides throttling functionality to limit how often a function can be called,\n * ensuring it executes at most once within a specified time window.\n *\n * The throttler will execute the function immediately (if leading is enabled) and then\n * prevent further executions until the wait period has elapsed.\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 * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 }\n * );\n *\n * // Opt-in to track isPending changes (optimized for loading states)\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // In an event handler\n * window.addEventListener('scroll', () => {\n * throttler.maybeExecute(window.scrollY);\n * });\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = throttler.state();\n * ```\n */\nexport function injectThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options:
|
|
1
|
+
{"version":3,"file":"injectThrottler.js","names":[],"sources":["../../src/throttler/injectThrottler.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { Throttler } from '@tanstack/pacer/throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface AngularThrottlerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends ThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.\n */\n onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void\n}\n\nexport interface AngularThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the 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<ThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages a Throttler instance.\n *\n * This is a lower-level function that provides direct access to the Throttler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides throttling functionality to limit how often a function can be called,\n * ensuring it executes at most once within a specified time window.\n *\n * The throttler will execute the function immediately (if leading is enabled) and then\n * prevent further executions until the wait period has elapsed.\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 * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending execution 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 = injectThrottler(fn, {\n * wait: 100,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 }\n * );\n *\n * // Opt-in to track isPending changes (optimized for loading states)\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // In an event handler\n * window.addEventListener('scroll', () => {\n * throttler.maybeExecute(window.scrollY);\n * });\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = throttler.state();\n * ```\n */\nexport function injectThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: AngularThrottlerOptions<TFn, TSelected>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): AngularThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().throttler,\n ...options,\n } as AngularThrottlerOptions<TFn, TSelected>\n\n const throttler = new Throttler<TFn>(fn, mergedOptions)\n const state = injectStore(throttler.store, selector)\n\n const result = {\n ...throttler,\n state,\n } as AngularThrottler<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 }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4GA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC7B;CAClC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAI,UAAe,IAAI,cAAc;CACvD,MAAM,QAAQ,YAAY,UAAU,OAAO,SAAS;CAEpD,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,WAAU,QAAQ;GAEpB;AAEF,QAAO"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/angular-pacer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.22.0",
|
|
4
4
|
"description": "Utilities for debouncing and throttling functions in Angular.",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -23,14 +23,6 @@
|
|
|
23
23
|
"async",
|
|
24
24
|
"signals"
|
|
25
25
|
],
|
|
26
|
-
"scripts": {
|
|
27
|
-
"clean": "premove ./build ./dist",
|
|
28
|
-
"test:eslint": "eslint ./src",
|
|
29
|
-
"test:lib": "vitest --passWithNoTests",
|
|
30
|
-
"test:lib:dev": "pnpm test:lib --watch",
|
|
31
|
-
"test:types": "tsc",
|
|
32
|
-
"build": "tsdown"
|
|
33
|
-
},
|
|
34
26
|
"type": "module",
|
|
35
27
|
"main": "./dist/index.cjs",
|
|
36
28
|
"module": "./dist/index.js",
|
|
@@ -107,15 +99,23 @@
|
|
|
107
99
|
"src"
|
|
108
100
|
],
|
|
109
101
|
"dependencies": {
|
|
110
|
-
"@tanstack/angular-store": "^0.
|
|
111
|
-
"@tanstack/pacer": "
|
|
112
|
-
"@tanstack/store": "^0.8.0"
|
|
102
|
+
"@tanstack/angular-store": "^0.9.2",
|
|
103
|
+
"@tanstack/pacer": "0.20.0"
|
|
113
104
|
},
|
|
114
105
|
"devDependencies": {
|
|
115
|
-
"@angular/core": "^21.
|
|
116
|
-
"@types/node": "^25.0
|
|
106
|
+
"@angular/core": "^21.2.5",
|
|
107
|
+
"@types/node": "^25.5.0"
|
|
117
108
|
},
|
|
118
109
|
"peerDependencies": {
|
|
119
110
|
"@angular/core": ">=17.0.0"
|
|
111
|
+
},
|
|
112
|
+
"scripts": {
|
|
113
|
+
"clean": "premove ./build ./dist",
|
|
114
|
+
"lint:fix": "eslint ./src --fix",
|
|
115
|
+
"test:eslint": "eslint ./src",
|
|
116
|
+
"test:lib": "vitest --passWithNoTests",
|
|
117
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
118
|
+
"test:types": "tsc",
|
|
119
|
+
"build": "tsdown"
|
|
120
120
|
}
|
|
121
|
-
}
|
|
121
|
+
}
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
+
import { DestroyRef, inject } from '@angular/core'
|
|
1
2
|
import { injectStore } from '@tanstack/angular-store'
|
|
2
3
|
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
3
4
|
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
5
|
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { Store } from '@tanstack/angular-store'
|
|
6
7
|
import type {
|
|
7
8
|
AsyncBatcherOptions,
|
|
8
9
|
AsyncBatcherState,
|
|
9
10
|
} from '@tanstack/pacer/async-batcher'
|
|
10
11
|
|
|
12
|
+
export interface AngularAsyncBatcherOptions<
|
|
13
|
+
TValue,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends AsyncBatcherOptions<TValue> {
|
|
16
|
+
/**
|
|
17
|
+
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
18
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
19
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
20
|
+
*/
|
|
21
|
+
onUnmount?: (batcher: AngularAsyncBatcher<TValue, TSelected>) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
11
24
|
export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
12
25
|
AsyncBatcher<TValue>,
|
|
13
26
|
'store'
|
|
@@ -44,6 +57,20 @@ export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
44
57
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
45
58
|
* full control over when your component tracks state changes.
|
|
46
59
|
*
|
|
60
|
+
* ## Cleanup on Destroy
|
|
61
|
+
*
|
|
62
|
+
* By default, the function cancels any pending batch and aborts in-flight work when the component is destroyed.
|
|
63
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* const batcher = injectAsyncBatcher(fn, {
|
|
67
|
+
* maxSize: 10,
|
|
68
|
+
* onUnmount: (b) => b.flush()
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
73
|
+
*
|
|
47
74
|
* @example
|
|
48
75
|
* ```ts
|
|
49
76
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -68,20 +95,32 @@ export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
|
68
95
|
*/
|
|
69
96
|
export function injectAsyncBatcher<TValue, TSelected = {}>(
|
|
70
97
|
fn: (items: Array<TValue>) => Promise<any>,
|
|
71
|
-
options:
|
|
98
|
+
options: AngularAsyncBatcherOptions<TValue, TSelected> = {},
|
|
72
99
|
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
73
100
|
({}) as TSelected,
|
|
74
101
|
): AngularAsyncBatcher<TValue, TSelected> {
|
|
75
102
|
const mergedOptions = {
|
|
76
103
|
...injectPacerOptions().asyncBatcher,
|
|
77
104
|
...options,
|
|
78
|
-
} as
|
|
105
|
+
} as AngularAsyncBatcherOptions<TValue, TSelected>
|
|
79
106
|
|
|
80
107
|
const batcher = new AsyncBatcher<TValue>(fn, mergedOptions)
|
|
81
108
|
const state = injectStore(batcher.store, selector)
|
|
82
109
|
|
|
83
|
-
|
|
110
|
+
const result = {
|
|
84
111
|
...batcher,
|
|
85
112
|
state,
|
|
86
113
|
} as AngularAsyncBatcher<TValue, TSelected>
|
|
114
|
+
|
|
115
|
+
const destroyRef = inject(DestroyRef, { optional: true })
|
|
116
|
+
destroyRef?.onDestroy(() => {
|
|
117
|
+
if (mergedOptions.onUnmount) {
|
|
118
|
+
mergedOptions.onUnmount(result)
|
|
119
|
+
} else {
|
|
120
|
+
batcher.cancel()
|
|
121
|
+
batcher.abort()
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return result
|
|
87
126
|
}
|
|
@@ -1,14 +1,27 @@
|
|
|
1
|
+
import { DestroyRef, inject } from '@angular/core'
|
|
1
2
|
import { injectStore } from '@tanstack/angular-store'
|
|
2
3
|
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
3
4
|
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
5
|
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { Store } from '@tanstack/angular-store'
|
|
6
7
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
8
|
import type {
|
|
8
9
|
AsyncDebouncerOptions,
|
|
9
10
|
AsyncDebouncerState,
|
|
10
11
|
} from '@tanstack/pacer/async-debouncer'
|
|
11
12
|
|
|
13
|
+
export interface AngularAsyncDebouncerOptions<
|
|
14
|
+
TFn extends AnyAsyncFunction,
|
|
15
|
+
TSelected = {},
|
|
16
|
+
> extends AsyncDebouncerOptions<TFn> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
19
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
21
|
+
*/
|
|
22
|
+
onUnmount?: (debouncer: AngularAsyncDebouncer<TFn, TSelected>) => void
|
|
23
|
+
}
|
|
24
|
+
|
|
12
25
|
export interface AngularAsyncDebouncer<
|
|
13
26
|
TFn extends AnyAsyncFunction,
|
|
14
27
|
TSelected = {},
|
|
@@ -60,6 +73,20 @@ export interface AngularAsyncDebouncer<
|
|
|
60
73
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing' | 'settled')
|
|
61
74
|
* - `successCount`: Number of function executions that have completed successfully
|
|
62
75
|
*
|
|
76
|
+
* ## Cleanup on Destroy
|
|
77
|
+
*
|
|
78
|
+
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
79
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
80
|
+
*
|
|
81
|
+
* ```ts
|
|
82
|
+
* const debouncer = injectAsyncDebouncer(fn, {
|
|
83
|
+
* wait: 500,
|
|
84
|
+
* onUnmount: (d) => d.flush()
|
|
85
|
+
* });
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
89
|
+
*
|
|
63
90
|
* @example
|
|
64
91
|
* ```ts
|
|
65
92
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -94,20 +121,32 @@ export function injectAsyncDebouncer<
|
|
|
94
121
|
TSelected = {},
|
|
95
122
|
>(
|
|
96
123
|
fn: TFn,
|
|
97
|
-
options:
|
|
124
|
+
options: AngularAsyncDebouncerOptions<TFn, TSelected>,
|
|
98
125
|
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
99
126
|
({}) as TSelected,
|
|
100
127
|
): AngularAsyncDebouncer<TFn, TSelected> {
|
|
101
128
|
const mergedOptions = {
|
|
102
129
|
...injectPacerOptions().asyncDebouncer,
|
|
103
130
|
...options,
|
|
104
|
-
} as
|
|
131
|
+
} as AngularAsyncDebouncerOptions<TFn, TSelected>
|
|
105
132
|
|
|
106
133
|
const debouncer = new AsyncDebouncer<TFn>(fn, mergedOptions)
|
|
107
134
|
const state = injectStore(debouncer.store, selector)
|
|
108
135
|
|
|
109
|
-
|
|
136
|
+
const result = {
|
|
110
137
|
...debouncer,
|
|
111
138
|
state,
|
|
112
139
|
} as AngularAsyncDebouncer<TFn, TSelected>
|
|
140
|
+
|
|
141
|
+
const destroyRef = inject(DestroyRef, { optional: true })
|
|
142
|
+
destroyRef?.onDestroy(() => {
|
|
143
|
+
if (mergedOptions.onUnmount) {
|
|
144
|
+
mergedOptions.onUnmount(result)
|
|
145
|
+
} else {
|
|
146
|
+
debouncer.cancel()
|
|
147
|
+
debouncer.abort()
|
|
148
|
+
}
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
return result
|
|
113
152
|
}
|
|
@@ -6,18 +6,10 @@ import type {
|
|
|
6
6
|
AsyncQueuerState,
|
|
7
7
|
} from '@tanstack/pacer/async-queuer'
|
|
8
8
|
|
|
9
|
-
export
|
|
10
|
-
TValue
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Add an item to the queue.
|
|
15
|
-
*/
|
|
16
|
-
readonly addItem: AngularAsyncQueuer<TValue, TSelected>['addItem']
|
|
17
|
-
/**
|
|
18
|
-
* The queuer instance with additional control methods and state signals.
|
|
19
|
-
*/
|
|
20
|
-
readonly queuer: AngularAsyncQueuer<TValue, TSelected>
|
|
9
|
+
export interface AsyncQueuedSignal<TValue, TSelected = {}> {
|
|
10
|
+
(): Array<TValue>
|
|
11
|
+
addItem: AngularAsyncQueuer<TValue, TSelected>['addItem']
|
|
12
|
+
queuer: AngularAsyncQueuer<TValue, TSelected>
|
|
21
13
|
}
|
|
22
14
|
|
|
23
15
|
/**
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
+
import { DestroyRef, inject } from '@angular/core'
|
|
1
2
|
import { injectStore } from '@tanstack/angular-store'
|
|
2
3
|
import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
3
4
|
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
5
|
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { Store } from '@tanstack/angular-store'
|
|
6
7
|
import type {
|
|
7
8
|
AsyncQueuerOptions,
|
|
8
9
|
AsyncQueuerState,
|
|
9
10
|
} from '@tanstack/pacer/async-queuer'
|
|
10
11
|
|
|
12
|
+
export interface AngularAsyncQueuerOptions<
|
|
13
|
+
TValue,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends AsyncQueuerOptions<TValue> {
|
|
16
|
+
/**
|
|
17
|
+
* Optional callback invoked when the component is destroyed. Receives the queuer instance.
|
|
18
|
+
* When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.
|
|
19
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
20
|
+
*/
|
|
21
|
+
onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
11
24
|
export interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
12
25
|
AsyncQueuer<TValue>,
|
|
13
26
|
'store'
|
|
@@ -44,6 +57,20 @@ export interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
|
44
57
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
45
58
|
* full control over when your component tracks state changes.
|
|
46
59
|
*
|
|
60
|
+
* ## Cleanup on Destroy
|
|
61
|
+
*
|
|
62
|
+
* By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
|
|
63
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* const queuer = injectAsyncQueuer(fn, {
|
|
67
|
+
* concurrency: 2,
|
|
68
|
+
* onUnmount: (q) => q.flush()
|
|
69
|
+
* });
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
73
|
+
*
|
|
47
74
|
* @example
|
|
48
75
|
* ```ts
|
|
49
76
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -68,20 +95,32 @@ export interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
|
68
95
|
*/
|
|
69
96
|
export function injectAsyncQueuer<TValue, TSelected = {}>(
|
|
70
97
|
fn: (value: TValue) => Promise<any>,
|
|
71
|
-
options:
|
|
98
|
+
options: AngularAsyncQueuerOptions<TValue, TSelected> = {},
|
|
72
99
|
selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
|
|
73
100
|
({}) as TSelected,
|
|
74
101
|
): AngularAsyncQueuer<TValue, TSelected> {
|
|
75
102
|
const mergedOptions = {
|
|
76
103
|
...injectPacerOptions().asyncQueuer,
|
|
77
104
|
...options,
|
|
78
|
-
} as
|
|
105
|
+
} as AngularAsyncQueuerOptions<TValue, TSelected>
|
|
79
106
|
|
|
80
107
|
const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)
|
|
81
108
|
const state = injectStore(queuer.store, selector)
|
|
82
109
|
|
|
83
|
-
|
|
110
|
+
const result = {
|
|
84
111
|
...queuer,
|
|
85
112
|
state,
|
|
86
113
|
} as AngularAsyncQueuer<TValue, TSelected>
|
|
114
|
+
|
|
115
|
+
const destroyRef = inject(DestroyRef, { optional: true })
|
|
116
|
+
destroyRef?.onDestroy(() => {
|
|
117
|
+
if (mergedOptions.onUnmount) {
|
|
118
|
+
mergedOptions.onUnmount(result)
|
|
119
|
+
} else {
|
|
120
|
+
queuer.stop()
|
|
121
|
+
queuer.abort()
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return result
|
|
87
126
|
}
|
|
@@ -1,14 +1,26 @@
|
|
|
1
|
+
import { DestroyRef, inject } from '@angular/core'
|
|
1
2
|
import { injectStore } from '@tanstack/angular-store'
|
|
2
3
|
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
3
4
|
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
5
|
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { Store } from '@tanstack/angular-store'
|
|
6
7
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
8
|
import type {
|
|
8
9
|
AsyncRateLimiterOptions,
|
|
9
10
|
AsyncRateLimiterState,
|
|
10
11
|
} from '@tanstack/pacer/async-rate-limiter'
|
|
11
12
|
|
|
13
|
+
export interface AngularAsyncRateLimiterOptions<
|
|
14
|
+
TFn extends AnyAsyncFunction,
|
|
15
|
+
TSelected = {},
|
|
16
|
+
> extends AsyncRateLimiterOptions<TFn> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
19
|
+
* When provided, replaces the default cleanup (abort).
|
|
20
|
+
*/
|
|
21
|
+
onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void
|
|
22
|
+
}
|
|
23
|
+
|
|
12
24
|
export interface AngularAsyncRateLimiter<
|
|
13
25
|
TFn extends AnyAsyncFunction,
|
|
14
26
|
TSelected = {},
|
|
@@ -45,6 +57,11 @@ export interface AngularAsyncRateLimiter<
|
|
|
45
57
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
46
58
|
* full control over when your component tracks state changes.
|
|
47
59
|
*
|
|
60
|
+
* ## Cleanup on Destroy
|
|
61
|
+
*
|
|
62
|
+
* By default, the function aborts in-flight work when the component is destroyed.
|
|
63
|
+
* Use the `onUnmount` option to customize this.
|
|
64
|
+
*
|
|
48
65
|
* @example
|
|
49
66
|
* ```ts
|
|
50
67
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -68,20 +85,31 @@ export function injectAsyncRateLimiter<
|
|
|
68
85
|
TSelected = {},
|
|
69
86
|
>(
|
|
70
87
|
fn: TFn,
|
|
71
|
-
options:
|
|
88
|
+
options: AngularAsyncRateLimiterOptions<TFn, TSelected>,
|
|
72
89
|
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
73
90
|
({}) as TSelected,
|
|
74
91
|
): AngularAsyncRateLimiter<TFn, TSelected> {
|
|
75
92
|
const mergedOptions = {
|
|
76
93
|
...injectPacerOptions().asyncRateLimiter,
|
|
77
94
|
...options,
|
|
78
|
-
} as
|
|
95
|
+
} as AngularAsyncRateLimiterOptions<TFn, TSelected>
|
|
79
96
|
|
|
80
97
|
const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)
|
|
81
98
|
const state = injectStore(rateLimiter.store, selector)
|
|
82
99
|
|
|
83
|
-
|
|
100
|
+
const result = {
|
|
84
101
|
...rateLimiter,
|
|
85
102
|
state,
|
|
86
103
|
} as AngularAsyncRateLimiter<TFn, TSelected>
|
|
104
|
+
|
|
105
|
+
const destroyRef = inject(DestroyRef, { optional: true })
|
|
106
|
+
destroyRef?.onDestroy(() => {
|
|
107
|
+
if (mergedOptions.onUnmount) {
|
|
108
|
+
mergedOptions.onUnmount(result)
|
|
109
|
+
} else {
|
|
110
|
+
rateLimiter.abort()
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
return result
|
|
87
115
|
}
|
|
@@ -1,14 +1,27 @@
|
|
|
1
|
+
import { DestroyRef, inject } from '@angular/core'
|
|
1
2
|
import { injectStore } from '@tanstack/angular-store'
|
|
2
3
|
import { AsyncThrottler } from '@tanstack/pacer/async-throttler'
|
|
3
4
|
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
5
|
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { Store } from '@tanstack/angular-store'
|
|
6
7
|
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
8
|
import type {
|
|
8
9
|
AsyncThrottlerOptions,
|
|
9
10
|
AsyncThrottlerState,
|
|
10
11
|
} from '@tanstack/pacer/async-throttler'
|
|
11
12
|
|
|
13
|
+
export interface AngularAsyncThrottlerOptions<
|
|
14
|
+
TFn extends AnyAsyncFunction,
|
|
15
|
+
TSelected = {},
|
|
16
|
+
> extends AsyncThrottlerOptions<TFn> {
|
|
17
|
+
/**
|
|
18
|
+
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
19
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
21
|
+
*/
|
|
22
|
+
onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void
|
|
23
|
+
}
|
|
24
|
+
|
|
12
25
|
export interface AngularAsyncThrottler<
|
|
13
26
|
TFn extends AnyAsyncFunction,
|
|
14
27
|
TSelected = {},
|
|
@@ -47,6 +60,20 @@ export interface AngularAsyncThrottler<
|
|
|
47
60
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
48
61
|
* full control over when your component tracks state changes.
|
|
49
62
|
*
|
|
63
|
+
* ## Cleanup on Destroy
|
|
64
|
+
*
|
|
65
|
+
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
66
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
67
|
+
*
|
|
68
|
+
* ```ts
|
|
69
|
+
* const throttler = injectAsyncThrottler(fn, {
|
|
70
|
+
* wait: 1000,
|
|
71
|
+
* onUnmount: (t) => t.flush()
|
|
72
|
+
* });
|
|
73
|
+
* ```
|
|
74
|
+
*
|
|
75
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
76
|
+
*
|
|
50
77
|
* @example
|
|
51
78
|
* ```ts
|
|
52
79
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -73,20 +100,32 @@ export function injectAsyncThrottler<
|
|
|
73
100
|
TSelected = {},
|
|
74
101
|
>(
|
|
75
102
|
fn: TFn,
|
|
76
|
-
options:
|
|
103
|
+
options: AngularAsyncThrottlerOptions<TFn, TSelected>,
|
|
77
104
|
selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>
|
|
78
105
|
({}) as TSelected,
|
|
79
106
|
): AngularAsyncThrottler<TFn, TSelected> {
|
|
80
107
|
const mergedOptions = {
|
|
81
108
|
...injectPacerOptions().asyncThrottler,
|
|
82
109
|
...options,
|
|
83
|
-
} as
|
|
110
|
+
} as AngularAsyncThrottlerOptions<TFn, TSelected>
|
|
84
111
|
|
|
85
112
|
const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)
|
|
86
113
|
const state = injectStore(throttler.store, selector)
|
|
87
114
|
|
|
88
|
-
|
|
115
|
+
const result = {
|
|
89
116
|
...throttler,
|
|
90
117
|
state,
|
|
91
118
|
} as AngularAsyncThrottler<TFn, TSelected>
|
|
119
|
+
|
|
120
|
+
const destroyRef = inject(DestroyRef, { optional: true })
|
|
121
|
+
destroyRef?.onDestroy(() => {
|
|
122
|
+
if (mergedOptions.onUnmount) {
|
|
123
|
+
mergedOptions.onUnmount(result)
|
|
124
|
+
} else {
|
|
125
|
+
throttler.cancel()
|
|
126
|
+
throttler.abort()
|
|
127
|
+
}
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
return result
|
|
92
131
|
}
|