@tanstack/angular-pacer 0.20.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 +165 -0
- package/dist/async-batcher/index.cjs +13 -0
- package/dist/async-batcher/index.d.cts +4 -0
- package/dist/async-batcher/index.d.ts +4 -0
- package/dist/async-batcher/index.js +6 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs +49 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs.map +1 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.d.cts +45 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +45 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.js +49 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.js.map +1 -0
- package/dist/async-batcher/injectAsyncBatcher.cjs +61 -0
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +1 -0
- package/dist/async-batcher/injectAsyncBatcher.d.cts +63 -0
- package/dist/async-batcher/injectAsyncBatcher.d.ts +63 -0
- package/dist/async-batcher/injectAsyncBatcher.js +61 -0
- package/dist/async-batcher/injectAsyncBatcher.js.map +1 -0
- package/dist/async-debouncer/index.cjs +13 -0
- package/dist/async-debouncer/index.d.cts +4 -0
- package/dist/async-debouncer/index.d.ts +4 -0
- package/dist/async-debouncer/index.js +6 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs +49 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.cts +43 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +43 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +49 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +83 -0
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +86 -0
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +86 -0
- package/dist/async-debouncer/injectAsyncDebouncer.js +83 -0
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +1 -0
- package/dist/async-queuer/index.cjs +13 -0
- package/dist/async-queuer/index.d.cts +4 -0
- package/dist/async-queuer/index.d.ts +4 -0
- package/dist/async-queuer/index.js +6 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs +53 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +1 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +55 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +55 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.js +53 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +1 -0
- package/dist/async-queuer/injectAsyncQueuer.cjs +61 -0
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +1 -0
- package/dist/async-queuer/injectAsyncQueuer.d.cts +63 -0
- package/dist/async-queuer/injectAsyncQueuer.d.ts +63 -0
- package/dist/async-queuer/injectAsyncQueuer.js +61 -0
- package/dist/async-queuer/injectAsyncQueuer.js.map +1 -0
- package/dist/async-rate-limiter/index.cjs +13 -0
- package/dist/async-rate-limiter/index.d.cts +4 -0
- package/dist/async-rate-limiter/index.d.ts +4 -0
- package/dist/async-rate-limiter/index.js +6 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs +52 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.cts +46 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +46 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +52 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +57 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +60 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +60 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +57 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +1 -0
- package/dist/async-retryer/index.cjs +9 -0
- package/dist/async-retryer/index.d.cts +1 -0
- package/dist/async-retryer/index.d.ts +1 -0
- package/dist/async-retryer/index.js +3 -0
- package/dist/async-throttler/index.cjs +13 -0
- package/dist/async-throttler/index.d.cts +4 -0
- package/dist/async-throttler/index.d.ts +4 -0
- package/dist/async-throttler/index.js +6 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs +50 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs.map +1 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.d.cts +44 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +44 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.js +50 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.js.map +1 -0
- package/dist/async-throttler/injectAsyncThrottler.cjs +62 -0
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +1 -0
- package/dist/async-throttler/injectAsyncThrottler.d.cts +65 -0
- package/dist/async-throttler/injectAsyncThrottler.d.ts +65 -0
- package/dist/async-throttler/injectAsyncThrottler.js +62 -0
- package/dist/async-throttler/injectAsyncThrottler.js.map +1 -0
- package/dist/batcher/index.cjs +13 -0
- package/dist/batcher/index.d.cts +4 -0
- package/dist/batcher/index.d.ts +4 -0
- package/dist/batcher/index.js +6 -0
- package/dist/batcher/injectBatchedCallback.cjs +47 -0
- package/dist/batcher/injectBatchedCallback.cjs.map +1 -0
- package/dist/batcher/injectBatchedCallback.d.cts +43 -0
- package/dist/batcher/injectBatchedCallback.d.ts +43 -0
- package/dist/batcher/injectBatchedCallback.js +47 -0
- package/dist/batcher/injectBatchedCallback.js.map +1 -0
- package/dist/batcher/injectBatcher.cjs +56 -0
- package/dist/batcher/injectBatcher.cjs.map +1 -0
- package/dist/batcher/injectBatcher.d.cts +58 -0
- package/dist/batcher/injectBatcher.d.ts +58 -0
- package/dist/batcher/injectBatcher.js +56 -0
- package/dist/batcher/injectBatcher.js.map +1 -0
- package/dist/debouncer/index.cjs +17 -0
- package/dist/debouncer/index.d.cts +6 -0
- package/dist/debouncer/index.d.ts +6 -0
- package/dist/debouncer/index.js +8 -0
- package/dist/debouncer/injectDebouncedCallback.cjs +46 -0
- package/dist/debouncer/injectDebouncedCallback.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedCallback.d.cts +43 -0
- package/dist/debouncer/injectDebouncedCallback.d.ts +43 -0
- package/dist/debouncer/injectDebouncedCallback.js +46 -0
- package/dist/debouncer/injectDebouncedCallback.js.map +1 -0
- package/dist/debouncer/injectDebouncedSignal.cjs +68 -0
- package/dist/debouncer/injectDebouncedSignal.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedSignal.d.cts +65 -0
- package/dist/debouncer/injectDebouncedSignal.d.ts +65 -0
- package/dist/debouncer/injectDebouncedSignal.js +68 -0
- package/dist/debouncer/injectDebouncedSignal.js.map +1 -0
- package/dist/debouncer/injectDebouncedValue.cjs +17 -0
- package/dist/debouncer/injectDebouncedValue.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedValue.d.cts +76 -0
- package/dist/debouncer/injectDebouncedValue.d.ts +76 -0
- package/dist/debouncer/injectDebouncedValue.js +17 -0
- package/dist/debouncer/injectDebouncedValue.js.map +1 -0
- package/dist/debouncer/injectDebouncer.cjs +76 -0
- package/dist/debouncer/injectDebouncer.cjs.map +1 -0
- package/dist/debouncer/injectDebouncer.d.cts +79 -0
- package/dist/debouncer/injectDebouncer.d.ts +79 -0
- package/dist/debouncer/injectDebouncer.js +76 -0
- package/dist/debouncer/injectDebouncer.js.map +1 -0
- package/dist/index.cjs +65 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +32 -0
- package/dist/provider/index.cjs +7 -0
- package/dist/provider/index.d.cts +3 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.js +4 -0
- package/dist/provider/pacer-context.cjs +20 -0
- package/dist/provider/pacer-context.cjs.map +1 -0
- package/dist/provider/pacer-context.d.cts +21 -0
- package/dist/provider/pacer-context.d.ts +21 -0
- package/dist/provider/pacer-context.js +19 -0
- package/dist/provider/pacer-context.js.map +1 -0
- package/dist/provider/pacer-provider.cjs +41 -0
- package/dist/provider/pacer-provider.cjs.map +1 -0
- package/dist/provider/pacer-provider.d.cts +36 -0
- package/dist/provider/pacer-provider.d.ts +36 -0
- package/dist/provider/pacer-provider.js +41 -0
- package/dist/provider/pacer-provider.js.map +1 -0
- package/dist/queuer/index.cjs +15 -0
- package/dist/queuer/index.d.cts +5 -0
- package/dist/queuer/index.d.ts +5 -0
- package/dist/queuer/index.js +7 -0
- package/dist/queuer/injectQueuedSignal.cjs +47 -0
- package/dist/queuer/injectQueuedSignal.cjs.map +1 -0
- package/dist/queuer/injectQueuedSignal.d.cts +49 -0
- package/dist/queuer/injectQueuedSignal.d.ts +49 -0
- package/dist/queuer/injectQueuedSignal.js +47 -0
- package/dist/queuer/injectQueuedSignal.js.map +1 -0
- package/dist/queuer/injectQueuedValue.cjs +23 -0
- package/dist/queuer/injectQueuedValue.cjs.map +1 -0
- package/dist/queuer/injectQueuedValue.d.cts +34 -0
- package/dist/queuer/injectQueuedValue.d.ts +34 -0
- package/dist/queuer/injectQueuedValue.js +23 -0
- package/dist/queuer/injectQueuedValue.js.map +1 -0
- package/dist/queuer/injectQueuer.cjs +61 -0
- package/dist/queuer/injectQueuer.cjs.map +1 -0
- package/dist/queuer/injectQueuer.d.cts +63 -0
- package/dist/queuer/injectQueuer.d.ts +63 -0
- package/dist/queuer/injectQueuer.js +61 -0
- package/dist/queuer/injectQueuer.js.map +1 -0
- package/dist/rate-limiter/index.cjs +17 -0
- package/dist/rate-limiter/index.d.cts +6 -0
- package/dist/rate-limiter/index.d.ts +6 -0
- package/dist/rate-limiter/index.js +8 -0
- package/dist/rate-limiter/injectRateLimitedCallback.cjs +45 -0
- package/dist/rate-limiter/injectRateLimitedCallback.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedCallback.d.cts +42 -0
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +42 -0
- package/dist/rate-limiter/injectRateLimitedCallback.js +45 -0
- package/dist/rate-limiter/injectRateLimitedCallback.js.map +1 -0
- package/dist/rate-limiter/injectRateLimitedSignal.cjs +62 -0
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +59 -0
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +59 -0
- package/dist/rate-limiter/injectRateLimitedSignal.js +62 -0
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +1 -0
- package/dist/rate-limiter/injectRateLimitedValue.cjs +17 -0
- package/dist/rate-limiter/injectRateLimitedValue.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedValue.d.cts +49 -0
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +49 -0
- package/dist/rate-limiter/injectRateLimitedValue.js +17 -0
- package/dist/rate-limiter/injectRateLimitedValue.js.map +1 -0
- package/dist/rate-limiter/injectRateLimiter.cjs +90 -0
- package/dist/rate-limiter/injectRateLimiter.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimiter.d.cts +93 -0
- package/dist/rate-limiter/injectRateLimiter.d.ts +93 -0
- package/dist/rate-limiter/injectRateLimiter.js +90 -0
- package/dist/rate-limiter/injectRateLimiter.js.map +1 -0
- package/dist/throttler/index.cjs +17 -0
- package/dist/throttler/index.d.cts +6 -0
- package/dist/throttler/index.d.ts +6 -0
- package/dist/throttler/index.js +8 -0
- package/dist/throttler/injectThrottledCallback.cjs +45 -0
- package/dist/throttler/injectThrottledCallback.cjs.map +1 -0
- package/dist/throttler/injectThrottledCallback.d.cts +42 -0
- package/dist/throttler/injectThrottledCallback.d.ts +42 -0
- package/dist/throttler/injectThrottledCallback.js +45 -0
- package/dist/throttler/injectThrottledCallback.js.map +1 -0
- package/dist/throttler/injectThrottledSignal.cjs +69 -0
- package/dist/throttler/injectThrottledSignal.cjs.map +1 -0
- package/dist/throttler/injectThrottledSignal.d.cts +66 -0
- package/dist/throttler/injectThrottledSignal.d.ts +66 -0
- package/dist/throttler/injectThrottledSignal.js +69 -0
- package/dist/throttler/injectThrottledSignal.js.map +1 -0
- package/dist/throttler/injectThrottledValue.cjs +17 -0
- package/dist/throttler/injectThrottledValue.cjs.map +1 -0
- package/dist/throttler/injectThrottledValue.d.cts +74 -0
- package/dist/throttler/injectThrottledValue.d.ts +74 -0
- package/dist/throttler/injectThrottledValue.js +17 -0
- package/dist/throttler/injectThrottledValue.js.map +1 -0
- package/dist/throttler/injectThrottler.cjs +76 -0
- package/dist/throttler/injectThrottler.cjs.map +1 -0
- package/dist/throttler/injectThrottler.d.cts +79 -0
- package/dist/throttler/injectThrottler.d.ts +79 -0
- package/dist/throttler/injectThrottler.js +76 -0
- package/dist/throttler/injectThrottler.js.map +1 -0
- package/dist/types/index.cjs +9 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/index.cjs +9 -0
- package/dist/utils/index.d.cts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +121 -0
- package/src/async-batcher/index.ts +4 -0
- package/src/async-batcher/injectAsyncBatchedCallback.ts +48 -0
- package/src/async-batcher/injectAsyncBatcher.ts +87 -0
- package/src/async-debouncer/index.ts +4 -0
- package/src/async-debouncer/injectAsyncDebouncedCallback.ts +50 -0
- package/src/async-debouncer/injectAsyncDebouncer.ts +113 -0
- package/src/async-queuer/index.ts +4 -0
- package/src/async-queuer/injectAsyncQueuedSignal.ts +82 -0
- package/src/async-queuer/injectAsyncQueuer.ts +87 -0
- package/src/async-rate-limiter/index.ts +4 -0
- package/src/async-rate-limiter/injectAsyncRateLimitedCallback.ts +52 -0
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +87 -0
- package/src/async-retryer/index.ts +1 -0
- package/src/async-throttler/index.ts +4 -0
- package/src/async-throttler/injectAsyncThrottledCallback.ts +50 -0
- package/src/async-throttler/injectAsyncThrottler.ts +92 -0
- package/src/batcher/index.ts +4 -0
- package/src/batcher/injectBatchedCallback.ts +46 -0
- package/src/batcher/injectBatcher.ts +79 -0
- package/src/debouncer/index.ts +6 -0
- package/src/debouncer/injectDebouncedCallback.ts +46 -0
- package/src/debouncer/injectDebouncedSignal.ts +101 -0
- package/src/debouncer/injectDebouncedValue.ts +126 -0
- package/src/debouncer/injectDebouncer.ts +102 -0
- package/src/index.ts +40 -0
- package/src/provider/index.ts +2 -0
- package/src/provider/pacer-context.ts +46 -0
- package/src/provider/pacer-provider.ts +38 -0
- package/src/queuer/index.ts +5 -0
- package/src/queuer/injectQueuedSignal.ts +70 -0
- package/src/queuer/injectQueuedValue.ts +101 -0
- package/src/queuer/injectQueuer.ts +83 -0
- package/src/rate-limiter/index.ts +6 -0
- package/src/rate-limiter/injectRateLimitedCallback.ts +45 -0
- package/src/rate-limiter/injectRateLimitedSignal.ts +95 -0
- package/src/rate-limiter/injectRateLimitedValue.ts +99 -0
- package/src/rate-limiter/injectRateLimiter.ts +116 -0
- package/src/throttler/index.ts +6 -0
- package/src/throttler/injectThrottledCallback.ts +45 -0
- package/src/throttler/injectThrottledSignal.ts +102 -0
- package/src/throttler/injectThrottledValue.ts +124 -0
- package/src/throttler/injectThrottler.ts +102 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
|
+
import type {
|
|
8
|
+
AsyncDebouncerOptions,
|
|
9
|
+
AsyncDebouncerState,
|
|
10
|
+
} from '@tanstack/pacer/async-debouncer'
|
|
11
|
+
|
|
12
|
+
export interface AngularAsyncDebouncer<
|
|
13
|
+
TFn extends AnyAsyncFunction,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* Reactive state signal that will be updated when the async debouncer state changes
|
|
18
|
+
*
|
|
19
|
+
* Use this instead of `debouncer.store.state`
|
|
20
|
+
*/
|
|
21
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
24
|
+
* The state on the store object is not reactive in Angular signals.
|
|
25
|
+
*/
|
|
26
|
+
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An Angular function that creates and manages an AsyncDebouncer instance.
|
|
31
|
+
*
|
|
32
|
+
* This is a lower-level function that provides direct access to the AsyncDebouncer's functionality.
|
|
33
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
34
|
+
*
|
|
35
|
+
* This function provides async debouncing functionality with promise support, error handling,
|
|
36
|
+
* retry capabilities, and abort support.
|
|
37
|
+
*
|
|
38
|
+
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
39
|
+
* since the last call. If the function is called again before the wait time expires, the
|
|
40
|
+
* timer resets and starts waiting again.
|
|
41
|
+
*
|
|
42
|
+
* ## State Management and Selector
|
|
43
|
+
*
|
|
44
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
45
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
46
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
47
|
+
*
|
|
48
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
49
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
50
|
+
* full control over when your component tracks state changes.
|
|
51
|
+
*
|
|
52
|
+
* Available state properties:
|
|
53
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
54
|
+
* - `errorCount`: Number of function executions that have resulted in errors
|
|
55
|
+
* - `isExecuting`: Whether the debounced function is currently executing asynchronously
|
|
56
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
57
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
58
|
+
* - `lastResult`: The result from the most recent successful function execution
|
|
59
|
+
* - `settleCount`: Number of function executions that have completed (either successfully or with errors)
|
|
60
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing' | 'settled')
|
|
61
|
+
* - `successCount`: Number of function executions that have completed successfully
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* // Default behavior - no reactive state subscriptions
|
|
66
|
+
* const debouncer = injectAsyncDebouncer(
|
|
67
|
+
* async (query: string) => {
|
|
68
|
+
* const response = await fetch(`/api/search?q=${query}`);
|
|
69
|
+
* return response.json();
|
|
70
|
+
* },
|
|
71
|
+
* { wait: 500 }
|
|
72
|
+
* );
|
|
73
|
+
*
|
|
74
|
+
* // Opt-in to track isExecuting changes (optimized for loading states)
|
|
75
|
+
* const debouncer = injectAsyncDebouncer(
|
|
76
|
+
* async (query: string) => fetchSearchResults(query),
|
|
77
|
+
* { wait: 500 },
|
|
78
|
+
* (state) => ({ isExecuting: state.isExecuting, isPending: state.isPending })
|
|
79
|
+
* );
|
|
80
|
+
*
|
|
81
|
+
* // In an event handler
|
|
82
|
+
* const handleChange = async (e: Event) => {
|
|
83
|
+
* const target = e.target as HTMLInputElement;
|
|
84
|
+
* const result = await debouncer.maybeExecute(target.value);
|
|
85
|
+
* console.log('Search results:', result);
|
|
86
|
+
* };
|
|
87
|
+
*
|
|
88
|
+
* // Access the selected state
|
|
89
|
+
* const { isExecuting, errorCount } = debouncer.state();
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
export function injectAsyncDebouncer<
|
|
93
|
+
TFn extends AnyAsyncFunction,
|
|
94
|
+
TSelected = {},
|
|
95
|
+
>(
|
|
96
|
+
fn: TFn,
|
|
97
|
+
options: AsyncDebouncerOptions<TFn>,
|
|
98
|
+
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
99
|
+
({}) as TSelected,
|
|
100
|
+
): AngularAsyncDebouncer<TFn, TSelected> {
|
|
101
|
+
const mergedOptions = {
|
|
102
|
+
...injectPacerOptions().asyncDebouncer,
|
|
103
|
+
...options,
|
|
104
|
+
} as AsyncDebouncerOptions<TFn>
|
|
105
|
+
|
|
106
|
+
const debouncer = new AsyncDebouncer<TFn>(fn, mergedOptions)
|
|
107
|
+
const state = injectStore(debouncer.store, selector)
|
|
108
|
+
|
|
109
|
+
return {
|
|
110
|
+
...debouncer,
|
|
111
|
+
state,
|
|
112
|
+
} as AngularAsyncDebouncer<TFn, TSelected>
|
|
113
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
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 type AsyncQueuedSignal<
|
|
10
|
+
TValue,
|
|
11
|
+
TSelected = {},
|
|
12
|
+
> = (() => Array<TValue>) & {
|
|
13
|
+
/**
|
|
14
|
+
* Add an item to the queue.
|
|
15
|
+
*/
|
|
16
|
+
readonly addItem: AngularAsyncQueuer<TValue, TSelected>['addItem']
|
|
17
|
+
/**
|
|
18
|
+
* The queuer instance with additional control methods and state signals.
|
|
19
|
+
*/
|
|
20
|
+
readonly queuer: AngularAsyncQueuer<TValue, TSelected>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An Angular function that creates an async queuer with managed state, combining Angular's signals with async queuing functionality.
|
|
25
|
+
* This function provides both the current queue state and queue control methods.
|
|
26
|
+
*
|
|
27
|
+
* The queue state is automatically updated whenever items are added, removed, or processed in the queue.
|
|
28
|
+
* All queue operations are reflected in the state array returned by the function.
|
|
29
|
+
*
|
|
30
|
+
* The function returns a callable object:
|
|
31
|
+
* - `queued()`: Get the current queue items as an array
|
|
32
|
+
* - `queued.addItem(...)`: Add an item to the queue
|
|
33
|
+
* - `queued.queue`: The queuer instance with additional control methods
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* // Default behavior - track items
|
|
38
|
+
* const queued = injectAsyncQueuedSignal(
|
|
39
|
+
* async (item) => {
|
|
40
|
+
* const response = await fetch('/api/process', {
|
|
41
|
+
* method: 'POST',
|
|
42
|
+
* body: JSON.stringify(item)
|
|
43
|
+
* });
|
|
44
|
+
* return response.json();
|
|
45
|
+
* },
|
|
46
|
+
* { concurrency: 2, wait: 1000 }
|
|
47
|
+
* );
|
|
48
|
+
*
|
|
49
|
+
* // Add items
|
|
50
|
+
* queued.addItem(data1);
|
|
51
|
+
*
|
|
52
|
+
* // Access items
|
|
53
|
+
* console.log(queued()); // [data1, ...]
|
|
54
|
+
*
|
|
55
|
+
* // Control the queue
|
|
56
|
+
* queued.queuer.start();
|
|
57
|
+
* queued.queuer.stop();
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function injectAsyncQueuedSignal<
|
|
61
|
+
TValue,
|
|
62
|
+
TSelected extends Pick<AsyncQueuerState<TValue>, 'items'> = Pick<
|
|
63
|
+
AsyncQueuerState<TValue>,
|
|
64
|
+
'items'
|
|
65
|
+
>,
|
|
66
|
+
>(
|
|
67
|
+
fn: (value: TValue) => Promise<any>,
|
|
68
|
+
options: AsyncQueuerOptions<TValue> = {},
|
|
69
|
+
selector: (state: AsyncQueuerState<TValue>) => TSelected = (state) =>
|
|
70
|
+
({ items: state.items }) as TSelected,
|
|
71
|
+
): AsyncQueuedSignal<TValue, TSelected> {
|
|
72
|
+
const queuer = injectAsyncQueuer(fn, options, selector)
|
|
73
|
+
|
|
74
|
+
const items = computed(() => queuer.state().items as Array<TValue>)
|
|
75
|
+
|
|
76
|
+
const queued = Object.assign(items, {
|
|
77
|
+
addItem: queuer.addItem.bind(queuer),
|
|
78
|
+
queuer,
|
|
79
|
+
}) as AsyncQueuedSignal<TValue, TSelected>
|
|
80
|
+
|
|
81
|
+
return queued
|
|
82
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { AsyncQueuer } from '@tanstack/pacer/async-queuer'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type {
|
|
7
|
+
AsyncQueuerOptions,
|
|
8
|
+
AsyncQueuerState,
|
|
9
|
+
} from '@tanstack/pacer/async-queuer'
|
|
10
|
+
|
|
11
|
+
export interface AngularAsyncQueuer<TValue, TSelected = {}> extends Omit<
|
|
12
|
+
AsyncQueuer<TValue>,
|
|
13
|
+
'store'
|
|
14
|
+
> {
|
|
15
|
+
/**
|
|
16
|
+
* Reactive state signal that will be updated when the async queuer state changes
|
|
17
|
+
*
|
|
18
|
+
* Use this instead of `queuer.store.state`
|
|
19
|
+
*/
|
|
20
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
23
|
+
* The state on the store object is not reactive in Angular signals.
|
|
24
|
+
*/
|
|
25
|
+
readonly store: Store<Readonly<AsyncQueuerState<TValue>>>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An Angular function that creates and manages an AsyncQueuer instance.
|
|
30
|
+
*
|
|
31
|
+
* This is a lower-level function that provides direct access to the AsyncQueuer's functionality.
|
|
32
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
33
|
+
*
|
|
34
|
+
* The AsyncQueuer processes items asynchronously with support for concurrent execution,
|
|
35
|
+
* promise-based processing, error handling, retry capabilities, and abort support.
|
|
36
|
+
*
|
|
37
|
+
* ## State Management and Selector
|
|
38
|
+
*
|
|
39
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
40
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
41
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
42
|
+
*
|
|
43
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
44
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
45
|
+
* full control over when your component tracks state changes.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Default behavior - no reactive state subscriptions
|
|
50
|
+
* const queuer = injectAsyncQueuer(
|
|
51
|
+
* async (item: Data) => {
|
|
52
|
+
* const response = await fetch('/api/process', {
|
|
53
|
+
* method: 'POST',
|
|
54
|
+
* body: JSON.stringify(item)
|
|
55
|
+
* });
|
|
56
|
+
* return response.json();
|
|
57
|
+
* },
|
|
58
|
+
* { concurrency: 2, wait: 1000 }
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Add items
|
|
62
|
+
* queuer.addItem(data1);
|
|
63
|
+
* queuer.addItem(data2);
|
|
64
|
+
*
|
|
65
|
+
* // Access the selected state
|
|
66
|
+
* const { items, isExecuting } = queuer.state();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function injectAsyncQueuer<TValue, TSelected = {}>(
|
|
70
|
+
fn: (value: TValue) => Promise<any>,
|
|
71
|
+
options: AsyncQueuerOptions<TValue> = {},
|
|
72
|
+
selector: (state: AsyncQueuerState<TValue>) => TSelected = () =>
|
|
73
|
+
({}) as TSelected,
|
|
74
|
+
): AngularAsyncQueuer<TValue, TSelected> {
|
|
75
|
+
const mergedOptions = {
|
|
76
|
+
...injectPacerOptions().asyncQueuer,
|
|
77
|
+
...options,
|
|
78
|
+
} as AsyncQueuerOptions<TValue>
|
|
79
|
+
|
|
80
|
+
const queuer = new AsyncQueuer<TValue>(fn, mergedOptions)
|
|
81
|
+
const state = injectStore(queuer.store, selector)
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
...queuer,
|
|
85
|
+
state,
|
|
86
|
+
} as AngularAsyncQueuer<TValue, TSelected>
|
|
87
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
|
+
import type {
|
|
8
|
+
AsyncRateLimiterOptions,
|
|
9
|
+
AsyncRateLimiterState,
|
|
10
|
+
} from '@tanstack/pacer/async-rate-limiter'
|
|
11
|
+
|
|
12
|
+
export interface AngularAsyncRateLimiter<
|
|
13
|
+
TFn extends AnyAsyncFunction,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* Reactive state signal that will be updated when the async rate limiter state changes
|
|
18
|
+
*
|
|
19
|
+
* Use this instead of `rateLimiter.store.state`
|
|
20
|
+
*/
|
|
21
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
24
|
+
* The state on the store object is not reactive in Angular signals.
|
|
25
|
+
*/
|
|
26
|
+
readonly store: Store<Readonly<AsyncRateLimiterState<TFn>>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An Angular function that creates and manages an AsyncRateLimiter instance.
|
|
31
|
+
*
|
|
32
|
+
* This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.
|
|
33
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
34
|
+
*
|
|
35
|
+
* This function provides async rate limiting functionality with promise support, error handling,
|
|
36
|
+
* retry capabilities, and abort support.
|
|
37
|
+
*
|
|
38
|
+
* ## State Management and Selector
|
|
39
|
+
*
|
|
40
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
41
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
42
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
43
|
+
*
|
|
44
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
45
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
46
|
+
* full control over when your component tracks state changes.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* // Default behavior - no reactive state subscriptions
|
|
51
|
+
* const rateLimiter = injectAsyncRateLimiter(
|
|
52
|
+
* async (id: string) => {
|
|
53
|
+
* const response = await fetch(`/api/data/${id}`);
|
|
54
|
+
* return response.json();
|
|
55
|
+
* },
|
|
56
|
+
* { limit: 5, window: 60000, windowType: 'sliding' }
|
|
57
|
+
* );
|
|
58
|
+
*
|
|
59
|
+
* // In an event handler
|
|
60
|
+
* const handleRequest = async (id: string) => {
|
|
61
|
+
* const result = await rateLimiter.maybeExecute(id);
|
|
62
|
+
* console.log('Result:', result);
|
|
63
|
+
* };
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
export function injectAsyncRateLimiter<
|
|
67
|
+
TFn extends AnyAsyncFunction,
|
|
68
|
+
TSelected = {},
|
|
69
|
+
>(
|
|
70
|
+
fn: TFn,
|
|
71
|
+
options: AsyncRateLimiterOptions<TFn>,
|
|
72
|
+
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
73
|
+
({}) as TSelected,
|
|
74
|
+
): AngularAsyncRateLimiter<TFn, TSelected> {
|
|
75
|
+
const mergedOptions = {
|
|
76
|
+
...injectPacerOptions().asyncRateLimiter,
|
|
77
|
+
...options,
|
|
78
|
+
} as AsyncRateLimiterOptions<TFn>
|
|
79
|
+
|
|
80
|
+
const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)
|
|
81
|
+
const state = injectStore(rateLimiter.store, selector)
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
...rateLimiter,
|
|
85
|
+
state,
|
|
86
|
+
} as AngularAsyncRateLimiter<TFn, TSelected>
|
|
87
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from '@tanstack/pacer/async-retryer'
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { AsyncThrottler } from '@tanstack/pacer/async-throttler'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
7
|
+
import type {
|
|
8
|
+
AsyncThrottlerOptions,
|
|
9
|
+
AsyncThrottlerState,
|
|
10
|
+
} from '@tanstack/pacer/async-throttler'
|
|
11
|
+
|
|
12
|
+
export interface AngularAsyncThrottler<
|
|
13
|
+
TFn extends AnyAsyncFunction,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* Reactive state signal that will be updated when the async throttler state changes
|
|
18
|
+
*
|
|
19
|
+
* Use this instead of `throttler.store.state`
|
|
20
|
+
*/
|
|
21
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
24
|
+
* The state on the store object is not reactive in Angular signals.
|
|
25
|
+
*/
|
|
26
|
+
readonly store: Store<Readonly<AsyncThrottlerState<TFn>>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An Angular function that creates and manages an AsyncThrottler instance.
|
|
31
|
+
*
|
|
32
|
+
* This is a lower-level function that provides direct access to the AsyncThrottler's functionality.
|
|
33
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
34
|
+
*
|
|
35
|
+
* This function provides async throttling functionality with promise support, error handling,
|
|
36
|
+
* retry capabilities, and abort support.
|
|
37
|
+
*
|
|
38
|
+
* The throttler will execute the function at most once within the specified wait time.
|
|
39
|
+
*
|
|
40
|
+
* ## State Management and Selector
|
|
41
|
+
*
|
|
42
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
43
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
44
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
45
|
+
*
|
|
46
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
47
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
48
|
+
* full control over when your component tracks state changes.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* // Default behavior - no reactive state subscriptions
|
|
53
|
+
* const throttler = injectAsyncThrottler(
|
|
54
|
+
* async (data: Data) => {
|
|
55
|
+
* const response = await fetch('/api/update', {
|
|
56
|
+
* method: 'POST',
|
|
57
|
+
* body: JSON.stringify(data)
|
|
58
|
+
* });
|
|
59
|
+
* return response.json();
|
|
60
|
+
* },
|
|
61
|
+
* { wait: 1000 }
|
|
62
|
+
* );
|
|
63
|
+
*
|
|
64
|
+
* // In an event handler
|
|
65
|
+
* const handleUpdate = async (data: Data) => {
|
|
66
|
+
* const result = await throttler.maybeExecute(data);
|
|
67
|
+
* console.log('Update result:', result);
|
|
68
|
+
* };
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export function injectAsyncThrottler<
|
|
72
|
+
TFn extends AnyAsyncFunction,
|
|
73
|
+
TSelected = {},
|
|
74
|
+
>(
|
|
75
|
+
fn: TFn,
|
|
76
|
+
options: AsyncThrottlerOptions<TFn>,
|
|
77
|
+
selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>
|
|
78
|
+
({}) as TSelected,
|
|
79
|
+
): AngularAsyncThrottler<TFn, TSelected> {
|
|
80
|
+
const mergedOptions = {
|
|
81
|
+
...injectPacerOptions().asyncThrottler,
|
|
82
|
+
...options,
|
|
83
|
+
} as AsyncThrottlerOptions<TFn>
|
|
84
|
+
|
|
85
|
+
const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)
|
|
86
|
+
const state = injectStore(throttler.store, selector)
|
|
87
|
+
|
|
88
|
+
return {
|
|
89
|
+
...throttler,
|
|
90
|
+
state,
|
|
91
|
+
} as AngularAsyncThrottler<TFn, TSelected>
|
|
92
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
}
|