@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,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 { Queuer } from "@tanstack/pacer/queuer";
|
|
4
5
|
|
|
@@ -43,17 +44,35 @@ import { Queuer } from "@tanstack/pacer/queuer";
|
|
|
43
44
|
* // Access the selected state
|
|
44
45
|
* const { items, isRunning } = queue.state();
|
|
45
46
|
* ```
|
|
47
|
+
*
|
|
48
|
+
* ## Cleanup on Destroy
|
|
49
|
+
*
|
|
50
|
+
* By default, the function stops the queuer when the component is destroyed.
|
|
51
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
52
|
+
*
|
|
53
|
+
* ```ts
|
|
54
|
+
* const queue = injectQueuer(fn, {
|
|
55
|
+
* started: true,
|
|
56
|
+
* onUnmount: (q) => q.flush()
|
|
57
|
+
* });
|
|
58
|
+
* ```
|
|
46
59
|
*/
|
|
47
60
|
function injectQueuer(fn, options = {}, selector = () => ({})) {
|
|
48
|
-
const
|
|
61
|
+
const mergedOptions = {
|
|
49
62
|
...injectPacerOptions().queuer,
|
|
50
63
|
...options
|
|
51
|
-
}
|
|
64
|
+
};
|
|
65
|
+
const queuer = new Queuer(fn, mergedOptions);
|
|
52
66
|
const state = injectStore(queuer.store, selector);
|
|
53
|
-
|
|
67
|
+
const result = {
|
|
54
68
|
...queuer,
|
|
55
69
|
state
|
|
56
70
|
};
|
|
71
|
+
inject(DestroyRef, { optional: true })?.onDestroy(() => {
|
|
72
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
73
|
+
else queuer.stop();
|
|
74
|
+
});
|
|
75
|
+
return result;
|
|
57
76
|
}
|
|
58
77
|
|
|
59
78
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectQueuer.js","names":[],"sources":["../../src/queuer/injectQueuer.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { Queuer } from '@tanstack/pacer/queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface AngularQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages a Queuer instance.\n *\n * This is a lower-level function that provides direct access to the Queuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The Queuer processes items synchronously in order, with optional delays between processing each item.\n * The queuer includes an internal tick mechanism that can be started and stopped, making it useful as a scheduler.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queue = injectQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 }\n * );\n *\n * // Opt-in to track queue contents changes\n * const queue = injectQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 },\n * (state) => ({ items: state.items, size: state.size })\n * );\n *\n * // Add items\n * queue.addItem('task1');\n *\n * // Access the selected state\n * const { items, isRunning } = queue.state();\n * ```\n */\nexport function injectQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n options:
|
|
1
|
+
{"version":3,"file":"injectQueuer.js","names":[],"sources":["../../src/queuer/injectQueuer.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { Queuer } from '@tanstack/pacer/queuer'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'\n\nexport interface AngularQueuerOptions<\n TValue,\n TSelected = {},\n> extends QueuerOptions<TValue> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the queuer instance.\n * When provided, replaces the default cleanup (stop); use it to call flush(), stop(), add logging, etc.\n */\n onUnmount?: (queuer: AngularQueuer<TValue, TSelected>) => void\n}\n\nexport interface AngularQueuer<TValue, TSelected = {}> extends Omit<\n Queuer<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the queuer state changes\n *\n * Use this instead of `queuer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<QueuerState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages a Queuer instance.\n *\n * This is a lower-level function that provides direct access to the Queuer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The Queuer processes items synchronously in order, with optional delays between processing each item.\n * The queuer includes an internal tick mechanism that can be started and stopped, making it useful as a scheduler.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const queue = injectQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 }\n * );\n *\n * // Opt-in to track queue contents changes\n * const queue = injectQueuer(\n * (item) => console.log('Processing:', item),\n * { started: true, wait: 1000 },\n * (state) => ({ items: state.items, size: state.size })\n * );\n *\n * // Add items\n * queue.addItem('task1');\n *\n * // Access the selected state\n * const { items, isRunning } = queue.state();\n * ```\n *\n * ## Cleanup on Destroy\n *\n * By default, the function stops the queuer when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending items instead:\n *\n * ```ts\n * const queue = injectQueuer(fn, {\n * started: true,\n * onUnmount: (q) => q.flush()\n * });\n * ```\n */\nexport function injectQueuer<TValue, TSelected = {}>(\n fn: (item: TValue) => void,\n options: AngularQueuerOptions<TValue, TSelected> = {},\n selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,\n): AngularQueuer<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().queuer,\n ...options,\n } as AngularQueuerOptions<TValue, TSelected>\n\n const queuer = new Queuer<TValue>(fn, mergedOptions)\n const state = injectStore(queuer.store, selector)\n\n const result = {\n ...queuer,\n state,\n } as AngularQueuer<TValue, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n queuer.stop()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyFA,SAAgB,aACd,IACA,UAAmD,EAAE,EACrD,kBAA6D,EAAE,GAC7B;CAClC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,SAAS,IAAI,OAAe,IAAI,cAAc;CACpD,MAAM,QAAQ,YAAY,OAAO,OAAO,SAAS;CAEjD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,CADmB,OAAO,YAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;MAE/B,QAAO,MAAM;GAEf;AAEF,QAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectRateLimitedCallback } from "./injectRateLimitedCallback.cjs";
|
|
2
|
-
import { AngularRateLimiter, injectRateLimiter } from "./injectRateLimiter.cjs";
|
|
2
|
+
import { AngularRateLimiter, AngularRateLimiterOptions, injectRateLimiter } from "./injectRateLimiter.cjs";
|
|
3
3
|
import { RateLimitedSignal, injectRateLimitedSignal } from "./injectRateLimitedSignal.cjs";
|
|
4
4
|
import { injectRateLimitedValue } from "./injectRateLimitedValue.cjs";
|
|
5
5
|
export * from "@tanstack/pacer/rate-limiter";
|
|
6
|
-
export { AngularRateLimiter, RateLimitedSignal, injectRateLimitedCallback, injectRateLimitedSignal, injectRateLimitedValue, injectRateLimiter };
|
|
6
|
+
export { AngularRateLimiter, AngularRateLimiterOptions, RateLimitedSignal, injectRateLimitedCallback, injectRateLimitedSignal, injectRateLimitedValue, injectRateLimiter };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectRateLimitedCallback } from "./injectRateLimitedCallback.js";
|
|
2
|
-
import { AngularRateLimiter, injectRateLimiter } from "./injectRateLimiter.js";
|
|
2
|
+
import { AngularRateLimiter, AngularRateLimiterOptions, injectRateLimiter } from "./injectRateLimiter.js";
|
|
3
3
|
import { RateLimitedSignal, injectRateLimitedSignal } from "./injectRateLimitedSignal.js";
|
|
4
4
|
import { injectRateLimitedValue } from "./injectRateLimitedValue.js";
|
|
5
5
|
export * from "@tanstack/pacer/rate-limiter";
|
|
6
|
-
export { AngularRateLimiter, RateLimitedSignal, injectRateLimitedCallback, injectRateLimitedSignal, injectRateLimitedValue, injectRateLimiter };
|
|
6
|
+
export { AngularRateLimiter, AngularRateLimiterOptions, RateLimitedSignal, injectRateLimitedCallback, injectRateLimitedSignal, injectRateLimitedValue, injectRateLimiter };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectRateLimitedSignal.cjs","names":["injectRateLimiter"],"sources":["../../src/rate-limiter/injectRateLimitedSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectRateLimiter } from './injectRateLimiter'\nimport type { AngularRateLimiter } from './injectRateLimiter'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport
|
|
1
|
+
{"version":3,"file":"injectRateLimitedSignal.cjs","names":["injectRateLimiter"],"sources":["../../src/rate-limiter/injectRateLimitedSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectRateLimiter } from './injectRateLimiter'\nimport type { AngularRateLimiter } from './injectRateLimiter'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface RateLimitedSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a rate-limited state signal, combining Angular's signal with rate limiting functionality.\n * This function provides both the current rate-limited value and methods to update it.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates.\n *\n * The function returns a callable object:\n * - `rateLimited()`: Get the current rate-limited value\n * - `rateLimited.set(...)`: Set or update the rate-limited value (rate-limited via maybeExecute)\n * - `rateLimited.rateLimiter`: The rate limiter instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying rate limiter instance.\n * The `selector` parameter allows you to specify which rate limiter state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimited = injectRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Opt-in to reactive updates when limit state changes\n * const rateLimited = injectRateLimitedSignal(\n * 0,\n * { limit: 5, window: 60000 },\n * (state) => ({ rejectionCount: state.rejectionCount })\n * );\n * ```\n */\nexport function injectRateLimitedSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n selector?: (state: RateLimiterState) => TSelected,\n): RateLimitedSignal<TValue, TSelected> {\n const rateLimitedValue = signal<TValue>(value)\n\n const rateLimiter = injectRateLimiter(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n rateLimitedValue.update(newValue as (prev: TValue) => TValue)\n } else {\n rateLimitedValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n rateLimiter.maybeExecute(newValue)\n }\n\n const rateLimited = Object.assign(() => rateLimitedValue(), {\n set,\n rateLimiter,\n }) as RateLimitedSignal<TValue, TSelected>\n\n return rateLimited\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDA,SAAgB,wBACd,OACA,gBACA,UACsC;CACtC,MAAM,6CAAkC,MAAM;CAE9C,MAAM,cAAcA,6CACjB,aAAkD;AACjD,MAAI,OAAO,aAAa,WACtB,kBAAiB,OAAO,SAAqC;MAE7D,kBAAiB,IAAI,SAAS;IAGlC,gBACA,SACD;CAED,MAAM,OACJ,aACG;AACH,cAAY,aAAa,SAAS;;AAQpC,QALoB,OAAO,aAAa,kBAAkB,EAAE;EAC1D;EACA;EACD,CAAC"}
|
|
@@ -3,16 +3,11 @@ import { RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limit
|
|
|
3
3
|
|
|
4
4
|
//#region src/rate-limiter/injectRateLimitedSignal.d.ts
|
|
5
5
|
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The rate limiter instance with additional control methods and state signals.
|
|
13
|
-
*/
|
|
14
|
-
readonly rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>;
|
|
15
|
-
};
|
|
6
|
+
interface RateLimitedSignal<TValue, TSelected = {}> {
|
|
7
|
+
(): TValue;
|
|
8
|
+
set: Setter<TValue>;
|
|
9
|
+
rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>;
|
|
10
|
+
}
|
|
16
11
|
/**
|
|
17
12
|
* An Angular function that creates a rate-limited state signal, combining Angular's signal with rate limiting functionality.
|
|
18
13
|
* This function provides both the current rate-limited value and methods to update it.
|
|
@@ -3,16 +3,11 @@ import { RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limit
|
|
|
3
3
|
|
|
4
4
|
//#region src/rate-limiter/injectRateLimitedSignal.d.ts
|
|
5
5
|
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The rate limiter instance with additional control methods and state signals.
|
|
13
|
-
*/
|
|
14
|
-
readonly rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>;
|
|
15
|
-
};
|
|
6
|
+
interface RateLimitedSignal<TValue, TSelected = {}> {
|
|
7
|
+
(): TValue;
|
|
8
|
+
set: Setter<TValue>;
|
|
9
|
+
rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>;
|
|
10
|
+
}
|
|
16
11
|
/**
|
|
17
12
|
* An Angular function that creates a rate-limited state signal, combining Angular's signal with rate limiting functionality.
|
|
18
13
|
* This function provides both the current rate-limited value and methods to update it.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectRateLimitedSignal.js","names":[],"sources":["../../src/rate-limiter/injectRateLimitedSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectRateLimiter } from './injectRateLimiter'\nimport type { AngularRateLimiter } from './injectRateLimiter'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport
|
|
1
|
+
{"version":3,"file":"injectRateLimitedSignal.js","names":[],"sources":["../../src/rate-limiter/injectRateLimitedSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectRateLimiter } from './injectRateLimiter'\nimport type { AngularRateLimiter } from './injectRateLimiter'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface RateLimitedSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a rate-limited state signal, combining Angular's signal with rate limiting functionality.\n * This function provides both the current rate-limited value and methods to update it.\n *\n * Rate limiting is a simple \"hard limit\" approach - it allows all updates until the limit is reached, then blocks\n * subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out\n * or intelligently collapse updates.\n *\n * The function returns a callable object:\n * - `rateLimited()`: Get the current rate-limited value\n * - `rateLimited.set(...)`: Set or update the rate-limited value (rate-limited via maybeExecute)\n * - `rateLimited.rateLimiter`: The rate limiter instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying rate limiter instance.\n * The `selector` parameter allows you to specify which rate limiter state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimited = injectRateLimitedSignal(0, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding'\n * });\n *\n * // Opt-in to reactive updates when limit state changes\n * const rateLimited = injectRateLimitedSignal(\n * 0,\n * { limit: 5, window: 60000 },\n * (state) => ({ rejectionCount: state.rejectionCount })\n * );\n * ```\n */\nexport function injectRateLimitedSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: RateLimiterOptions<Setter<TValue>>,\n selector?: (state: RateLimiterState) => TSelected,\n): RateLimitedSignal<TValue, TSelected> {\n const rateLimitedValue = signal<TValue>(value)\n\n const rateLimiter = injectRateLimiter(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n rateLimitedValue.update(newValue as (prev: TValue) => TValue)\n } else {\n rateLimitedValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n rateLimiter.maybeExecute(newValue)\n }\n\n const rateLimited = Object.assign(() => rateLimitedValue(), {\n set,\n rateLimiter,\n }) as RateLimitedSignal<TValue, TSelected>\n\n return rateLimited\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwDA,SAAgB,wBACd,OACA,gBACA,UACsC;CACtC,MAAM,mBAAmB,OAAe,MAAM;CAE9C,MAAM,cAAc,mBACjB,aAAkD;AACjD,MAAI,OAAO,aAAa,WACtB,kBAAiB,OAAO,SAAqC;MAE7D,kBAAiB,IAAI,SAAS;IAGlC,gBACA,SACD;CAED,MAAM,OACJ,aACG;AACH,cAAY,aAAa,SAAS;;AAQpC,QALoB,OAAO,aAAa,kBAAkB,EAAE;EAC1D;EACA;EACD,CAAC"}
|
|
@@ -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_rate_limiter = require("@tanstack/pacer/rate-limiter");
|
|
4
5
|
|
|
@@ -39,6 +40,10 @@ let _tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
|
|
|
39
40
|
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
40
41
|
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
41
42
|
*
|
|
43
|
+
* ## Cleanup on Destroy
|
|
44
|
+
*
|
|
45
|
+
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
46
|
+
*
|
|
42
47
|
* @example
|
|
43
48
|
* ```ts
|
|
44
49
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -74,15 +79,20 @@ let _tanstack_pacer_rate_limiter = require("@tanstack/pacer/rate-limiter");
|
|
|
74
79
|
* ```
|
|
75
80
|
*/
|
|
76
81
|
function injectRateLimiter(fn, options, selector = () => ({})) {
|
|
77
|
-
const
|
|
82
|
+
const mergedOptions = {
|
|
78
83
|
...require_pacer_context.injectPacerOptions().rateLimiter,
|
|
79
84
|
...options
|
|
80
|
-
}
|
|
85
|
+
};
|
|
86
|
+
const rateLimiter = new _tanstack_pacer_rate_limiter.RateLimiter(fn, mergedOptions);
|
|
81
87
|
const state = (0, _tanstack_angular_store.injectStore)(rateLimiter.store, selector);
|
|
82
|
-
|
|
88
|
+
const result = {
|
|
83
89
|
...rateLimiter,
|
|
84
90
|
state
|
|
85
91
|
};
|
|
92
|
+
(0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
|
|
93
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
94
|
+
});
|
|
95
|
+
return result;
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectRateLimiter.cjs","names":["RateLimiter","
|
|
1
|
+
{"version":3,"file":"injectRateLimiter.cjs","names":["injectPacerOptions","RateLimiter","DestroyRef"],"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,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,cAAc,IAAIC,yCAAiB,IAAI,cAAc;CAC3D,MAAM,iDAAoB,YAAY,OAAO,SAAS;CAEtD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,2BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;GAEjC;AAEF,QAAO"}
|
|
@@ -1,9 +1,15 @@
|
|
|
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 { RateLimiter, RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limiter";
|
|
5
5
|
|
|
6
6
|
//#region src/rate-limiter/injectRateLimiter.d.ts
|
|
7
|
+
interface AngularRateLimiterOptions<TFn extends AnyFunction, TSelected = {}> extends RateLimiterOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
10
|
+
*/
|
|
11
|
+
onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void;
|
|
12
|
+
}
|
|
7
13
|
interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
8
14
|
/**
|
|
9
15
|
* Reactive state signal that will be updated when the rate limiter state changes
|
|
@@ -53,6 +59,10 @@ interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Om
|
|
|
53
59
|
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
54
60
|
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
55
61
|
*
|
|
62
|
+
* ## Cleanup on Destroy
|
|
63
|
+
*
|
|
64
|
+
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
65
|
+
*
|
|
56
66
|
* @example
|
|
57
67
|
* ```ts
|
|
58
68
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -87,7 +97,7 @@ interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Om
|
|
|
87
97
|
* const { executionCount, rejectionCount } = rateLimiter.state();
|
|
88
98
|
* ```
|
|
89
99
|
*/
|
|
90
|
-
declare function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
100
|
+
declare function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularRateLimiterOptions<TFn, TSelected>, selector?: (state: RateLimiterState) => TSelected): AngularRateLimiter<TFn, TSelected>;
|
|
91
101
|
//#endregion
|
|
92
|
-
export { AngularRateLimiter, injectRateLimiter };
|
|
102
|
+
export { AngularRateLimiter, AngularRateLimiterOptions, injectRateLimiter };
|
|
93
103
|
//# sourceMappingURL=injectRateLimiter.d.cts.map
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
2
3
|
import { RateLimiter, RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limiter";
|
|
3
4
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Store } from "@tanstack/store";
|
|
5
5
|
|
|
6
6
|
//#region src/rate-limiter/injectRateLimiter.d.ts
|
|
7
|
+
interface AngularRateLimiterOptions<TFn extends AnyFunction, TSelected = {}> extends RateLimiterOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
10
|
+
*/
|
|
11
|
+
onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void;
|
|
12
|
+
}
|
|
7
13
|
interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
8
14
|
/**
|
|
9
15
|
* Reactive state signal that will be updated when the rate limiter state changes
|
|
@@ -53,6 +59,10 @@ interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Om
|
|
|
53
59
|
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
54
60
|
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
55
61
|
*
|
|
62
|
+
* ## Cleanup on Destroy
|
|
63
|
+
*
|
|
64
|
+
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
65
|
+
*
|
|
56
66
|
* @example
|
|
57
67
|
* ```ts
|
|
58
68
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -87,7 +97,7 @@ interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Om
|
|
|
87
97
|
* const { executionCount, rejectionCount } = rateLimiter.state();
|
|
88
98
|
* ```
|
|
89
99
|
*/
|
|
90
|
-
declare function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
100
|
+
declare function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularRateLimiterOptions<TFn, TSelected>, selector?: (state: RateLimiterState) => TSelected): AngularRateLimiter<TFn, TSelected>;
|
|
91
101
|
//#endregion
|
|
92
|
-
export { AngularRateLimiter, injectRateLimiter };
|
|
102
|
+
export { AngularRateLimiter, AngularRateLimiterOptions, injectRateLimiter };
|
|
93
103
|
//# sourceMappingURL=injectRateLimiter.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 { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
4
5
|
|
|
@@ -39,6 +40,10 @@ import { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
|
39
40
|
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
40
41
|
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
41
42
|
*
|
|
43
|
+
* ## Cleanup on Destroy
|
|
44
|
+
*
|
|
45
|
+
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
46
|
+
*
|
|
42
47
|
* @example
|
|
43
48
|
* ```ts
|
|
44
49
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -74,15 +79,20 @@ import { RateLimiter } from "@tanstack/pacer/rate-limiter";
|
|
|
74
79
|
* ```
|
|
75
80
|
*/
|
|
76
81
|
function injectRateLimiter(fn, options, selector = () => ({})) {
|
|
77
|
-
const
|
|
82
|
+
const mergedOptions = {
|
|
78
83
|
...injectPacerOptions().rateLimiter,
|
|
79
84
|
...options
|
|
80
|
-
}
|
|
85
|
+
};
|
|
86
|
+
const rateLimiter = new RateLimiter(fn, mergedOptions);
|
|
81
87
|
const state = injectStore(rateLimiter.store, selector);
|
|
82
|
-
|
|
88
|
+
const result = {
|
|
83
89
|
...rateLimiter,
|
|
84
90
|
state
|
|
85
91
|
};
|
|
92
|
+
inject(DestroyRef, { optional: true })?.onDestroy(() => {
|
|
93
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
94
|
+
});
|
|
95
|
+
return result;
|
|
86
96
|
}
|
|
87
97
|
|
|
88
98
|
//#endregion
|
|
@@ -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 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledSignal.cjs","names":["injectThrottler"],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport
|
|
1
|
+
{"version":3,"file":"injectThrottledSignal.cjs","names":["injectThrottler"],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface ThrottledSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n throttler: AngularThrottler<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.\n * This function provides both the current throttled value and methods to update it.\n *\n * The state value is updated at most once within the specified wait time.\n * This is useful for handling frequent state updates that should be rate-limited, like scroll positions\n * or mouse movements.\n *\n * The function returns a callable object:\n * - `throttled()`: Get the current throttled value\n * - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)\n * - `throttled.throttler`: The throttler instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions 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 throttler 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 * const throttledScrollY = injectThrottledSignal(0, { wait: 100 })\n *\n * // Get value\n * console.log(throttledScrollY())\n *\n * // Set/update value (throttled)\n * throttledScrollY.set(window.scrollY)\n *\n * // Access throttler\n * console.log(throttledScrollY.throttler.state().isPending)\n * ```\n */\nexport function injectThrottledSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const throttledValue = signal<TValue>(value)\n\n const throttler = injectThrottler(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n throttledValue.update(newValue as (prev: TValue) => TValue)\n } else {\n throttledValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n throttler.maybeExecute(newValue)\n }\n\n const throttled = Object.assign(() => throttledValue(), {\n set,\n throttler,\n }) as ThrottledSignal<TValue, TSelected>\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,sBACd,OACA,gBACA,UACoC;CACpC,MAAM,2CAAgC,MAAM;CAE5C,MAAM,YAAYA,yCACf,aAAkD;AACjD,MAAI,OAAO,aAAa,WACtB,gBAAe,OAAO,SAAqC;MAE3D,gBAAe,IAAI,SAAS;IAGhC,gBACA,SACD;CAED,MAAM,OACJ,aACG;AACH,YAAU,aAAa,SAAS;;AAQlC,QALkB,OAAO,aAAa,gBAAgB,EAAE;EACtD;EACA;EACD,CAAC"}
|
|
@@ -3,16 +3,11 @@ import { ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
|
3
3
|
|
|
4
4
|
//#region src/throttler/injectThrottledSignal.d.ts
|
|
5
5
|
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The throttler instance with additional control methods and state signals.
|
|
13
|
-
*/
|
|
14
|
-
readonly throttler: AngularThrottler<Setter<TValue>, TSelected>;
|
|
15
|
-
};
|
|
6
|
+
interface ThrottledSignal<TValue, TSelected = {}> {
|
|
7
|
+
(): TValue;
|
|
8
|
+
set: Setter<TValue>;
|
|
9
|
+
throttler: AngularThrottler<Setter<TValue>, TSelected>;
|
|
10
|
+
}
|
|
16
11
|
/**
|
|
17
12
|
* An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.
|
|
18
13
|
* This function provides both the current throttled value and methods to update it.
|
|
@@ -3,16 +3,11 @@ import { ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
|
3
3
|
|
|
4
4
|
//#region src/throttler/injectThrottledSignal.d.ts
|
|
5
5
|
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The throttler instance with additional control methods and state signals.
|
|
13
|
-
*/
|
|
14
|
-
readonly throttler: AngularThrottler<Setter<TValue>, TSelected>;
|
|
15
|
-
};
|
|
6
|
+
interface ThrottledSignal<TValue, TSelected = {}> {
|
|
7
|
+
(): TValue;
|
|
8
|
+
set: Setter<TValue>;
|
|
9
|
+
throttler: AngularThrottler<Setter<TValue>, TSelected>;
|
|
10
|
+
}
|
|
16
11
|
/**
|
|
17
12
|
* An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.
|
|
18
13
|
* This function provides both the current throttled value and methods to update it.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledSignal.js","names":[],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport
|
|
1
|
+
{"version":3,"file":"injectThrottledSignal.js","names":[],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface ThrottledSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n throttler: AngularThrottler<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.\n * This function provides both the current throttled value and methods to update it.\n *\n * The state value is updated at most once within the specified wait time.\n * This is useful for handling frequent state updates that should be rate-limited, like scroll positions\n * or mouse movements.\n *\n * The function returns a callable object:\n * - `throttled()`: Get the current throttled value\n * - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)\n * - `throttled.throttler`: The throttler instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions 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 throttler 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 * const throttledScrollY = injectThrottledSignal(0, { wait: 100 })\n *\n * // Get value\n * console.log(throttledScrollY())\n *\n * // Set/update value (throttled)\n * throttledScrollY.set(window.scrollY)\n *\n * // Access throttler\n * console.log(throttledScrollY.throttler.state().isPending)\n * ```\n */\nexport function injectThrottledSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const throttledValue = signal<TValue>(value)\n\n const throttler = injectThrottler(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n throttledValue.update(newValue as (prev: TValue) => TValue)\n } else {\n throttledValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n throttler.maybeExecute(newValue)\n }\n\n const throttled = Object.assign(() => throttledValue(), {\n set,\n throttler,\n }) as ThrottledSignal<TValue, TSelected>\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,sBACd,OACA,gBACA,UACoC;CACpC,MAAM,iBAAiB,OAAe,MAAM;CAE5C,MAAM,YAAY,iBACf,aAAkD;AACjD,MAAI,OAAO,aAAa,WACtB,gBAAe,OAAO,SAAqC;MAE3D,gBAAe,IAAI,SAAS;IAGhC,gBACA,SACD;CAED,MAAM,OACJ,aACG;AACH,YAAU,aAAa,SAAS;;AAQlC,QALkB,OAAO,aAAa,gBAAgB,EAAE;EACtD;EACA;EACD,CAAC"}
|
|
@@ -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"}
|