@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,9 +1,17 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
-
import { Store } from "@tanstack/store";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
3
3
|
import { AnyAsyncFunction } from "@tanstack/pacer/types";
|
|
4
4
|
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState } from "@tanstack/pacer/async-throttler";
|
|
5
5
|
|
|
6
6
|
//#region src/async-throttler/injectAsyncThrottler.d.ts
|
|
7
|
+
interface AngularAsyncThrottlerOptions<TFn extends AnyAsyncFunction, TSelected = {}> extends AsyncThrottlerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
12
|
+
*/
|
|
13
|
+
onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void;
|
|
14
|
+
}
|
|
7
15
|
interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
8
16
|
/**
|
|
9
17
|
* Reactive state signal that will be updated when the async throttler state changes
|
|
@@ -38,6 +46,20 @@ interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
38
46
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
39
47
|
* full control over when your component tracks state changes.
|
|
40
48
|
*
|
|
49
|
+
* ## Cleanup on Destroy
|
|
50
|
+
*
|
|
51
|
+
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
52
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const throttler = injectAsyncThrottler(fn, {
|
|
56
|
+
* wait: 1000,
|
|
57
|
+
* onUnmount: (t) => t.flush()
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
62
|
+
*
|
|
41
63
|
* @example
|
|
42
64
|
* ```ts
|
|
43
65
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -59,7 +81,7 @@ interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
59
81
|
* };
|
|
60
82
|
* ```
|
|
61
83
|
*/
|
|
62
|
-
declare function injectAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options:
|
|
84
|
+
declare function injectAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AngularAsyncThrottlerOptions<TFn, TSelected>, selector?: (state: AsyncThrottlerState<TFn>) => TSelected): AngularAsyncThrottler<TFn, TSelected>;
|
|
63
85
|
//#endregion
|
|
64
|
-
export { AngularAsyncThrottler, injectAsyncThrottler };
|
|
86
|
+
export { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottler };
|
|
65
87
|
//# sourceMappingURL=injectAsyncThrottler.d.cts.map
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
2
3
|
import { AsyncThrottler, AsyncThrottlerOptions, AsyncThrottlerState } from "@tanstack/pacer/async-throttler";
|
|
3
4
|
import { AnyAsyncFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Store } from "@tanstack/store";
|
|
5
5
|
|
|
6
6
|
//#region src/async-throttler/injectAsyncThrottler.d.ts
|
|
7
|
+
interface AngularAsyncThrottlerOptions<TFn extends AnyAsyncFunction, TSelected = {}> extends AsyncThrottlerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
12
|
+
*/
|
|
13
|
+
onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void;
|
|
14
|
+
}
|
|
7
15
|
interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
8
16
|
/**
|
|
9
17
|
* Reactive state signal that will be updated when the async throttler state changes
|
|
@@ -38,6 +46,20 @@ interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
38
46
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
39
47
|
* full control over when your component tracks state changes.
|
|
40
48
|
*
|
|
49
|
+
* ## Cleanup on Destroy
|
|
50
|
+
*
|
|
51
|
+
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
52
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* const throttler = injectAsyncThrottler(fn, {
|
|
56
|
+
* wait: 1000,
|
|
57
|
+
* onUnmount: (t) => t.flush()
|
|
58
|
+
* });
|
|
59
|
+
* ```
|
|
60
|
+
*
|
|
61
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
62
|
+
*
|
|
41
63
|
* @example
|
|
42
64
|
* ```ts
|
|
43
65
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -59,7 +81,7 @@ interface AngularAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}> ex
|
|
|
59
81
|
* };
|
|
60
82
|
* ```
|
|
61
83
|
*/
|
|
62
|
-
declare function injectAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options:
|
|
84
|
+
declare function injectAsyncThrottler<TFn extends AnyAsyncFunction, TSelected = {}>(fn: TFn, options: AngularAsyncThrottlerOptions<TFn, TSelected>, selector?: (state: AsyncThrottlerState<TFn>) => TSelected): AngularAsyncThrottler<TFn, TSelected>;
|
|
63
85
|
//#endregion
|
|
64
|
-
export { AngularAsyncThrottler, injectAsyncThrottler };
|
|
86
|
+
export { AngularAsyncThrottler, AngularAsyncThrottlerOptions, injectAsyncThrottler };
|
|
65
87
|
//# sourceMappingURL=injectAsyncThrottler.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 { AsyncThrottler } from "@tanstack/pacer/async-throttler";
|
|
4
5
|
|
|
@@ -24,6 +25,20 @@ import { AsyncThrottler } from "@tanstack/pacer/async-throttler";
|
|
|
24
25
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
25
26
|
* full control over when your component tracks state changes.
|
|
26
27
|
*
|
|
28
|
+
* ## Cleanup on Destroy
|
|
29
|
+
*
|
|
30
|
+
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
31
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const throttler = injectAsyncThrottler(fn, {
|
|
35
|
+
* wait: 1000,
|
|
36
|
+
* onUnmount: (t) => t.flush()
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
41
|
+
*
|
|
27
42
|
* @example
|
|
28
43
|
* ```ts
|
|
29
44
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -46,15 +61,24 @@ import { AsyncThrottler } from "@tanstack/pacer/async-throttler";
|
|
|
46
61
|
* ```
|
|
47
62
|
*/
|
|
48
63
|
function injectAsyncThrottler(fn, options, selector = () => ({})) {
|
|
49
|
-
const
|
|
64
|
+
const mergedOptions = {
|
|
50
65
|
...injectPacerOptions().asyncThrottler,
|
|
51
66
|
...options
|
|
52
|
-
}
|
|
67
|
+
};
|
|
68
|
+
const throttler = new AsyncThrottler(fn, mergedOptions);
|
|
53
69
|
const state = injectStore(throttler.store, selector);
|
|
54
|
-
|
|
70
|
+
const result = {
|
|
55
71
|
...throttler,
|
|
56
72
|
state
|
|
57
73
|
};
|
|
74
|
+
inject(DestroyRef, { optional: true })?.onDestroy(() => {
|
|
75
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
76
|
+
else {
|
|
77
|
+
throttler.cancel();
|
|
78
|
+
throttler.abort();
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
return result;
|
|
58
82
|
}
|
|
59
83
|
|
|
60
84
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectAsyncThrottler.js","names":[],"sources":["../../src/async-throttler/injectAsyncThrottler.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface AngularAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncThrottler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncThrottler instance.\n *\n * This is a lower-level function that provides direct access to the AsyncThrottler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async throttling functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * The throttler will execute the function at most once within the specified wait time.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectAsyncThrottler(\n * async (data: Data) => {\n * const response = await fetch('/api/update', {\n * method: 'POST',\n * body: JSON.stringify(data)\n * });\n * return response.json();\n * },\n * { wait: 1000 }\n * );\n *\n * // In an event handler\n * const handleUpdate = async (data: Data) => {\n * const result = await throttler.maybeExecute(data);\n * console.log('Update result:', result);\n * };\n * ```\n */\nexport function injectAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options:
|
|
1
|
+
{"version":3,"file":"injectAsyncThrottler.js","names":[],"sources":["../../src/async-throttler/injectAsyncThrottler.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { AsyncThrottler } from '@tanstack/pacer/async-throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyAsyncFunction } from '@tanstack/pacer/types'\nimport type {\n AsyncThrottlerOptions,\n AsyncThrottlerState,\n} from '@tanstack/pacer/async-throttler'\n\nexport interface AngularAsyncThrottlerOptions<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends AsyncThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n */\n onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void\n}\n\nexport interface AngularAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n> extends Omit<AsyncThrottler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the async throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages an AsyncThrottler instance.\n *\n * This is a lower-level function that provides direct access to the AsyncThrottler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides async throttling functionality with promise support, error handling,\n * retry capabilities, and abort support.\n *\n * The throttler will execute the function at most once within the specified wait time.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```ts\n * const throttler = injectAsyncThrottler(fn, {\n * wait: 1000,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * When using onUnmount with flush, guard your callbacks since the component may already be destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectAsyncThrottler(\n * async (data: Data) => {\n * const response = await fetch('/api/update', {\n * method: 'POST',\n * body: JSON.stringify(data)\n * });\n * return response.json();\n * },\n * { wait: 1000 }\n * );\n *\n * // In an event handler\n * const handleUpdate = async (data: Data) => {\n * const result = await throttler.maybeExecute(data);\n * console.log('Update result:', result);\n * };\n * ```\n */\nexport function injectAsyncThrottler<\n TFn extends AnyAsyncFunction,\n TSelected = {},\n>(\n fn: TFn,\n options: AngularAsyncThrottlerOptions<TFn, TSelected>,\n selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>\n ({}) as TSelected,\n): AngularAsyncThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().asyncThrottler,\n ...options,\n } as AngularAsyncThrottlerOptions<TFn, TSelected>\n\n const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)\n const state = injectStore(throttler.store, selector)\n\n const result = {\n ...throttler,\n state,\n } as AngularAsyncThrottler<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n throttler.cancel()\n throttler.abort()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiGA,SAAgB,qBAId,IACA,SACA,kBACG,EAAE,GACkC;CACvC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAI,eAAoB,IAAI,cAAc;CAC5D,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;OAC1B;AACL,aAAU,QAAQ;AAClB,aAAU,OAAO;;GAEnB;AAEF,QAAO"}
|
package/dist/batcher/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { injectBatchedCallback } from "./injectBatchedCallback.cjs";
|
|
2
|
-
import { AngularBatcher, injectBatcher } from "./injectBatcher.cjs";
|
|
2
|
+
import { AngularBatcher, AngularBatcherOptions, injectBatcher } from "./injectBatcher.cjs";
|
|
3
3
|
export * from "@tanstack/pacer/batcher";
|
|
4
|
-
export { AngularBatcher, injectBatchedCallback, injectBatcher };
|
|
4
|
+
export { AngularBatcher, AngularBatcherOptions, injectBatchedCallback, injectBatcher };
|
package/dist/batcher/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { injectBatchedCallback } from "./injectBatchedCallback.js";
|
|
2
|
-
import { AngularBatcher, injectBatcher } from "./injectBatcher.js";
|
|
2
|
+
import { AngularBatcher, AngularBatcherOptions, injectBatcher } from "./injectBatcher.js";
|
|
3
3
|
export * from "@tanstack/pacer/batcher";
|
|
4
|
-
export { AngularBatcher, injectBatchedCallback, injectBatcher };
|
|
4
|
+
export { AngularBatcher, AngularBatcherOptions, injectBatchedCallback, injectBatcher };
|
|
@@ -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_batcher = require("@tanstack/pacer/batcher");
|
|
4
5
|
|
|
@@ -24,6 +25,18 @@ let _tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
|
24
25
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
25
26
|
* full control over when your component tracks state changes.
|
|
26
27
|
*
|
|
28
|
+
* ## Cleanup on Destroy
|
|
29
|
+
*
|
|
30
|
+
* By default, the function cancels any pending batch when the component is destroyed.
|
|
31
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const batcher = injectBatcher(fn, {
|
|
35
|
+
* maxSize: 5,
|
|
36
|
+
* onUnmount: (b) => b.flush()
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
27
40
|
* @example
|
|
28
41
|
* ```ts
|
|
29
42
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -40,15 +53,21 @@ let _tanstack_pacer_batcher = require("@tanstack/pacer/batcher");
|
|
|
40
53
|
* ```
|
|
41
54
|
*/
|
|
42
55
|
function injectBatcher(fn, options = {}, selector = () => ({})) {
|
|
43
|
-
const
|
|
56
|
+
const mergedOptions = {
|
|
44
57
|
...require_pacer_context.injectPacerOptions().batcher,
|
|
45
58
|
...options
|
|
46
|
-
}
|
|
59
|
+
};
|
|
60
|
+
const batcher = new _tanstack_pacer_batcher.Batcher(fn, mergedOptions);
|
|
47
61
|
const state = (0, _tanstack_angular_store.injectStore)(batcher.store, selector);
|
|
48
|
-
|
|
62
|
+
const result = {
|
|
49
63
|
...batcher,
|
|
50
64
|
state
|
|
51
65
|
};
|
|
66
|
+
(0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
|
|
67
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
68
|
+
else batcher.cancel();
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
52
71
|
}
|
|
53
72
|
|
|
54
73
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectBatcher.cjs","names":["Batcher","
|
|
1
|
+
{"version":3,"file":"injectBatcher.cjs","names":["injectPacerOptions","Batcher","DestroyRef"],"sources":["../../src/batcher/injectBatcher.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { Batcher } from '@tanstack/pacer/batcher'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface AngularBatcherOptions<\n TValue,\n TSelected = {},\n> extends BatcherOptions<TValue> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the batcher instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.\n */\n onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void\n}\n\nexport interface AngularBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages a Batcher instance.\n *\n * This is a lower-level function that provides direct access to the Batcher's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The Batcher collects items and processes them in batches based on configurable conditions:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending batch 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 batcher = injectBatcher(fn, {\n * maxSize: 5,\n * onUnmount: (b) => b.flush()\n * });\n * ```\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const batcher = injectBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 }\n * );\n *\n * // Add items\n * batcher.addItem('task1');\n *\n * // Access the selected state\n * const { items, isPending } = batcher.state();\n * ```\n */\nexport function injectBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options: AngularBatcherOptions<TValue, TSelected> = {},\n selector: (state: BatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularBatcher<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().batcher,\n ...options,\n } as AngularBatcherOptions<TValue, TSelected>\n\n const batcher = new Batcher<TValue>(fn, mergedOptions)\n const state = injectStore(batcher.store, selector)\n\n const result = {\n ...batcher,\n state,\n } as AngularBatcher<TValue, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n batcher.cancel()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFA,SAAgB,cACd,IACA,UAAoD,EAAE,EACtD,kBACG,EAAE,GAC8B;CACnC,MAAM,gBAAgB;EACpB,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,UAAU,IAAIC,gCAAgB,IAAI,cAAc;CACtD,MAAM,iDAAoB,QAAQ,OAAO,SAAS;CAElD,MAAM,SAAS;EACb,GAAG;EACH;EACD;AAGD,2BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAAC,EAC7C,gBAAgB;AAC1B,MAAI,cAAc,UAChB,eAAc,UAAU,OAAO;MAE/B,SAAQ,QAAQ;GAElB;AAEF,QAAO"}
|
|
@@ -1,8 +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 { Batcher, BatcherOptions, BatcherState } from "@tanstack/pacer/batcher";
|
|
4
4
|
|
|
5
5
|
//#region src/batcher/injectBatcher.d.ts
|
|
6
|
+
interface AngularBatcherOptions<TValue, TSelected = {}> extends BatcherOptions<TValue> {
|
|
7
|
+
/**
|
|
8
|
+
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
9
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
10
|
+
*/
|
|
11
|
+
onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void;
|
|
12
|
+
}
|
|
6
13
|
interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'store'> {
|
|
7
14
|
/**
|
|
8
15
|
* Reactive state signal that will be updated when the batcher state changes
|
|
@@ -37,6 +44,18 @@ interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, '
|
|
|
37
44
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
38
45
|
* full control over when your component tracks state changes.
|
|
39
46
|
*
|
|
47
|
+
* ## Cleanup on Destroy
|
|
48
|
+
*
|
|
49
|
+
* By default, the function cancels any pending batch when the component is destroyed.
|
|
50
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* const batcher = injectBatcher(fn, {
|
|
54
|
+
* maxSize: 5,
|
|
55
|
+
* onUnmount: (b) => b.flush()
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
40
59
|
* @example
|
|
41
60
|
* ```ts
|
|
42
61
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -52,7 +71,7 @@ interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, '
|
|
|
52
71
|
* const { items, isPending } = batcher.state();
|
|
53
72
|
* ```
|
|
54
73
|
*/
|
|
55
|
-
declare function injectBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => void, options?:
|
|
74
|
+
declare function injectBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => void, options?: AngularBatcherOptions<TValue, TSelected>, selector?: (state: BatcherState<TValue>) => TSelected): AngularBatcher<TValue, TSelected>;
|
|
56
75
|
//#endregion
|
|
57
|
-
export { AngularBatcher, injectBatcher };
|
|
76
|
+
export { AngularBatcher, AngularBatcherOptions, injectBatcher };
|
|
58
77
|
//# sourceMappingURL=injectBatcher.d.cts.map
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
2
3
|
import { Batcher, BatcherOptions, BatcherState } from "@tanstack/pacer/batcher";
|
|
3
|
-
import { Store } from "@tanstack/store";
|
|
4
4
|
|
|
5
5
|
//#region src/batcher/injectBatcher.d.ts
|
|
6
|
+
interface AngularBatcherOptions<TValue, TSelected = {}> extends BatcherOptions<TValue> {
|
|
7
|
+
/**
|
|
8
|
+
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
9
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
10
|
+
*/
|
|
11
|
+
onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void;
|
|
12
|
+
}
|
|
6
13
|
interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, 'store'> {
|
|
7
14
|
/**
|
|
8
15
|
* Reactive state signal that will be updated when the batcher state changes
|
|
@@ -37,6 +44,18 @@ interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, '
|
|
|
37
44
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
38
45
|
* full control over when your component tracks state changes.
|
|
39
46
|
*
|
|
47
|
+
* ## Cleanup on Destroy
|
|
48
|
+
*
|
|
49
|
+
* By default, the function cancels any pending batch when the component is destroyed.
|
|
50
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* const batcher = injectBatcher(fn, {
|
|
54
|
+
* maxSize: 5,
|
|
55
|
+
* onUnmount: (b) => b.flush()
|
|
56
|
+
* });
|
|
57
|
+
* ```
|
|
58
|
+
*
|
|
40
59
|
* @example
|
|
41
60
|
* ```ts
|
|
42
61
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -52,7 +71,7 @@ interface AngularBatcher<TValue, TSelected = {}> extends Omit<Batcher<TValue>, '
|
|
|
52
71
|
* const { items, isPending } = batcher.state();
|
|
53
72
|
* ```
|
|
54
73
|
*/
|
|
55
|
-
declare function injectBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => void, options?:
|
|
74
|
+
declare function injectBatcher<TValue, TSelected = {}>(fn: (items: Array<TValue>) => void, options?: AngularBatcherOptions<TValue, TSelected>, selector?: (state: BatcherState<TValue>) => TSelected): AngularBatcher<TValue, TSelected>;
|
|
56
75
|
//#endregion
|
|
57
|
-
export { AngularBatcher, injectBatcher };
|
|
76
|
+
export { AngularBatcher, AngularBatcherOptions, injectBatcher };
|
|
58
77
|
//# sourceMappingURL=injectBatcher.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 { Batcher } from "@tanstack/pacer/batcher";
|
|
4
5
|
|
|
@@ -24,6 +25,18 @@ import { Batcher } from "@tanstack/pacer/batcher";
|
|
|
24
25
|
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
25
26
|
* full control over when your component tracks state changes.
|
|
26
27
|
*
|
|
28
|
+
* ## Cleanup on Destroy
|
|
29
|
+
*
|
|
30
|
+
* By default, the function cancels any pending batch when the component is destroyed.
|
|
31
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* const batcher = injectBatcher(fn, {
|
|
35
|
+
* maxSize: 5,
|
|
36
|
+
* onUnmount: (b) => b.flush()
|
|
37
|
+
* });
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
27
40
|
* @example
|
|
28
41
|
* ```ts
|
|
29
42
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -40,15 +53,21 @@ import { Batcher } from "@tanstack/pacer/batcher";
|
|
|
40
53
|
* ```
|
|
41
54
|
*/
|
|
42
55
|
function injectBatcher(fn, options = {}, selector = () => ({})) {
|
|
43
|
-
const
|
|
56
|
+
const mergedOptions = {
|
|
44
57
|
...injectPacerOptions().batcher,
|
|
45
58
|
...options
|
|
46
|
-
}
|
|
59
|
+
};
|
|
60
|
+
const batcher = new Batcher(fn, mergedOptions);
|
|
47
61
|
const state = injectStore(batcher.store, selector);
|
|
48
|
-
|
|
62
|
+
const result = {
|
|
49
63
|
...batcher,
|
|
50
64
|
state
|
|
51
65
|
};
|
|
66
|
+
inject(DestroyRef, { optional: true })?.onDestroy(() => {
|
|
67
|
+
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
68
|
+
else batcher.cancel();
|
|
69
|
+
});
|
|
70
|
+
return result;
|
|
52
71
|
}
|
|
53
72
|
|
|
54
73
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectBatcher.js","names":[],"sources":["../../src/batcher/injectBatcher.ts"],"sourcesContent":["import { injectStore } from '@tanstack/angular-store'\nimport { Batcher } from '@tanstack/pacer/batcher'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/store'\nimport type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface AngularBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages a Batcher instance.\n *\n * This is a lower-level function that provides direct access to the Batcher's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The Batcher collects items and processes them in batches based on configurable conditions:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\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 batcher = injectBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 }\n * );\n *\n * // Add items\n * batcher.addItem('task1');\n *\n * // Access the selected state\n * const { items, isPending } = batcher.state();\n * ```\n */\nexport function injectBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options:
|
|
1
|
+
{"version":3,"file":"injectBatcher.js","names":[],"sources":["../../src/batcher/injectBatcher.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { Batcher } from '@tanstack/pacer/batcher'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'\n\nexport interface AngularBatcherOptions<\n TValue,\n TSelected = {},\n> extends BatcherOptions<TValue> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the batcher instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.\n */\n onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void\n}\n\nexport interface AngularBatcher<TValue, TSelected = {}> extends Omit<\n Batcher<TValue>,\n 'store'\n> {\n /**\n * Reactive state signal that will be updated when the batcher state changes\n *\n * Use this instead of `batcher.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `batcher.state` instead of `batcher.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<BatcherState<TValue>>>\n}\n\n/**\n * An Angular function that creates and manages a Batcher instance.\n *\n * This is a lower-level function that provides direct access to the Batcher's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * The Batcher collects items and processes them in batches based on configurable conditions:\n * - Maximum batch size\n * - Time-based batching (process after X milliseconds)\n * - Custom batch processing logic via getShouldExecute\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending batch 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 batcher = injectBatcher(fn, {\n * maxSize: 5,\n * onUnmount: (b) => b.flush()\n * });\n * ```\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const batcher = injectBatcher(\n * (items) => console.log('Processing batch:', items),\n * { maxSize: 5, wait: 2000 }\n * );\n *\n * // Add items\n * batcher.addItem('task1');\n *\n * // Access the selected state\n * const { items, isPending } = batcher.state();\n * ```\n */\nexport function injectBatcher<TValue, TSelected = {}>(\n fn: (items: Array<TValue>) => void,\n options: AngularBatcherOptions<TValue, TSelected> = {},\n selector: (state: BatcherState<TValue>) => TSelected = () =>\n ({}) as TSelected,\n): AngularBatcher<TValue, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().batcher,\n ...options,\n } as AngularBatcherOptions<TValue, TSelected>\n\n const batcher = new Batcher<TValue>(fn, mergedOptions)\n const state = injectStore(batcher.store, selector)\n\n const result = {\n ...batcher,\n state,\n } as AngularBatcher<TValue, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n batcher.cancel()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFA,SAAgB,cACd,IACA,UAAoD,EAAE,EACtD,kBACG,EAAE,GAC8B;CACnC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,UAAU,IAAI,QAAgB,IAAI,cAAc;CACtD,MAAM,QAAQ,YAAY,QAAQ,OAAO,SAAS;CAElD,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,SAAQ,QAAQ;GAElB;AAEF,QAAO"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectDebouncedCallback } from "./injectDebouncedCallback.cjs";
|
|
2
|
-
import { AngularDebouncer, injectDebouncer } from "./injectDebouncer.cjs";
|
|
2
|
+
import { AngularDebouncer, AngularDebouncerOptions, injectDebouncer } from "./injectDebouncer.cjs";
|
|
3
3
|
import { DebouncedSignal, injectDebouncedSignal } from "./injectDebouncedSignal.cjs";
|
|
4
4
|
import { injectDebouncedValue } from "./injectDebouncedValue.cjs";
|
|
5
5
|
export * from "@tanstack/pacer/debouncer";
|
|
6
|
-
export { AngularDebouncer, DebouncedSignal, injectDebouncedCallback, injectDebouncedSignal, injectDebouncedValue, injectDebouncer };
|
|
6
|
+
export { AngularDebouncer, AngularDebouncerOptions, DebouncedSignal, injectDebouncedCallback, injectDebouncedSignal, injectDebouncedValue, injectDebouncer };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { injectDebouncedCallback } from "./injectDebouncedCallback.js";
|
|
2
|
-
import { AngularDebouncer, injectDebouncer } from "./injectDebouncer.js";
|
|
2
|
+
import { AngularDebouncer, AngularDebouncerOptions, injectDebouncer } from "./injectDebouncer.js";
|
|
3
3
|
import { DebouncedSignal, injectDebouncedSignal } from "./injectDebouncedSignal.js";
|
|
4
4
|
import { injectDebouncedValue } from "./injectDebouncedValue.js";
|
|
5
5
|
export * from "@tanstack/pacer/debouncer";
|
|
6
|
-
export { AngularDebouncer, DebouncedSignal, injectDebouncedCallback, injectDebouncedSignal, injectDebouncedValue, injectDebouncer };
|
|
6
|
+
export { AngularDebouncer, AngularDebouncerOptions, DebouncedSignal, injectDebouncedCallback, injectDebouncedSignal, injectDebouncedValue, injectDebouncer };
|
|
@@ -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_debouncer = require("@tanstack/pacer/debouncer");
|
|
4
5
|
|
|
@@ -34,6 +35,18 @@ let _tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
|
34
35
|
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
36
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
36
37
|
*
|
|
38
|
+
* ## Cleanup on Destroy
|
|
39
|
+
*
|
|
40
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
41
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* const debouncer = injectDebouncer(fn, {
|
|
45
|
+
* wait: 500,
|
|
46
|
+
* onUnmount: (d) => d.flush()
|
|
47
|
+
* });
|
|
48
|
+
* ```
|
|
49
|
+
*
|
|
37
50
|
* @example
|
|
38
51
|
* ```ts
|
|
39
52
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -60,15 +73,21 @@ let _tanstack_pacer_debouncer = require("@tanstack/pacer/debouncer");
|
|
|
60
73
|
* ```
|
|
61
74
|
*/
|
|
62
75
|
function injectDebouncer(fn, options, selector = () => ({})) {
|
|
63
|
-
const
|
|
76
|
+
const mergedOptions = {
|
|
64
77
|
...require_pacer_context.injectPacerOptions().debouncer,
|
|
65
78
|
...options
|
|
66
|
-
}
|
|
79
|
+
};
|
|
80
|
+
const debouncer = new _tanstack_pacer_debouncer.Debouncer(fn, mergedOptions);
|
|
67
81
|
const state = (0, _tanstack_angular_store.injectStore)(debouncer.store, selector);
|
|
68
|
-
|
|
82
|
+
const result = {
|
|
69
83
|
...debouncer,
|
|
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 debouncer.cancel();
|
|
89
|
+
});
|
|
90
|
+
return result;
|
|
72
91
|
}
|
|
73
92
|
|
|
74
93
|
//#endregion
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"injectDebouncer.cjs","names":["Debouncer","
|
|
1
|
+
{"version":3,"file":"injectDebouncer.cjs","names":["injectPacerOptions","Debouncer","DestroyRef"],"sources":["../../src/debouncer/injectDebouncer.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectStore } from '@tanstack/angular-store'\nimport { Debouncer } from '@tanstack/pacer/debouncer'\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 DebouncerOptions,\n DebouncerState,\n} from '@tanstack/pacer/debouncer'\n\nexport interface AngularDebouncerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends DebouncerOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the debouncer instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.\n */\n onUnmount?: (debouncer: AngularDebouncer<TFn, TSelected>) => void\n}\n\nexport interface AngularDebouncer<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Debouncer<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the debouncer state changes\n *\n * Use this instead of `debouncer.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages a Debouncer instance.\n *\n * This is a lower-level function that provides direct access to the Debouncer's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides debouncing functionality to limit how often a function can be called,\n * waiting for a specified delay before executing the latest call. This is useful for handling\n * frequent events like window resizing, scroll events, or real-time search inputs.\n *\n * The debouncer will only execute the function after the specified wait time has elapsed\n * since the last call. If the function is called again before the wait time expires, the\n * timer resets and starts waiting again.\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 debouncer can execute on the leading edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\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 debouncer = injectDebouncer(fn, {\n * wait: 500,\n * onUnmount: (d) => d.flush()\n * });\n * ```\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const debouncer = injectDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 }\n * );\n *\n * // Opt-in to track isPending changes (optimized for loading states)\n * const debouncer = injectDebouncer(\n * (query: string) => fetchSearchResults(query),\n * { wait: 500 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // In an event handler\n * const handleChange = (e: Event) => {\n * const target = e.target as HTMLInputElement\n * debouncer.maybeExecute(target.value);\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = debouncer.state();\n * ```\n */\nexport function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: AngularDebouncerOptions<TFn, TSelected>,\n selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,\n): AngularDebouncer<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().debouncer,\n ...options,\n } as AngularDebouncerOptions<TFn, TSelected>\n\n const debouncer = new Debouncer<TFn>(fn, mergedOptions)\n const state = injectStore(debouncer.store, selector)\n\n const result = {\n ...debouncer,\n state,\n } as AngularDebouncer<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n debouncer.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 { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
5
5
|
|
|
6
6
|
//#region src/debouncer/injectDebouncer.d.ts
|
|
7
|
+
interface AngularDebouncerOptions<TFn extends AnyFunction, TSelected = {}> extends DebouncerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
*/
|
|
12
|
+
onUnmount?: (debouncer: AngularDebouncer<TFn, TSelected>) => void;
|
|
13
|
+
}
|
|
7
14
|
interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
8
15
|
/**
|
|
9
16
|
* Reactive state signal that will be updated when the debouncer state changes
|
|
@@ -48,6 +55,18 @@ interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
48
55
|
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
49
56
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
50
57
|
*
|
|
58
|
+
* ## Cleanup on Destroy
|
|
59
|
+
*
|
|
60
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
61
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const debouncer = injectDebouncer(fn, {
|
|
65
|
+
* wait: 500,
|
|
66
|
+
* onUnmount: (d) => d.flush()
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
51
70
|
* @example
|
|
52
71
|
* ```ts
|
|
53
72
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -73,7 +92,7 @@ interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
73
92
|
* const { isPending } = debouncer.state();
|
|
74
93
|
* ```
|
|
75
94
|
*/
|
|
76
|
-
declare function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
95
|
+
declare function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularDebouncerOptions<TFn, TSelected>, selector?: (state: DebouncerState<TFn>) => TSelected): AngularDebouncer<TFn, TSelected>;
|
|
77
96
|
//#endregion
|
|
78
|
-
export { AngularDebouncer, injectDebouncer };
|
|
97
|
+
export { AngularDebouncer, AngularDebouncerOptions, injectDebouncer };
|
|
79
98
|
//# sourceMappingURL=injectDebouncer.d.cts.map
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/angular-store";
|
|
2
3
|
import { Debouncer, DebouncerOptions, DebouncerState } from "@tanstack/pacer/debouncer";
|
|
3
4
|
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Store } from "@tanstack/store";
|
|
5
5
|
|
|
6
6
|
//#region src/debouncer/injectDebouncer.d.ts
|
|
7
|
+
interface AngularDebouncerOptions<TFn extends AnyFunction, TSelected = {}> extends DebouncerOptions<TFn> {
|
|
8
|
+
/**
|
|
9
|
+
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
10
|
+
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
+
*/
|
|
12
|
+
onUnmount?: (debouncer: AngularDebouncer<TFn, TSelected>) => void;
|
|
13
|
+
}
|
|
7
14
|
interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit<Debouncer<TFn>, 'store'> {
|
|
8
15
|
/**
|
|
9
16
|
* Reactive state signal that will be updated when the debouncer state changes
|
|
@@ -48,6 +55,18 @@ interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
48
55
|
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
49
56
|
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
50
57
|
*
|
|
58
|
+
* ## Cleanup on Destroy
|
|
59
|
+
*
|
|
60
|
+
* By default, the function cancels any pending execution when the component is destroyed.
|
|
61
|
+
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* const debouncer = injectDebouncer(fn, {
|
|
65
|
+
* wait: 500,
|
|
66
|
+
* onUnmount: (d) => d.flush()
|
|
67
|
+
* });
|
|
68
|
+
* ```
|
|
69
|
+
*
|
|
51
70
|
* @example
|
|
52
71
|
* ```ts
|
|
53
72
|
* // Default behavior - no reactive state subscriptions
|
|
@@ -73,7 +92,7 @@ interface AngularDebouncer<TFn extends AnyFunction, TSelected = {}> extends Omit
|
|
|
73
92
|
* const { isPending } = debouncer.state();
|
|
74
93
|
* ```
|
|
75
94
|
*/
|
|
76
|
-
declare function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options:
|
|
95
|
+
declare function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularDebouncerOptions<TFn, TSelected>, selector?: (state: DebouncerState<TFn>) => TSelected): AngularDebouncer<TFn, TSelected>;
|
|
77
96
|
//#endregion
|
|
78
|
-
export { AngularDebouncer, injectDebouncer };
|
|
97
|
+
export { AngularDebouncer, AngularDebouncerOptions, injectDebouncer };
|
|
79
98
|
//# sourceMappingURL=injectDebouncer.d.ts.map
|