@tanstack/angular-pacer 0.23.1 → 0.25.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/README.md +21 -6
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +2 -5
- package/dist/async-batcher/injectAsyncBatchedCallback.js +1 -2
- package/dist/async-batcher/injectAsyncBatcher.d.ts +4 -7
- package/dist/async-batcher/injectAsyncBatcher.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +2 -5
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +4 -7
- package/dist/async-debouncer/injectAsyncDebouncer.js +1 -2
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +3 -6
- package/dist/async-queuer/injectAsyncQueuedSignal.js +1 -2
- package/dist/async-queuer/injectAsyncQueuer.d.ts +4 -7
- package/dist/async-queuer/injectAsyncQueuer.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +2 -5
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +4 -7
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +1 -2
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +2 -5
- package/dist/async-throttler/injectAsyncThrottledCallback.js +1 -2
- package/dist/async-throttler/injectAsyncThrottler.d.ts +4 -7
- package/dist/async-throttler/injectAsyncThrottler.js +1 -2
- package/dist/batcher/injectBatchedCallback.d.ts +2 -5
- package/dist/batcher/injectBatchedCallback.js +1 -2
- package/dist/batcher/injectBatcher.d.ts +4 -7
- package/dist/batcher/injectBatcher.js +1 -2
- package/dist/debouncer/injectDebouncedCallback.d.ts +2 -5
- package/dist/debouncer/injectDebouncedCallback.js +1 -2
- package/dist/debouncer/injectDebouncedSignal.d.ts +3 -6
- package/dist/debouncer/injectDebouncedSignal.js +1 -2
- package/dist/debouncer/injectDebouncedValue.d.ts +3 -6
- package/dist/debouncer/injectDebouncedValue.js +1 -2
- package/dist/debouncer/injectDebouncer.d.ts +4 -7
- package/dist/debouncer/injectDebouncer.js +1 -2
- package/dist/provider/pacer-context.d.ts +4 -7
- package/dist/provider/pacer-context.js +1 -2
- package/dist/provider/pacer-provider.d.ts +2 -5
- package/dist/provider/pacer-provider.js +1 -2
- package/dist/queuer/injectQueuedSignal.d.ts +3 -6
- package/dist/queuer/injectQueuedSignal.js +1 -2
- package/dist/queuer/injectQueuedValue.d.ts +3 -6
- package/dist/queuer/injectQueuedValue.js +1 -2
- package/dist/queuer/injectQueuer.d.ts +4 -7
- package/dist/queuer/injectQueuer.js +1 -2
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +2 -5
- package/dist/rate-limiter/injectRateLimitedCallback.js +1 -2
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +3 -6
- package/dist/rate-limiter/injectRateLimitedSignal.js +1 -2
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +3 -6
- package/dist/rate-limiter/injectRateLimitedValue.js +1 -2
- package/dist/rate-limiter/injectRateLimiter.d.ts +4 -7
- package/dist/rate-limiter/injectRateLimiter.js +1 -2
- package/dist/throttler/injectThrottledCallback.d.ts +2 -5
- package/dist/throttler/injectThrottledCallback.js +1 -2
- package/dist/throttler/injectThrottledSignal.d.ts +3 -8
- package/dist/throttler/injectThrottledSignal.js +1 -4
- package/dist/throttler/injectThrottledValue.d.ts +3 -8
- package/dist/throttler/injectThrottledValue.js +1 -2
- package/dist/throttler/injectThrottler.d.ts +4 -9
- package/dist/throttler/injectThrottler.js +1 -4
- package/package.json +25 -72
- package/dist/async-batcher/index.cjs +0 -13
- package/dist/async-batcher/index.d.cts +0 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs +0 -49
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatchedCallback.d.cts +0 -45
- package/dist/async-batcher/injectAsyncBatchedCallback.js.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.cjs +0 -85
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.d.cts +0 -85
- package/dist/async-batcher/injectAsyncBatcher.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -13
- package/dist/async-debouncer/index.d.cts +0 -4
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs +0 -49
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.cts +0 -43
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +0 -107
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +0 -108
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -13
- package/dist/async-queuer/index.d.cts +0 -4
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs +0 -53
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +0 -50
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.cjs +0 -85
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.d.cts +0 -85
- package/dist/async-queuer/injectAsyncQueuer.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -13
- package/dist/async-rate-limiter/index.d.cts +0 -4
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs +0 -52
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.cts +0 -46
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +0 -69
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +0 -72
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +0 -1
- package/dist/async-retryer/index.cjs +0 -9
- package/dist/async-retryer/index.d.cts +0 -1
- package/dist/async-throttler/index.cjs +0 -13
- package/dist/async-throttler/index.d.cts +0 -4
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs +0 -50
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottledCallback.d.cts +0 -44
- package/dist/async-throttler/injectAsyncThrottledCallback.js.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.cjs +0 -86
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.d.cts +0 -87
- package/dist/async-throttler/injectAsyncThrottler.js.map +0 -1
- package/dist/batcher/index.cjs +0 -13
- package/dist/batcher/index.d.cts +0 -4
- package/dist/batcher/injectBatchedCallback.cjs +0 -47
- package/dist/batcher/injectBatchedCallback.cjs.map +0 -1
- package/dist/batcher/injectBatchedCallback.d.cts +0 -43
- package/dist/batcher/injectBatchedCallback.js.map +0 -1
- package/dist/batcher/injectBatcher.cjs +0 -75
- package/dist/batcher/injectBatcher.cjs.map +0 -1
- package/dist/batcher/injectBatcher.d.cts +0 -77
- package/dist/batcher/injectBatcher.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -17
- package/dist/debouncer/index.d.cts +0 -6
- package/dist/debouncer/injectDebouncedCallback.cjs +0 -46
- package/dist/debouncer/injectDebouncedCallback.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedCallback.d.cts +0 -43
- package/dist/debouncer/injectDebouncedCallback.js.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.cjs +0 -68
- package/dist/debouncer/injectDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.d.cts +0 -60
- package/dist/debouncer/injectDebouncedSignal.js.map +0 -1
- package/dist/debouncer/injectDebouncedValue.cjs +0 -17
- package/dist/debouncer/injectDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedValue.d.cts +0 -76
- package/dist/debouncer/injectDebouncedValue.js.map +0 -1
- package/dist/debouncer/injectDebouncer.cjs +0 -95
- package/dist/debouncer/injectDebouncer.cjs.map +0 -1
- package/dist/debouncer/injectDebouncer.d.cts +0 -98
- package/dist/debouncer/injectDebouncer.js.map +0 -1
- package/dist/index.cjs +0 -65
- package/dist/index.d.cts +0 -30
- package/dist/provider/index.cjs +0 -7
- package/dist/provider/index.d.cts +0 -3
- package/dist/provider/pacer-context.cjs +0 -20
- package/dist/provider/pacer-context.cjs.map +0 -1
- package/dist/provider/pacer-context.d.cts +0 -21
- package/dist/provider/pacer-context.js.map +0 -1
- package/dist/provider/pacer-provider.cjs +0 -41
- package/dist/provider/pacer-provider.cjs.map +0 -1
- package/dist/provider/pacer-provider.d.cts +0 -36
- package/dist/provider/pacer-provider.js.map +0 -1
- package/dist/queuer/index.cjs +0 -15
- package/dist/queuer/index.d.cts +0 -5
- package/dist/queuer/injectQueuedSignal.cjs +0 -47
- package/dist/queuer/injectQueuedSignal.cjs.map +0 -1
- package/dist/queuer/injectQueuedSignal.d.cts +0 -44
- package/dist/queuer/injectQueuedSignal.js.map +0 -1
- package/dist/queuer/injectQueuedValue.cjs +0 -23
- package/dist/queuer/injectQueuedValue.cjs.map +0 -1
- package/dist/queuer/injectQueuedValue.d.cts +0 -34
- package/dist/queuer/injectQueuedValue.js.map +0 -1
- package/dist/queuer/injectQueuer.cjs +0 -80
- package/dist/queuer/injectQueuer.cjs.map +0 -1
- package/dist/queuer/injectQueuer.d.cts +0 -82
- package/dist/queuer/injectQueuer.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -17
- package/dist/rate-limiter/index.d.cts +0 -6
- package/dist/rate-limiter/injectRateLimitedCallback.cjs +0 -45
- package/dist/rate-limiter/injectRateLimitedCallback.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedCallback.d.cts +0 -42
- package/dist/rate-limiter/injectRateLimitedCallback.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.cjs +0 -62
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +0 -54
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.cjs +0 -17
- package/dist/rate-limiter/injectRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.d.cts +0 -49
- package/dist/rate-limiter/injectRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.cjs +0 -100
- package/dist/rate-limiter/injectRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.d.cts +0 -103
- package/dist/rate-limiter/injectRateLimiter.js.map +0 -1
- package/dist/throttler/index.cjs +0 -17
- package/dist/throttler/index.d.cts +0 -6
- package/dist/throttler/injectThrottledCallback.cjs +0 -45
- package/dist/throttler/injectThrottledCallback.cjs.map +0 -1
- package/dist/throttler/injectThrottledCallback.d.cts +0 -42
- package/dist/throttler/injectThrottledCallback.js.map +0 -1
- package/dist/throttler/injectThrottledSignal.cjs +0 -69
- package/dist/throttler/injectThrottledSignal.cjs.map +0 -1
- package/dist/throttler/injectThrottledSignal.d.cts +0 -61
- package/dist/throttler/injectThrottledSignal.js.map +0 -1
- package/dist/throttler/injectThrottledValue.cjs +0 -17
- package/dist/throttler/injectThrottledValue.cjs.map +0 -1
- package/dist/throttler/injectThrottledValue.d.cts +0 -74
- package/dist/throttler/injectThrottledValue.js.map +0 -1
- package/dist/throttler/injectThrottler.cjs +0 -95
- package/dist/throttler/injectThrottler.cjs.map +0 -1
- package/dist/throttler/injectThrottler.d.cts +0 -98
- package/dist/throttler/injectThrottler.js.map +0 -1
- package/dist/types/index.cjs +0 -9
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -9
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/index.ts +0 -4
- package/src/async-batcher/injectAsyncBatchedCallback.ts +0 -48
- package/src/async-batcher/injectAsyncBatcher.ts +0 -126
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-debouncer/injectAsyncDebouncedCallback.ts +0 -50
- package/src/async-debouncer/injectAsyncDebouncer.ts +0 -152
- package/src/async-queuer/index.ts +0 -4
- package/src/async-queuer/injectAsyncQueuedSignal.ts +0 -74
- package/src/async-queuer/injectAsyncQueuer.ts +0 -126
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-rate-limiter/injectAsyncRateLimitedCallback.ts +0 -52
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +0 -115
- package/src/async-retryer/index.ts +0 -1
- package/src/async-throttler/index.ts +0 -4
- package/src/async-throttler/injectAsyncThrottledCallback.ts +0 -50
- package/src/async-throttler/injectAsyncThrottler.ts +0 -131
- package/src/batcher/index.ts +0 -4
- package/src/batcher/injectBatchedCallback.ts +0 -46
- package/src/batcher/injectBatcher.ts +0 -114
- package/src/debouncer/index.ts +0 -6
- package/src/debouncer/injectDebouncedCallback.ts +0 -46
- package/src/debouncer/injectDebouncedSignal.ts +0 -94
- package/src/debouncer/injectDebouncedValue.ts +0 -126
- package/src/debouncer/injectDebouncer.ts +0 -137
- package/src/index.ts +0 -40
- package/src/provider/index.ts +0 -2
- package/src/provider/pacer-context.ts +0 -46
- package/src/provider/pacer-provider.ts +0 -38
- package/src/queuer/index.ts +0 -5
- package/src/queuer/injectQueuedSignal.ts +0 -65
- package/src/queuer/injectQueuedValue.ts +0 -101
- package/src/queuer/injectQueuer.ts +0 -118
- package/src/rate-limiter/index.ts +0 -6
- package/src/rate-limiter/injectRateLimitedCallback.ts +0 -45
- package/src/rate-limiter/injectRateLimitedSignal.ts +0 -88
- package/src/rate-limiter/injectRateLimitedValue.ts +0 -99
- package/src/rate-limiter/injectRateLimiter.ts +0 -140
- package/src/throttler/index.ts +0 -6
- package/src/throttler/injectThrottledCallback.ts +0 -45
- package/src/throttler/injectThrottledSignal.ts +0 -95
- package/src/throttler/injectThrottledValue.ts +0 -124
- package/src/throttler/injectThrottler.ts +0 -137
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type {
|
|
8
|
-
AsyncQueuerOptions,
|
|
9
|
-
AsyncQueuerState,
|
|
10
|
-
} from '@tanstack/pacer/async-queuer'
|
|
11
|
-
|
|
12
|
-
export interface AngularAsyncQueuerOptions<
|
|
13
|
-
TValue,
|
|
14
|
-
TSelected = {},
|
|
15
|
-
> extends AsyncQueuerOptions<TValue> {
|
|
16
|
-
/**
|
|
17
|
-
* Optional callback invoked when the component is destroyed. Receives the queuer instance.
|
|
18
|
-
* When provided, replaces the default cleanup (stop + abort); use it to call flush(), stop(), add logging, etc.
|
|
19
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (queuer: AngularAsyncQueuer<TValue, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
25
|
-
AsyncQueuer<TValue>,
|
|
26
|
-
'store'
|
|
27
|
-
> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the async queuer state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `queuer.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
36
|
-
* The state on the store object is not reactive in Angular signals.
|
|
37
|
-
*/
|
|
38
|
-
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages an AsyncQueuer instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the AsyncQueuer's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* The AsyncQueuer processes items asynchronously with support for concurrent execution,
|
|
48
|
-
* promise-based processing, error handling, retry capabilities, and abort support.
|
|
49
|
-
*
|
|
50
|
-
* ## State Management and Selector
|
|
51
|
-
*
|
|
52
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
53
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
54
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
55
|
-
*
|
|
56
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
57
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
58
|
-
* full control over when your component tracks state changes.
|
|
59
|
-
*
|
|
60
|
-
* ## Cleanup on Destroy
|
|
61
|
-
*
|
|
62
|
-
* By default, the function stops the queuer and aborts in-flight work when the component is destroyed.
|
|
63
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
64
|
-
*
|
|
65
|
-
* ```ts
|
|
66
|
-
* const queuer = injectAsyncQueuer(fn, {
|
|
67
|
-
* concurrency: 2,
|
|
68
|
-
* onUnmount: (q) => q.flush()
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```ts
|
|
76
|
-
* // Default behavior - no reactive state subscriptions
|
|
77
|
-
* const queuer = injectAsyncQueuer(
|
|
78
|
-
* async (item: Data) => {
|
|
79
|
-
* const response = await fetch('/api/process', {
|
|
80
|
-
* method: 'POST',
|
|
81
|
-
* body: JSON.stringify(item)
|
|
82
|
-
* });
|
|
83
|
-
* return response.json();
|
|
84
|
-
* },
|
|
85
|
-
* { concurrency: 2, wait: 1000 }
|
|
86
|
-
* );
|
|
87
|
-
*
|
|
88
|
-
* // Add items
|
|
89
|
-
* queuer.addItem(data1);
|
|
90
|
-
* queuer.addItem(data2);
|
|
91
|
-
*
|
|
92
|
-
* // Access the selected state
|
|
93
|
-
* const { items, isExecuting } = queuer.state();
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
export function injectAsyncQueuer<TValue, TSelected = {}>(
|
|
97
|
-
fn: (value: TValue) => Promise<any>,
|
|
98
|
-
options: AngularAsyncQueuerOptions<TValue, TSelected> = {},
|
|
99
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
|
|
100
|
-
({}) as TSelected,
|
|
101
|
-
): AngularAsyncQueuer<TValue, TSelected> {
|
|
102
|
-
const mergedOptions = {
|
|
103
|
-
...injectPacerOptions().asyncQueuer,
|
|
104
|
-
...options,
|
|
105
|
-
} as AngularAsyncQueuerOptions<TValue, TSelected>
|
|
106
|
-
|
|
107
|
-
const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)
|
|
108
|
-
const state = injectSelector(queuer.store, selector)
|
|
109
|
-
|
|
110
|
-
const result = {
|
|
111
|
-
...queuer,
|
|
112
|
-
state,
|
|
113
|
-
} as AngularAsyncQueuer<TValue, TSelected>
|
|
114
|
-
|
|
115
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
116
|
-
destroyRef?.onDestroy(() => {
|
|
117
|
-
if (mergedOptions.onUnmount) {
|
|
118
|
-
mergedOptions.onUnmount(result)
|
|
119
|
-
} else {
|
|
120
|
-
queuer.stop()
|
|
121
|
-
queuer.abort()
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
return result
|
|
126
|
-
}
|
|
@@ -1,52 +0,0 @@
|
|
|
1
|
-
import { injectAsyncRateLimiter } from './injectAsyncRateLimiter'
|
|
2
|
-
import type { AsyncRateLimiterOptions } from '@tanstack/pacer/async-rate-limiter'
|
|
3
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates an async rate-limited version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectAsyncRateLimiter` that provides
|
|
8
|
-
* a simplified API for basic async rate limiting needs.
|
|
9
|
-
*
|
|
10
|
-
* This function provides a simpler API compared to `injectAsyncRateLimiter`, making it ideal for basic
|
|
11
|
-
* async rate limiting needs. However, it does not expose the underlying AsyncRateLimiter instance.
|
|
12
|
-
*
|
|
13
|
-
* For advanced usage requiring features like:
|
|
14
|
-
* - Manual cancellation/reset
|
|
15
|
-
* - Access to execution counts
|
|
16
|
-
* - Error handling callbacks
|
|
17
|
-
* - Retry support
|
|
18
|
-
*
|
|
19
|
-
* Consider using the `injectAsyncRateLimiter` function instead.
|
|
20
|
-
*
|
|
21
|
-
* @example
|
|
22
|
-
* ```ts
|
|
23
|
-
* // Rate limit API calls
|
|
24
|
-
* const makeApiCall = injectAsyncRateLimitedCallback(
|
|
25
|
-
* async (data: ApiData) => {
|
|
26
|
-
* const response = await fetch('/api/endpoint', {
|
|
27
|
-
* method: 'POST',
|
|
28
|
-
* body: JSON.stringify(data)
|
|
29
|
-
* });
|
|
30
|
-
* return response.json();
|
|
31
|
-
* },
|
|
32
|
-
* {
|
|
33
|
-
* limit: 5,
|
|
34
|
-
* window: 60000,
|
|
35
|
-
* windowType: 'sliding',
|
|
36
|
-
* }
|
|
37
|
-
* );
|
|
38
|
-
*
|
|
39
|
-
* // Use in event handler
|
|
40
|
-
* const result = await makeApiCall(apiData);
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export function injectAsyncRateLimitedCallback<TFn extends AnyAsyncFunction>(
|
|
44
|
-
fn: TFn,
|
|
45
|
-
options: AsyncRateLimiterOptions<TFn>,
|
|
46
|
-
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>> | undefined> {
|
|
47
|
-
const rateLimiter = injectAsyncRateLimiter(fn, options)
|
|
48
|
-
return async (...args: Parameters<TFn>) => {
|
|
49
|
-
const result = await rateLimiter.maybeExecute(...args)
|
|
50
|
-
return result !== undefined ? await result : undefined
|
|
51
|
-
}
|
|
52
|
-
}
|
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
AsyncRateLimiterOptions,
|
|
10
|
-
AsyncRateLimiterState,
|
|
11
|
-
} from '@tanstack/pacer/async-rate-limiter'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncRateLimiterOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncRateLimiterOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
19
|
-
* When provided, replaces the default cleanup (abort).
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularAsyncRateLimiter<
|
|
25
|
-
TFn extends AnyAsyncFunction,
|
|
26
|
-
TSelected = {},
|
|
27
|
-
> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the async rate limiter state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `rateLimiter.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
36
|
-
* The state on the store object is not reactive in Angular signals.
|
|
37
|
-
*/
|
|
38
|
-
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages an AsyncRateLimiter instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* This function provides async rate limiting functionality with promise support, error handling,
|
|
48
|
-
* retry capabilities, and abort support.
|
|
49
|
-
*
|
|
50
|
-
* ## State Management and Selector
|
|
51
|
-
*
|
|
52
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
53
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
54
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
55
|
-
*
|
|
56
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
57
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
58
|
-
* full control over when your component tracks state changes.
|
|
59
|
-
*
|
|
60
|
-
* ## Cleanup on Destroy
|
|
61
|
-
*
|
|
62
|
-
* By default, the function aborts in-flight work when the component is destroyed.
|
|
63
|
-
* Use the `onUnmount` option to customize this.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```ts
|
|
67
|
-
* // Default behavior - no reactive state subscriptions
|
|
68
|
-
* const rateLimiter = injectAsyncRateLimiter(
|
|
69
|
-
* async (id: string) => {
|
|
70
|
-
* const response = await fetch(`/api/data/${id}`);
|
|
71
|
-
* return response.json();
|
|
72
|
-
* },
|
|
73
|
-
* { limit: 5, window: 60000, windowType: 'sliding' }
|
|
74
|
-
* );
|
|
75
|
-
*
|
|
76
|
-
* // In an event handler
|
|
77
|
-
* const handleRequest = async (id: string) => {
|
|
78
|
-
* const result = await rateLimiter.maybeExecute(id);
|
|
79
|
-
* console.log('Result:', result);
|
|
80
|
-
* };
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export function injectAsyncRateLimiter<
|
|
84
|
-
TFn extends AnyAsyncFunction,
|
|
85
|
-
TSelected = {},
|
|
86
|
-
>(
|
|
87
|
-
fn: TFn,
|
|
88
|
-
options: AngularAsyncRateLimiterOptions<TFn, TSelected>,
|
|
89
|
-
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
90
|
-
({}) as TSelected,
|
|
91
|
-
): AngularAsyncRateLimiter<TFn, TSelected> {
|
|
92
|
-
const mergedOptions = {
|
|
93
|
-
...injectPacerOptions().asyncRateLimiter,
|
|
94
|
-
...options,
|
|
95
|
-
} as AngularAsyncRateLimiterOptions<TFn, TSelected>
|
|
96
|
-
|
|
97
|
-
const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)
|
|
98
|
-
const state = injectSelector(rateLimiter.store, selector)
|
|
99
|
-
|
|
100
|
-
const result = {
|
|
101
|
-
...rateLimiter,
|
|
102
|
-
state,
|
|
103
|
-
} as AngularAsyncRateLimiter<TFn, TSelected>
|
|
104
|
-
|
|
105
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
106
|
-
destroyRef?.onDestroy(() => {
|
|
107
|
-
if (mergedOptions.onUnmount) {
|
|
108
|
-
mergedOptions.onUnmount(result)
|
|
109
|
-
} else {
|
|
110
|
-
rateLimiter.abort()
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
return result
|
|
115
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@tanstack/pacer/async-retryer'
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { injectAsyncThrottler } from './injectAsyncThrottler'
|
|
2
|
-
import type { AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler'
|
|
3
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates an async throttled version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectAsyncThrottler` that provides
|
|
8
|
-
* a simplified API for basic async throttling needs.
|
|
9
|
-
*
|
|
10
|
-
* The throttled function will execute at most once within the specified wait time.
|
|
11
|
-
*
|
|
12
|
-
* This function provides a simpler API compared to `injectAsyncThrottler`, making it ideal for basic
|
|
13
|
-
* async throttling needs. However, it does not expose the underlying AsyncThrottler instance.
|
|
14
|
-
*
|
|
15
|
-
* For advanced usage requiring features like:
|
|
16
|
-
* - Manual cancellation
|
|
17
|
-
* - Access to execution counts
|
|
18
|
-
* - Error handling callbacks
|
|
19
|
-
* - Retry support
|
|
20
|
-
*
|
|
21
|
-
* Consider using the `injectAsyncThrottler` function instead.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* // Throttle an async update handler
|
|
26
|
-
* const handleUpdate = injectAsyncThrottledCallback(
|
|
27
|
-
* async (data: Data) => {
|
|
28
|
-
* const response = await fetch('/api/update', {
|
|
29
|
-
* method: 'POST',
|
|
30
|
-
* body: JSON.stringify(data)
|
|
31
|
-
* });
|
|
32
|
-
* return response.json();
|
|
33
|
-
* },
|
|
34
|
-
* { wait: 1000 }
|
|
35
|
-
* );
|
|
36
|
-
*
|
|
37
|
-
* // Use in an event handler
|
|
38
|
-
* const result = await handleUpdate(updateData);
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export function injectAsyncThrottledCallback<TFn extends AnyAsyncFunction>(
|
|
42
|
-
fn: TFn,
|
|
43
|
-
options: AsyncThrottlerOptions<TFn>,
|
|
44
|
-
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>> | undefined> {
|
|
45
|
-
const throttler = injectAsyncThrottler(fn, options)
|
|
46
|
-
return async (...args: Parameters<TFn>) => {
|
|
47
|
-
const result = await throttler.maybeExecute(...args)
|
|
48
|
-
return result !== undefined ? await result : undefined
|
|
49
|
-
}
|
|
50
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncThrottler } from '@tanstack/pacer/async-throttler'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
AsyncThrottlerOptions,
|
|
10
|
-
AsyncThrottlerState,
|
|
11
|
-
} from '@tanstack/pacer/async-throttler'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncThrottlerOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncThrottlerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
21
|
-
*/
|
|
22
|
-
onUnmount?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface AngularAsyncThrottler<
|
|
26
|
-
TFn extends AnyAsyncFunction,
|
|
27
|
-
TSelected = {},
|
|
28
|
-
> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
29
|
-
/**
|
|
30
|
-
* Reactive state signal that will be updated when the async throttler state changes
|
|
31
|
-
*
|
|
32
|
-
* Use this instead of `throttler.store.state`
|
|
33
|
-
*/
|
|
34
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
37
|
-
* The state on the store object is not reactive in Angular signals.
|
|
38
|
-
*/
|
|
39
|
-
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* An Angular function that creates and manages an AsyncThrottler instance.
|
|
44
|
-
*
|
|
45
|
-
* This is a lower-level function that provides direct access to the AsyncThrottler's functionality.
|
|
46
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
47
|
-
*
|
|
48
|
-
* This function provides async throttling functionality with promise support, error handling,
|
|
49
|
-
* retry capabilities, and abort support.
|
|
50
|
-
*
|
|
51
|
-
* The throttler will execute the function at most once within the specified wait time.
|
|
52
|
-
*
|
|
53
|
-
* ## State Management and Selector
|
|
54
|
-
*
|
|
55
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
56
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
57
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
58
|
-
*
|
|
59
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
60
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
61
|
-
* full control over when your component tracks state changes.
|
|
62
|
-
*
|
|
63
|
-
* ## Cleanup on Destroy
|
|
64
|
-
*
|
|
65
|
-
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
66
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
67
|
-
*
|
|
68
|
-
* ```ts
|
|
69
|
-
* const throttler = injectAsyncThrottler(fn, {
|
|
70
|
-
* wait: 1000,
|
|
71
|
-
* onUnmount: (t) => t.flush()
|
|
72
|
-
* });
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* // Default behavior - no reactive state subscriptions
|
|
80
|
-
* const throttler = injectAsyncThrottler(
|
|
81
|
-
* async (data: Data) => {
|
|
82
|
-
* const response = await fetch('/api/update', {
|
|
83
|
-
* method: 'POST',
|
|
84
|
-
* body: JSON.stringify(data)
|
|
85
|
-
* });
|
|
86
|
-
* return response.json();
|
|
87
|
-
* },
|
|
88
|
-
* { wait: 1000 }
|
|
89
|
-
* );
|
|
90
|
-
*
|
|
91
|
-
* // In an event handler
|
|
92
|
-
* const handleUpdate = async (data: Data) => {
|
|
93
|
-
* const result = await throttler.maybeExecute(data);
|
|
94
|
-
* console.log('Update result:', result);
|
|
95
|
-
* };
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export function injectAsyncThrottler<
|
|
99
|
-
TFn extends AnyAsyncFunction,
|
|
100
|
-
TSelected = {},
|
|
101
|
-
>(
|
|
102
|
-
fn: TFn,
|
|
103
|
-
options: AngularAsyncThrottlerOptions<TFn, TSelected>,
|
|
104
|
-
selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>
|
|
105
|
-
({}) as TSelected,
|
|
106
|
-
): AngularAsyncThrottler<TFn, TSelected> {
|
|
107
|
-
const mergedOptions = {
|
|
108
|
-
...injectPacerOptions().asyncThrottler,
|
|
109
|
-
...options,
|
|
110
|
-
} as AngularAsyncThrottlerOptions<TFn, TSelected>
|
|
111
|
-
|
|
112
|
-
const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)
|
|
113
|
-
const state = injectSelector(throttler.store, selector)
|
|
114
|
-
|
|
115
|
-
const result = {
|
|
116
|
-
...throttler,
|
|
117
|
-
state,
|
|
118
|
-
} as AngularAsyncThrottler<TFn, TSelected>
|
|
119
|
-
|
|
120
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
121
|
-
destroyRef?.onDestroy(() => {
|
|
122
|
-
if (mergedOptions.onUnmount) {
|
|
123
|
-
mergedOptions.onUnmount(result)
|
|
124
|
-
} else {
|
|
125
|
-
throttler.cancel()
|
|
126
|
-
throttler.abort()
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
return result
|
|
131
|
-
}
|
package/src/batcher/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { injectBatcher } from './injectBatcher'
|
|
2
|
-
import type { BatcherOptions } from '@tanstack/pacer/batcher'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* An Angular function that creates a batched version of a callback function.
|
|
6
|
-
* This function is essentially a wrapper around `injectBatcher` that provides
|
|
7
|
-
* a simplified API for basic batching needs.
|
|
8
|
-
*
|
|
9
|
-
* The batched function will collect items and process them in batches based on
|
|
10
|
-
* the configured conditions (maxSize, wait time, etc.).
|
|
11
|
-
*
|
|
12
|
-
* This function provides a simpler API compared to `injectBatcher`, making it ideal for basic
|
|
13
|
-
* batching needs. However, it does not expose the underlying Batcher instance.
|
|
14
|
-
*
|
|
15
|
-
* For advanced usage requiring features like:
|
|
16
|
-
* - Manual flushing
|
|
17
|
-
* - Access to batch state
|
|
18
|
-
* - State tracking
|
|
19
|
-
*
|
|
20
|
-
* Consider using the `injectBatcher` function instead.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* // Batch API calls
|
|
25
|
-
* const batchApiCall = injectBatchedCallback(
|
|
26
|
-
* (items) => {
|
|
27
|
-
* return fetch('/api/batch', {
|
|
28
|
-
* method: 'POST',
|
|
29
|
-
* body: JSON.stringify(items)
|
|
30
|
-
* });
|
|
31
|
-
* },
|
|
32
|
-
* { maxSize: 10, wait: 1000 }
|
|
33
|
-
* );
|
|
34
|
-
*
|
|
35
|
-
* // Items will be batched and sent together
|
|
36
|
-
* batchApiCall('item1');
|
|
37
|
-
* batchApiCall('item2');
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function injectBatchedCallback<TValue>(
|
|
41
|
-
fn: (items: Array<TValue>) => void,
|
|
42
|
-
options: BatcherOptions<TValue>,
|
|
43
|
-
): (item: TValue) => void {
|
|
44
|
-
const batcher = injectBatcher(fn, options)
|
|
45
|
-
return (item: TValue) => batcher.addItem(item)
|
|
46
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { Batcher } from '@tanstack/pacer/batcher'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'
|
|
8
|
-
|
|
9
|
-
export interface AngularBatcherOptions<
|
|
10
|
-
TValue,
|
|
11
|
-
TSelected = {},
|
|
12
|
-
> extends BatcherOptions<TValue> {
|
|
13
|
-
/**
|
|
14
|
-
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
15
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
16
|
-
*/
|
|
17
|
-
onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AngularBatcher<TValue, TSelected = {}> extends Omit<
|
|
21
|
-
Batcher<TValue>,
|
|
22
|
-
'store'
|
|
23
|
-
> {
|
|
24
|
-
/**
|
|
25
|
-
* Reactive state signal that will be updated when the batcher state changes
|
|
26
|
-
*
|
|
27
|
-
* Use this instead of `batcher.store.state`
|
|
28
|
-
*/
|
|
29
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
30
|
-
/**
|
|
31
|
-
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
32
|
-
* The state on the store object is not reactive in Angular signals.
|
|
33
|
-
*/
|
|
34
|
-
readonly store: Store<Readonly<BatcherState<TValue>>>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* An Angular function that creates and manages a Batcher instance.
|
|
39
|
-
*
|
|
40
|
-
* This is a lower-level function that provides direct access to the Batcher's functionality.
|
|
41
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
42
|
-
*
|
|
43
|
-
* The Batcher collects items and processes them in batches based on configurable conditions:
|
|
44
|
-
* - Maximum batch size
|
|
45
|
-
* - Time-based batching (process after X milliseconds)
|
|
46
|
-
* - Custom batch processing logic via getShouldExecute
|
|
47
|
-
*
|
|
48
|
-
* ## State Management and Selector
|
|
49
|
-
*
|
|
50
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
51
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
52
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
53
|
-
*
|
|
54
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
55
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
56
|
-
* full control over when your component tracks state changes.
|
|
57
|
-
*
|
|
58
|
-
* ## Cleanup on Destroy
|
|
59
|
-
*
|
|
60
|
-
* By default, the function cancels any pending batch 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 batcher = injectBatcher(fn, {
|
|
65
|
-
* maxSize: 5,
|
|
66
|
-
* onUnmount: (b) => b.flush()
|
|
67
|
-
* });
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* // Default behavior - no reactive state subscriptions
|
|
73
|
-
* const batcher = injectBatcher(
|
|
74
|
-
* (items) => console.log('Processing batch:', items),
|
|
75
|
-
* { maxSize: 5, wait: 2000 }
|
|
76
|
-
* );
|
|
77
|
-
*
|
|
78
|
-
* // Add items
|
|
79
|
-
* batcher.addItem('task1');
|
|
80
|
-
*
|
|
81
|
-
* // Access the selected state
|
|
82
|
-
* const { items, isPending } = batcher.state();
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
export function injectBatcher<TValue, TSelected = {}>(
|
|
86
|
-
fn: (items: Array<TValue>) => void,
|
|
87
|
-
options: AngularBatcherOptions<TValue, TSelected> = {},
|
|
88
|
-
selector: (state: BatcherState<TValue>) => TSelected = () =>
|
|
89
|
-
({}) as TSelected,
|
|
90
|
-
): AngularBatcher<TValue, TSelected> {
|
|
91
|
-
const mergedOptions = {
|
|
92
|
-
...injectPacerOptions().batcher,
|
|
93
|
-
...options,
|
|
94
|
-
} as AngularBatcherOptions<TValue, TSelected>
|
|
95
|
-
|
|
96
|
-
const batcher = new Batcher<TValue>(fn, mergedOptions)
|
|
97
|
-
const state = injectSelector(batcher.store, selector)
|
|
98
|
-
|
|
99
|
-
const result = {
|
|
100
|
-
...batcher,
|
|
101
|
-
state,
|
|
102
|
-
} as AngularBatcher<TValue, TSelected>
|
|
103
|
-
|
|
104
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
105
|
-
destroyRef?.onDestroy(() => {
|
|
106
|
-
if (mergedOptions.onUnmount) {
|
|
107
|
-
mergedOptions.onUnmount(result)
|
|
108
|
-
} else {
|
|
109
|
-
batcher.cancel()
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
return result
|
|
114
|
-
}
|