@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.
- package/LICENSE +21 -0
- 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/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/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/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/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/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/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/injectDebouncer.ts +39 -4
- package/src/queuer/injectQueuer.ts +39 -4
- package/src/rate-limiter/injectRateLimiter.ts +28 -4
- package/src/throttler/injectThrottler.ts +39 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectRateLimiter.js","names":[],"sources":["../../src/rate-limiter/injectRateLimiter.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { RateLimiter } from '@tanstack/pacer/rate-limiter'\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 RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface AngularRateLimiter<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the 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<RateLimiterState>>\n}\n\n/**\n * An Angular function that creates and manages a RateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the RateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\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 * - `executionCount`: Number of function executions that have been completed\n * - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations\n * - `rejectionCount`: Number of function executions that have been rejected due to rate limiting\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * });\n *\n * // Opt-in to track execution count changes\n * const rateLimiter = injectRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * const remaining = rateLimiter.getRemainingInWindow();\n * if (remaining > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options:
|
|
1
|
+
{"version":3,"file":"injectRateLimiter.js","names":[],"sources":["../../src/rate-limiter/injectRateLimiter.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { RateLimiter } from '@tanstack/pacer/rate-limiter'\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 RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface AngularRateLimiterOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends RateLimiterOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.\n */\n onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void\n}\n\nexport interface AngularRateLimiter<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the 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<RateLimiterState>>\n}\n\n/**\n * An Angular function that creates and manages a RateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the RateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\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 * - `executionCount`: Number of function executions that have been completed\n * - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations\n * - `rejectionCount`: Number of function executions that have been rejected due to rate limiting\n *\n * ## Cleanup on Destroy\n *\n * Use the `onUnmount` option to run a callback when the component is destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * });\n *\n * // Opt-in to track execution count changes\n * const rateLimiter = injectRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * const remaining = rateLimiter.getRemainingInWindow();\n * if (remaining > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: AngularRateLimiterOptions<TFn, TSelected>,\n selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,\n): AngularRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().rateLimiter,\n ...options,\n } as AngularRateLimiterOptions<TFn, TSelected>\n\n const rateLimiter = new RateLimiter<TFn>(fn, mergedOptions)\n const state = injectStore(rateLimiter.store, selector)\n\n const result = {\n ...rateLimiter,\n state,\n } as AngularRateLimiter<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiHA,SAAgB,kBACd,IACA,SACA,kBAA0D,EAAE,GACxB;CACpC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,cAAc,IAAI,YAAiB,IAAI,cAAc;CAC3D,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;GAEjC;AAEF,QAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectThrottledCallback } from "./injectThrottledCallback.cjs";
|
|
2
|
-
import { AngularThrottler, injectThrottler } from "./injectThrottler.cjs";
|
|
2
|
+
import { AngularThrottler, AngularThrottlerOptions, injectThrottler } from "./injectThrottler.cjs";
|
|
3
3
|
import { ThrottledSignal, injectThrottledSignal } from "./injectThrottledSignal.cjs";
|
|
4
4
|
import { injectThrottledValue } from "./injectThrottledValue.cjs";
|
|
5
5
|
export * from "@tanstack/pacer/throttler";
|
|
6
|
-
export { AngularThrottler, ThrottledSignal, injectThrottledCallback, injectThrottledSignal, injectThrottledValue, injectThrottler };
|
|
6
|
+
export { AngularThrottler, AngularThrottlerOptions, ThrottledSignal, injectThrottledCallback, injectThrottledSignal, injectThrottledValue, injectThrottler };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectThrottledCallback } from "./injectThrottledCallback.js";
|
|
2
|
-
import { AngularThrottler, injectThrottler } from "./injectThrottler.js";
|
|
2
|
+
import { AngularThrottler, AngularThrottlerOptions, injectThrottler } from "./injectThrottler.js";
|
|
3
3
|
import { ThrottledSignal, injectThrottledSignal } from "./injectThrottledSignal.js";
|
|
4
4
|
import { injectThrottledValue } from "./injectThrottledValue.js";
|
|
5
5
|
export * from "@tanstack/pacer/throttler";
|
|
6
|
-
export { AngularThrottler, ThrottledSignal, injectThrottledCallback, injectThrottledSignal, injectThrottledValue, injectThrottler };
|
|
6
|
+
export { AngularThrottler, AngularThrottlerOptions, ThrottledSignal, injectThrottledCallback, injectThrottledSignal, injectThrottledValue, injectThrottler };
|
|
@@ -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_throttler = require("@tanstack/pacer/throttler");
|
|
4
5
|
|
|
@@ -35,6 +36,18 @@ let _tanstack_pacer_throttler = require("@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 @@ let _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
|
60
73
|
* ```
|
|
61
74
|
*/
|
|
62
75
|
function injectThrottler(fn, options, selector = () => ({})) {
|
|
63
|
-
const
|
|
76
|
+
const mergedOptions = {
|
|
64
77
|
...require_pacer_context.injectPacerOptions().throttler,
|
|
65
78
|
...options
|
|
66
|
-
}
|
|
79
|
+
};
|
|
80
|
+
const throttler = new _tanstack_pacer_throttler.Throttler(fn, mergedOptions);
|
|
67
81
|
const state = (0, _tanstack_angular_store.injectStore)(throttler.store, selector);
|
|
68
|
-
|
|
82
|
+
const result = {
|
|
69
83
|
...throttler,
|
|
70
84
|
state
|
|
71
85
|
};
|
|
86
|
+
(0, _angular_core.inject)(_angular_core.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.cjs","names":["Throttler","
|
|
1
|
+
{"version":3,"file":"injectThrottler.cjs","names":["injectPacerOptions","Throttler","DestroyRef"],"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,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAIC,oCAAe,IAAI,cAAc;CACvD,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;MAE/B,WAAU,QAAQ;GAEpB;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 { 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.21.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.8.
|
|
111
|
-
"@tanstack/pacer": "
|
|
112
|
-
"@tanstack/store": "^0.8.0"
|
|
102
|
+
"@tanstack/angular-store": "^0.8.1",
|
|
103
|
+
"@tanstack/pacer": "0.19.0"
|
|
113
104
|
},
|
|
114
105
|
"devDependencies": {
|
|
115
|
-
"@angular/core": "^21.
|
|
116
|
-
"@types/node": "^25.
|
|
106
|
+
"@angular/core": "^21.1.4",
|
|
107
|
+
"@types/node": "^25.2.3"
|
|
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
|
}
|
|
@@ -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
|
}
|