@tanstack/angular-pacer 0.24.0 → 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 +4 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +2 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.js +1 -2
- package/dist/async-batcher/injectAsyncBatcher.d.ts +4 -6
- package/dist/async-batcher/injectAsyncBatcher.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +2 -4
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +4 -6
- package/dist/async-debouncer/injectAsyncDebouncer.js +1 -2
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +3 -5
- package/dist/async-queuer/injectAsyncQueuedSignal.js +1 -2
- package/dist/async-queuer/injectAsyncQueuer.d.ts +4 -6
- package/dist/async-queuer/injectAsyncQueuer.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +2 -4
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +4 -6
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +1 -2
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +2 -4
- package/dist/async-throttler/injectAsyncThrottledCallback.js +1 -2
- package/dist/async-throttler/injectAsyncThrottler.d.ts +4 -6
- package/dist/async-throttler/injectAsyncThrottler.js +1 -2
- package/dist/batcher/injectBatchedCallback.d.ts +2 -4
- package/dist/batcher/injectBatchedCallback.js +1 -2
- package/dist/batcher/injectBatcher.d.ts +4 -6
- package/dist/batcher/injectBatcher.js +1 -2
- package/dist/debouncer/injectDebouncedCallback.d.ts +2 -4
- package/dist/debouncer/injectDebouncedCallback.js +1 -2
- package/dist/debouncer/injectDebouncedSignal.d.ts +3 -5
- package/dist/debouncer/injectDebouncedSignal.js +1 -2
- package/dist/debouncer/injectDebouncedValue.d.ts +3 -5
- package/dist/debouncer/injectDebouncedValue.js +1 -2
- package/dist/debouncer/injectDebouncer.d.ts +4 -6
- package/dist/debouncer/injectDebouncer.js +1 -2
- package/dist/provider/pacer-context.d.ts +4 -6
- package/dist/provider/pacer-context.js +1 -2
- package/dist/provider/pacer-provider.d.ts +2 -4
- package/dist/provider/pacer-provider.js +1 -2
- package/dist/queuer/injectQueuedSignal.d.ts +3 -5
- package/dist/queuer/injectQueuedSignal.js +1 -2
- package/dist/queuer/injectQueuedValue.d.ts +3 -5
- package/dist/queuer/injectQueuedValue.js +1 -2
- package/dist/queuer/injectQueuer.d.ts +4 -6
- package/dist/queuer/injectQueuer.js +1 -2
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +2 -4
- package/dist/rate-limiter/injectRateLimitedCallback.js +1 -2
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +3 -5
- package/dist/rate-limiter/injectRateLimitedSignal.js +1 -2
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +3 -5
- package/dist/rate-limiter/injectRateLimitedValue.js +1 -2
- package/dist/rate-limiter/injectRateLimiter.d.ts +4 -6
- package/dist/rate-limiter/injectRateLimiter.js +1 -2
- package/dist/throttler/injectThrottledCallback.d.ts +2 -4
- package/dist/throttler/injectThrottledCallback.js +1 -2
- package/dist/throttler/injectThrottledSignal.d.ts +3 -7
- package/dist/throttler/injectThrottledSignal.js +1 -4
- package/dist/throttler/injectThrottledValue.d.ts +3 -7
- package/dist/throttler/injectThrottledValue.js +1 -2
- package/dist/throttler/injectThrottler.d.ts +4 -8
- package/dist/throttler/injectThrottler.js +1 -4
- package/package.json +24 -71
- 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 -44
- 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 -84
- 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 -42
- 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 -107
- 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 -49
- 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 -84
- 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 -45
- 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 -71
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +0 -1
- package/dist/async-retryer/index.cjs +0 -8
- 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 -43
- 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 -86
- 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 -42
- 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 -76
- 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 -42
- 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 -59
- 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 -75
- 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 -97
- 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 -20
- 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 -35
- 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 -43
- 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 -33
- 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 -81
- 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 -41
- 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 -53
- 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 -48
- 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 -102
- 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 -41
- 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 -60
- 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 -73
- 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 -97
- package/dist/throttler/injectThrottler.js.map +0 -1
- package/dist/types/index.cjs +0 -8
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -8
- 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 -125
- 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 -99
- 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 -98
- 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 -123
- 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 { AsyncBatcher } from '@tanstack/pacer/async-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 {
|
|
8
|
-
AsyncBatcherOptions,
|
|
9
|
-
AsyncBatcherState,
|
|
10
|
-
} from '@tanstack/pacer/async-batcher'
|
|
11
|
-
|
|
12
|
-
export interface AngularAsyncBatcherOptions<
|
|
13
|
-
TValue,
|
|
14
|
-
TSelected = {},
|
|
15
|
-
> extends AsyncBatcherOptions<TValue> {
|
|
16
|
-
/**
|
|
17
|
-
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
18
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
19
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (batcher: AngularAsyncBatcher<TValue, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
25
|
-
AsyncBatcher<TValue>,
|
|
26
|
-
'store'
|
|
27
|
-
> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the async batcher state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `batcher.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `batcher.state` instead of `batcher.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<AsyncBatcherState<TValue>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages an AsyncBatcher instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the AsyncBatcher's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* The AsyncBatcher collects items and processes them in batches asynchronously with support for
|
|
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 cancels any pending batch and aborts in-flight work when the component is destroyed.
|
|
63
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
64
|
-
*
|
|
65
|
-
* ```ts
|
|
66
|
-
* const batcher = injectAsyncBatcher(fn, {
|
|
67
|
-
* maxSize: 10,
|
|
68
|
-
* onUnmount: (b) => b.flush()
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```ts
|
|
76
|
-
* // Default behavior - no reactive state subscriptions
|
|
77
|
-
* const batcher = injectAsyncBatcher(
|
|
78
|
-
* async (items: Array<Data>) => {
|
|
79
|
-
* const response = await fetch('/api/batch', {
|
|
80
|
-
* method: 'POST',
|
|
81
|
-
* body: JSON.stringify(items)
|
|
82
|
-
* });
|
|
83
|
-
* return response.json();
|
|
84
|
-
* },
|
|
85
|
-
* { maxSize: 10, wait: 2000 }
|
|
86
|
-
* );
|
|
87
|
-
*
|
|
88
|
-
* // Add items
|
|
89
|
-
* batcher.addItem(data1);
|
|
90
|
-
* batcher.addItem(data2);
|
|
91
|
-
*
|
|
92
|
-
* // Access the selected state
|
|
93
|
-
* const { items, isExecuting } = batcher.state();
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
export function injectAsyncBatcher<TValue, TSelected = {}>(
|
|
97
|
-
fn: (items: Array<TValue>) => Promise<any>,
|
|
98
|
-
options: AngularAsyncBatcherOptions<TValue, TSelected> = {},
|
|
99
|
-
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
100
|
-
({}) as TSelected,
|
|
101
|
-
): AngularAsyncBatcher<TValue, TSelected> {
|
|
102
|
-
const mergedOptions = {
|
|
103
|
-
...injectPacerOptions().asyncBatcher,
|
|
104
|
-
...options,
|
|
105
|
-
} as AngularAsyncBatcherOptions<TValue, TSelected>
|
|
106
|
-
|
|
107
|
-
const batcher = new AsyncBatcher<TValue>(fn, mergedOptions)
|
|
108
|
-
const state = injectSelector(batcher.store, selector)
|
|
109
|
-
|
|
110
|
-
const result = {
|
|
111
|
-
...batcher,
|
|
112
|
-
state,
|
|
113
|
-
} as AngularAsyncBatcher<TValue, TSelected>
|
|
114
|
-
|
|
115
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
116
|
-
destroyRef?.onDestroy(() => {
|
|
117
|
-
if (mergedOptions.onUnmount) {
|
|
118
|
-
mergedOptions.onUnmount(result)
|
|
119
|
-
} else {
|
|
120
|
-
batcher.cancel()
|
|
121
|
-
batcher.abort()
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
return result
|
|
126
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { injectAsyncDebouncer } from './injectAsyncDebouncer'
|
|
2
|
-
import type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'
|
|
3
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates an async debounced version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectAsyncDebouncer` that provides
|
|
8
|
-
* a simplified API for basic async debouncing needs.
|
|
9
|
-
*
|
|
10
|
-
* The debounced function will only execute after the specified wait time has elapsed
|
|
11
|
-
* since its last invocation. If called again before the wait time expires, the timer
|
|
12
|
-
* resets and starts waiting again.
|
|
13
|
-
*
|
|
14
|
-
* This function provides a simpler API compared to `injectAsyncDebouncer`, making it ideal for basic
|
|
15
|
-
* async debouncing needs. However, it does not expose the underlying AsyncDebouncer instance.
|
|
16
|
-
*
|
|
17
|
-
* For advanced usage requiring features like:
|
|
18
|
-
* - Manual cancellation
|
|
19
|
-
* - Access to execution counts
|
|
20
|
-
* - Error handling callbacks
|
|
21
|
-
* - Retry support
|
|
22
|
-
*
|
|
23
|
-
* Consider using the `injectAsyncDebouncer` function instead.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* // Debounce an async search handler
|
|
28
|
-
* const handleSearch = injectAsyncDebouncedCallback(
|
|
29
|
-
* async (query: string) => {
|
|
30
|
-
* const response = await fetch(`/api/search?q=${query}`);
|
|
31
|
-
* return response.json();
|
|
32
|
-
* },
|
|
33
|
-
* { wait: 500 }
|
|
34
|
-
* );
|
|
35
|
-
*
|
|
36
|
-
* // Use in an input
|
|
37
|
-
* const results = await handleSearch(searchQuery);
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function injectAsyncDebouncedCallback<TFn extends AnyAsyncFunction>(
|
|
41
|
-
fn: TFn,
|
|
42
|
-
options: AsyncDebouncerOptions<TFn>,
|
|
43
|
-
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>> | undefined> {
|
|
44
|
-
const debouncer = injectAsyncDebouncer(fn, options)
|
|
45
|
-
return async (...args: Parameters<TFn>) => {
|
|
46
|
-
const result = await debouncer.maybeExecute(...args)
|
|
47
|
-
// Not sure if this is the best way to handle this,
|
|
48
|
-
return result !== undefined ? await result : undefined
|
|
49
|
-
}
|
|
50
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
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
|
-
AsyncDebouncerOptions,
|
|
10
|
-
AsyncDebouncerState,
|
|
11
|
-
} from '@tanstack/pacer/async-debouncer'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncDebouncerOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncDebouncerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
21
|
-
*/
|
|
22
|
-
onUnmount?: (debouncer: AngularAsyncDebouncer<TFn, TSelected>) => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface AngularAsyncDebouncer<
|
|
26
|
-
TFn extends AnyAsyncFunction,
|
|
27
|
-
TSelected = {},
|
|
28
|
-
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
29
|
-
/**
|
|
30
|
-
* Reactive state signal that will be updated when the async debouncer state changes
|
|
31
|
-
*
|
|
32
|
-
* Use this instead of `debouncer.store.state`
|
|
33
|
-
*/
|
|
34
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use `debouncer.state` instead of `debouncer.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<AsyncDebouncerState<TFn>>>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* An Angular function that creates and manages an AsyncDebouncer instance.
|
|
44
|
-
*
|
|
45
|
-
* This is a lower-level function that provides direct access to the AsyncDebouncer's functionality.
|
|
46
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
47
|
-
*
|
|
48
|
-
* This function provides async debouncing functionality with promise support, error handling,
|
|
49
|
-
* retry capabilities, and abort support.
|
|
50
|
-
*
|
|
51
|
-
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
52
|
-
* since the last call. If the function is called again before the wait time expires, the
|
|
53
|
-
* timer resets and starts waiting again.
|
|
54
|
-
*
|
|
55
|
-
* ## State Management and Selector
|
|
56
|
-
*
|
|
57
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
58
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
59
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
60
|
-
*
|
|
61
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
62
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
63
|
-
* full control over when your component tracks state changes.
|
|
64
|
-
*
|
|
65
|
-
* Available state properties:
|
|
66
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
67
|
-
* - `errorCount`: Number of function executions that have resulted in errors
|
|
68
|
-
* - `isExecuting`: Whether the debounced function is currently executing asynchronously
|
|
69
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
70
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
71
|
-
* - `lastResult`: The result from the most recent successful function execution
|
|
72
|
-
* - `settleCount`: Number of function executions that have completed (either successfully or with errors)
|
|
73
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing' | 'settled')
|
|
74
|
-
* - `successCount`: Number of function executions that have completed successfully
|
|
75
|
-
*
|
|
76
|
-
* ## Cleanup on Destroy
|
|
77
|
-
*
|
|
78
|
-
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
79
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* const debouncer = injectAsyncDebouncer(fn, {
|
|
83
|
-
* wait: 500,
|
|
84
|
-
* onUnmount: (d) => d.flush()
|
|
85
|
-
* });
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* // Default behavior - no reactive state subscriptions
|
|
93
|
-
* const debouncer = injectAsyncDebouncer(
|
|
94
|
-
* async (query: string) => {
|
|
95
|
-
* const response = await fetch(`/api/search?q=${query}`);
|
|
96
|
-
* return response.json();
|
|
97
|
-
* },
|
|
98
|
-
* { wait: 500 }
|
|
99
|
-
* );
|
|
100
|
-
*
|
|
101
|
-
* // Opt-in to track isExecuting changes (optimized for loading states)
|
|
102
|
-
* const debouncer = injectAsyncDebouncer(
|
|
103
|
-
* async (query: string) => fetchSearchResults(query),
|
|
104
|
-
* { wait: 500 },
|
|
105
|
-
* (state) => ({ isExecuting: state.isExecuting, isPending: state.isPending })
|
|
106
|
-
* );
|
|
107
|
-
*
|
|
108
|
-
* // In an event handler
|
|
109
|
-
* const handleChange = async (e: Event) => {
|
|
110
|
-
* const target = e.target as HTMLInputElement;
|
|
111
|
-
* const result = await debouncer.maybeExecute(target.value);
|
|
112
|
-
* console.log('Search results:', result);
|
|
113
|
-
* };
|
|
114
|
-
*
|
|
115
|
-
* // Access the selected state
|
|
116
|
-
* const { isExecuting, errorCount } = debouncer.state();
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export function injectAsyncDebouncer<
|
|
120
|
-
TFn extends AnyAsyncFunction,
|
|
121
|
-
TSelected = {},
|
|
122
|
-
>(
|
|
123
|
-
fn: TFn,
|
|
124
|
-
options: AngularAsyncDebouncerOptions<TFn, TSelected>,
|
|
125
|
-
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
126
|
-
({}) as TSelected,
|
|
127
|
-
): AngularAsyncDebouncer<TFn, TSelected> {
|
|
128
|
-
const mergedOptions = {
|
|
129
|
-
...injectPacerOptions().asyncDebouncer,
|
|
130
|
-
...options,
|
|
131
|
-
} as AngularAsyncDebouncerOptions<TFn, TSelected>
|
|
132
|
-
|
|
133
|
-
const debouncer = new AsyncDebouncer<TFn>(fn, mergedOptions)
|
|
134
|
-
const state = injectSelector(debouncer.store, selector)
|
|
135
|
-
|
|
136
|
-
const result = {
|
|
137
|
-
...debouncer,
|
|
138
|
-
state,
|
|
139
|
-
} as AngularAsyncDebouncer<TFn, TSelected>
|
|
140
|
-
|
|
141
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
142
|
-
destroyRef?.onDestroy(() => {
|
|
143
|
-
if (mergedOptions.onUnmount) {
|
|
144
|
-
mergedOptions.onUnmount(result)
|
|
145
|
-
} else {
|
|
146
|
-
debouncer.cancel()
|
|
147
|
-
debouncer.abort()
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
return result
|
|
152
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { computed } from '@angular/core'
|
|
2
|
-
import { injectAsyncQueuer } from './injectAsyncQueuer'
|
|
3
|
-
import type { AngularAsyncQueuer } from './injectAsyncQueuer'
|
|
4
|
-
import type {
|
|
5
|
-
AsyncQueuerOptions,
|
|
6
|
-
AsyncQueuerState,
|
|
7
|
-
} from '@tanstack/pacer/async-queuer'
|
|
8
|
-
|
|
9
|
-
export interface AsyncQueuedSignal<TValue, TSelected = {}> {
|
|
10
|
-
(): Array<TValue>
|
|
11
|
-
addItem: AngularAsyncQueuer<TValue, TSelected>['addItem']
|
|
12
|
-
queuer: AngularAsyncQueuer<TValue, TSelected>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* An Angular function that creates an async queuer with managed state, combining Angular's signals with async queuing functionality.
|
|
17
|
-
* This function provides both the current queue state and queue control methods.
|
|
18
|
-
*
|
|
19
|
-
* The queue state is automatically updated whenever items are added, removed, or processed in the queue.
|
|
20
|
-
* All queue operations are reflected in the state array returned by the function.
|
|
21
|
-
*
|
|
22
|
-
* The function returns a callable object:
|
|
23
|
-
* - `queued()`: Get the current queue items as an array
|
|
24
|
-
* - `queued.addItem(...)`: Add an item to the queue
|
|
25
|
-
* - `queued.queue`: The queuer instance with additional control methods
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* // Default behavior - track items
|
|
30
|
-
* const queued = injectAsyncQueuedSignal(
|
|
31
|
-
* async (item) => {
|
|
32
|
-
* const response = await fetch('/api/process', {
|
|
33
|
-
* method: 'POST',
|
|
34
|
-
* body: JSON.stringify(item)
|
|
35
|
-
* });
|
|
36
|
-
* return response.json();
|
|
37
|
-
* },
|
|
38
|
-
* { concurrency: 2, wait: 1000 }
|
|
39
|
-
* );
|
|
40
|
-
*
|
|
41
|
-
* // Add items
|
|
42
|
-
* queued.addItem(data1);
|
|
43
|
-
*
|
|
44
|
-
* // Access items
|
|
45
|
-
* console.log(queued()); // [data1, ...]
|
|
46
|
-
*
|
|
47
|
-
* // Control the queue
|
|
48
|
-
* queued.queuer.start();
|
|
49
|
-
* queued.queuer.stop();
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function injectAsyncQueuedSignal<
|
|
53
|
-
TValue,
|
|
54
|
-
TSelected extends Pick<AsyncQueuerState<TValue>, 'items'> = Pick<
|
|
55
|
-
AsyncQueuerState<TValue>,
|
|
56
|
-
'items'
|
|
57
|
-
>,
|
|
58
|
-
>(
|
|
59
|
-
fn: (value: TValue) => Promise<any>,
|
|
60
|
-
options: AsyncQueuerOptions<TValue> = {},
|
|
61
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected = (state) =>
|
|
62
|
-
({ items: state.items }) as TSelected,
|
|
63
|
-
): AsyncQueuedSignal<TValue, TSelected> {
|
|
64
|
-
const queuer = injectAsyncQueuer(fn, options, selector)
|
|
65
|
-
|
|
66
|
-
const items = computed(() => queuer.state().items as Array<TValue>)
|
|
67
|
-
|
|
68
|
-
const queued = Object.assign(items, {
|
|
69
|
-
addItem: queuer.addItem.bind(queuer),
|
|
70
|
-
queuer,
|
|
71
|
-
}) as AsyncQueuedSignal<TValue, TSelected>
|
|
72
|
-
|
|
73
|
-
return queued
|
|
74
|
-
}
|
|
@@ -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
|
-
}
|