@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,79 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { Batcher } from '@tanstack/pacer/batcher'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'
|
|
7
|
+
|
|
8
|
+
export interface AngularBatcher<TValue, TSelected = {}> extends Omit<
|
|
9
|
+
Batcher<TValue>,
|
|
10
|
+
'store'
|
|
11
|
+
> {
|
|
12
|
+
/**
|
|
13
|
+
* Reactive state signal that will be updated when the batcher state changes
|
|
14
|
+
*
|
|
15
|
+
* Use this instead of `batcher.store.state`
|
|
16
|
+
*/
|
|
17
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
18
|
+
/**
|
|
19
|
+
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
20
|
+
* The state on the store object is not reactive in Angular signals.
|
|
21
|
+
*/
|
|
22
|
+
readonly store: Store<Readonly<BatcherState<TValue>>>
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An Angular function that creates and manages a Batcher instance.
|
|
27
|
+
*
|
|
28
|
+
* This is a lower-level function that provides direct access to the Batcher's functionality.
|
|
29
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
30
|
+
*
|
|
31
|
+
* The Batcher collects items and processes them in batches based on configurable conditions:
|
|
32
|
+
* - Maximum batch size
|
|
33
|
+
* - Time-based batching (process after X milliseconds)
|
|
34
|
+
* - Custom batch processing logic via getShouldExecute
|
|
35
|
+
*
|
|
36
|
+
* ## State Management and Selector
|
|
37
|
+
*
|
|
38
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
39
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
40
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
41
|
+
*
|
|
42
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
43
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
44
|
+
* full control over when your component tracks state changes.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* // Default behavior - no reactive state subscriptions
|
|
49
|
+
* const batcher = injectBatcher(
|
|
50
|
+
* (items) => console.log('Processing batch:', items),
|
|
51
|
+
* { maxSize: 5, wait: 2000 }
|
|
52
|
+
* );
|
|
53
|
+
*
|
|
54
|
+
* // Add items
|
|
55
|
+
* batcher.addItem('task1');
|
|
56
|
+
*
|
|
57
|
+
* // Access the selected state
|
|
58
|
+
* const { items, isPending } = batcher.state();
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function injectBatcher<TValue, TSelected = {}>(
|
|
62
|
+
fn: (items: Array<TValue>) => void,
|
|
63
|
+
options: BatcherOptions<TValue> = {},
|
|
64
|
+
selector: (state: BatcherState<TValue>) => TSelected = () =>
|
|
65
|
+
({}) as TSelected,
|
|
66
|
+
): AngularBatcher<TValue, TSelected> {
|
|
67
|
+
const mergedOptions = {
|
|
68
|
+
...injectPacerOptions().batcher,
|
|
69
|
+
...options,
|
|
70
|
+
} as BatcherOptions<TValue>
|
|
71
|
+
|
|
72
|
+
const batcher = new Batcher<TValue>(fn, mergedOptions)
|
|
73
|
+
const state = injectStore(batcher.store, selector)
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
...batcher,
|
|
77
|
+
state,
|
|
78
|
+
} as AngularBatcher<TValue, TSelected>
|
|
79
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { injectDebouncer } from './injectDebouncer'
|
|
2
|
+
import type { DebouncerOptions } from '@tanstack/pacer/debouncer'
|
|
3
|
+
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An Angular function that creates a debounced version of a callback function.
|
|
7
|
+
* This function is essentially a wrapper around `injectDebouncer` that provides
|
|
8
|
+
* a simplified API for basic 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 `injectDebouncer`, making it ideal for basic
|
|
15
|
+
* debouncing needs. However, it does not expose the underlying Debouncer instance.
|
|
16
|
+
*
|
|
17
|
+
* For advanced usage requiring features like:
|
|
18
|
+
* - Manual cancellation
|
|
19
|
+
* - Access to execution counts
|
|
20
|
+
* - State tracking
|
|
21
|
+
*
|
|
22
|
+
* Consider using the `injectDebouncer` function instead.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* // Debounce a search handler
|
|
27
|
+
* const handleSearch = injectDebouncedCallback((query: string) => {
|
|
28
|
+
* fetchSearchResults(query);
|
|
29
|
+
* }, {
|
|
30
|
+
* wait: 500 // Wait 500ms between executions
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* // Use in an input
|
|
34
|
+
* <input
|
|
35
|
+
* type="search"
|
|
36
|
+
* (input)="handleSearch($event.target.value)"
|
|
37
|
+
* />
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function injectDebouncedCallback<TFn extends AnyFunction>(
|
|
41
|
+
fn: TFn,
|
|
42
|
+
options: DebouncerOptions<TFn>,
|
|
43
|
+
): (...args: Parameters<TFn>) => void {
|
|
44
|
+
const debouncer = injectDebouncer(fn, options)
|
|
45
|
+
return (...args: Parameters<TFn>) => debouncer.maybeExecute(...args)
|
|
46
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { signal } from '@angular/core'
|
|
2
|
+
import { injectDebouncer } from './injectDebouncer'
|
|
3
|
+
import type { AngularDebouncer } from './injectDebouncer'
|
|
4
|
+
import type {
|
|
5
|
+
DebouncerOptions,
|
|
6
|
+
DebouncerState,
|
|
7
|
+
} from '@tanstack/pacer/debouncer'
|
|
8
|
+
|
|
9
|
+
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
10
|
+
|
|
11
|
+
export type DebouncedSignal<TValue, TSelected = {}> = ((
|
|
12
|
+
...args: []
|
|
13
|
+
) => TValue) & {
|
|
14
|
+
/**
|
|
15
|
+
* Set or update the debounced value. This calls `debouncer.maybeExecute(...)`.
|
|
16
|
+
*/
|
|
17
|
+
readonly set: Setter<TValue>
|
|
18
|
+
/**
|
|
19
|
+
* The debouncer instance with additional control methods and state signals.
|
|
20
|
+
*/
|
|
21
|
+
readonly debouncer: AngularDebouncer<Setter<TValue>, TSelected>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* An Angular function that creates a debounced state signal, combining Angular's signal with debouncing functionality.
|
|
26
|
+
* This function provides both the current debounced value and methods to update it.
|
|
27
|
+
*
|
|
28
|
+
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
29
|
+
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
30
|
+
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
31
|
+
* or window resize dimensions.
|
|
32
|
+
*
|
|
33
|
+
* The function returns a callable object:
|
|
34
|
+
* - `debounced()`: Get the current debounced value
|
|
35
|
+
* - `debounced.set(...)`: Set or update the debounced value (debounced via maybeExecute)
|
|
36
|
+
* - `debounced.debouncer`: The debouncer instance with additional control methods and state signals
|
|
37
|
+
*
|
|
38
|
+
* ## State Management and Selector
|
|
39
|
+
*
|
|
40
|
+
* The function uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
41
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger signal updates,
|
|
42
|
+
* optimizing performance by preventing unnecessary subscriptions 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. Only when you provide a selector will
|
|
47
|
+
* the reactive system track the selected state values.
|
|
48
|
+
*
|
|
49
|
+
* Available debouncer state properties:
|
|
50
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
51
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
52
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
53
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
54
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* const debouncedQuery = injectDebouncedSignal('', { wait: 500 })
|
|
59
|
+
*
|
|
60
|
+
* // Get value
|
|
61
|
+
* console.log(debouncedQuery())
|
|
62
|
+
*
|
|
63
|
+
* // Set/update value (debounced)
|
|
64
|
+
* debouncedQuery.set('hello')
|
|
65
|
+
*
|
|
66
|
+
* // Access debouncer
|
|
67
|
+
* console.log(debouncedQuery.debouncer.state().isPending)
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export function injectDebouncedSignal<TValue, TSelected = {}>(
|
|
71
|
+
value: TValue,
|
|
72
|
+
initialOptions: DebouncerOptions<Setter<TValue>>,
|
|
73
|
+
selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
|
|
74
|
+
): DebouncedSignal<TValue, TSelected> {
|
|
75
|
+
const debouncedValue = signal<TValue>(value)
|
|
76
|
+
|
|
77
|
+
const debouncer = injectDebouncer(
|
|
78
|
+
(newValue: TValue | ((prev: TValue) => TValue)) => {
|
|
79
|
+
if (typeof newValue === 'function') {
|
|
80
|
+
debouncedValue.update(newValue as (prev: TValue) => TValue)
|
|
81
|
+
} else {
|
|
82
|
+
debouncedValue.set(newValue)
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
initialOptions,
|
|
86
|
+
selector,
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
const set: Setter<TValue> = (
|
|
90
|
+
newValue: TValue | ((prev: TValue) => TValue),
|
|
91
|
+
) => {
|
|
92
|
+
debouncer.maybeExecute(newValue)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const debounced = Object.assign(() => debouncedValue(), {
|
|
96
|
+
set,
|
|
97
|
+
debouncer,
|
|
98
|
+
}) as DebouncedSignal<TValue, TSelected>
|
|
99
|
+
|
|
100
|
+
return debounced
|
|
101
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { effect } from '@angular/core'
|
|
2
|
+
import { injectDebouncedSignal } from './injectDebouncedSignal'
|
|
3
|
+
import type { DebouncedSignal } from './injectDebouncedSignal'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type {
|
|
6
|
+
DebouncerOptions,
|
|
7
|
+
DebouncerState,
|
|
8
|
+
} from '@tanstack/pacer/debouncer'
|
|
9
|
+
|
|
10
|
+
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* An Angular function that creates a debounced value that updates only after a specified delay.
|
|
14
|
+
* Unlike injectDebouncedSignal, this function automatically tracks changes to the input signal
|
|
15
|
+
* and updates the debounced value accordingly.
|
|
16
|
+
*
|
|
17
|
+
* The debounced value will only update after the specified wait time has elapsed since
|
|
18
|
+
* the last change to the input value. If the input value changes again before the wait
|
|
19
|
+
* time expires, the timer resets and starts waiting again.
|
|
20
|
+
*
|
|
21
|
+
* This is useful for deriving debounced values from signals that change frequently,
|
|
22
|
+
* like search queries or form inputs, where you want to limit how often downstream effects
|
|
23
|
+
* or calculations occur.
|
|
24
|
+
*
|
|
25
|
+
* The function returns a tuple containing:
|
|
26
|
+
* - A Signal that provides the current debounced value
|
|
27
|
+
* - The debouncer instance with control methods
|
|
28
|
+
*
|
|
29
|
+
* ## State Management and Selector
|
|
30
|
+
*
|
|
31
|
+
* The function uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
32
|
+
* The `selector` parameter allows you to specify which debouncer state changes will trigger signal updates,
|
|
33
|
+
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
34
|
+
*
|
|
35
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
36
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
37
|
+
* full control over when your component tracks state changes. Only when you provide a selector will
|
|
38
|
+
* the reactive system track the selected state values.
|
|
39
|
+
*
|
|
40
|
+
* Available debouncer state properties:
|
|
41
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
42
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
43
|
+
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
44
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
45
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
46
|
+
*
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* // Default behavior - no reactive state subscriptions
|
|
51
|
+
* const searchQuery = signal('')
|
|
52
|
+
* const debounced = injectDebouncedValue(searchQuery, {
|
|
53
|
+
* wait: 500, // Wait 500ms after last change
|
|
54
|
+
* })
|
|
55
|
+
*
|
|
56
|
+
* // Opt-in to reactive updates when pending state changes (optimized for loading indicators)
|
|
57
|
+
* const debouncedWithPending = injectDebouncedValue(
|
|
58
|
+
* searchQuery,
|
|
59
|
+
* { wait: 500 },
|
|
60
|
+
* (state) => ({ isPending: state.isPending }),
|
|
61
|
+
* )
|
|
62
|
+
*
|
|
63
|
+
* // Debounced value will update 500ms after searchQuery stops changing
|
|
64
|
+
* effect(() => {
|
|
65
|
+
* fetchSearchResults(debounced())
|
|
66
|
+
* })
|
|
67
|
+
*
|
|
68
|
+
* // Access selected debouncer state via signals (only if you provided a selector)
|
|
69
|
+
* effect(() => {
|
|
70
|
+
* console.log('Is pending:', debouncedWithPending.state().isPending)
|
|
71
|
+
* })
|
|
72
|
+
*
|
|
73
|
+
* // Control the debouncer
|
|
74
|
+
* debounced.cancel() // Cancel any pending updates
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export function injectDebouncedValue<TValue, TSelected = {}>(
|
|
78
|
+
value: Signal<TValue>,
|
|
79
|
+
initialOptions: DebouncerOptions<Setter<TValue>>,
|
|
80
|
+
selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
|
|
81
|
+
): DebouncedSignal<TValue, TSelected>
|
|
82
|
+
export function injectDebouncedValue<TValue, TSelected = {}>(
|
|
83
|
+
value: Signal<TValue>,
|
|
84
|
+
initialValue: TValue,
|
|
85
|
+
initialOptions: DebouncerOptions<Setter<TValue>>,
|
|
86
|
+
selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
|
|
87
|
+
): DebouncedSignal<TValue, TSelected>
|
|
88
|
+
export function injectDebouncedValue<TValue, TSelected = {}>(
|
|
89
|
+
value: Signal<TValue>,
|
|
90
|
+
initialValueOrOptions: TValue | DebouncerOptions<Setter<TValue>>,
|
|
91
|
+
initialOptionsOrSelector?:
|
|
92
|
+
| DebouncerOptions<Setter<TValue>>
|
|
93
|
+
| ((state: DebouncerState<Setter<TValue>>) => TSelected),
|
|
94
|
+
maybeSelector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
|
|
95
|
+
): DebouncedSignal<TValue, TSelected> {
|
|
96
|
+
const hasSelector = typeof initialOptionsOrSelector === 'function'
|
|
97
|
+
|
|
98
|
+
const hasInitialValue =
|
|
99
|
+
(initialOptionsOrSelector !== undefined && !hasSelector) ||
|
|
100
|
+
maybeSelector !== undefined
|
|
101
|
+
|
|
102
|
+
const initialValue = hasInitialValue
|
|
103
|
+
? (initialValueOrOptions as TValue)
|
|
104
|
+
: (undefined as unknown as TValue)
|
|
105
|
+
const initialOptions = hasInitialValue
|
|
106
|
+
? (initialOptionsOrSelector as DebouncerOptions<Setter<TValue>>)
|
|
107
|
+
: (initialValueOrOptions as DebouncerOptions<Setter<TValue>>)
|
|
108
|
+
const selector = hasInitialValue
|
|
109
|
+
? maybeSelector
|
|
110
|
+
: (initialOptionsOrSelector as
|
|
111
|
+
| ((state: DebouncerState<Setter<TValue>>) => TSelected)
|
|
112
|
+
| undefined)
|
|
113
|
+
|
|
114
|
+
const debounced = injectDebouncedSignal(
|
|
115
|
+
initialValue,
|
|
116
|
+
initialOptions,
|
|
117
|
+
selector,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
effect(() => {
|
|
121
|
+
const latest = value()
|
|
122
|
+
debounced.set(latest)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
return debounced
|
|
126
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { Debouncer } from '@tanstack/pacer/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 { AnyFunction } from '@tanstack/pacer/types'
|
|
7
|
+
import type {
|
|
8
|
+
DebouncerOptions,
|
|
9
|
+
DebouncerState,
|
|
10
|
+
} from '@tanstack/pacer/debouncer'
|
|
11
|
+
|
|
12
|
+
export interface AngularDebouncer<
|
|
13
|
+
TFn extends AnyFunction,
|
|
14
|
+
TSelected = {},
|
|
15
|
+
> extends Omit<Debouncer<TFn>, 'store'> {
|
|
16
|
+
/**
|
|
17
|
+
* Reactive state signal that will be updated when the 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<DebouncerState<TFn>>>
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An Angular function that creates and manages a Debouncer instance.
|
|
31
|
+
*
|
|
32
|
+
* This is a lower-level function that provides direct access to the Debouncer's functionality.
|
|
33
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
34
|
+
*
|
|
35
|
+
* This function provides debouncing functionality to limit how often a function can be called,
|
|
36
|
+
* waiting for a specified delay before executing the latest call. This is useful for handling
|
|
37
|
+
* frequent events like window resizing, scroll events, or real-time search inputs.
|
|
38
|
+
*
|
|
39
|
+
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
40
|
+
* since the last call. If the function is called again before the wait time expires, the
|
|
41
|
+
* timer resets and starts waiting again.
|
|
42
|
+
*
|
|
43
|
+
* ## State Management and Selector
|
|
44
|
+
*
|
|
45
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
46
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
47
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
48
|
+
*
|
|
49
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
50
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
51
|
+
* full control over when your component tracks state changes.
|
|
52
|
+
*
|
|
53
|
+
* Available state properties:
|
|
54
|
+
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
55
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
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
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* // Default behavior - no reactive state subscriptions
|
|
63
|
+
* const debouncer = injectDebouncer(
|
|
64
|
+
* (query: string) => fetchSearchResults(query),
|
|
65
|
+
* { wait: 500 }
|
|
66
|
+
* );
|
|
67
|
+
*
|
|
68
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
69
|
+
* const debouncer = injectDebouncer(
|
|
70
|
+
* (query: string) => fetchSearchResults(query),
|
|
71
|
+
* { wait: 500 },
|
|
72
|
+
* (state) => ({ isPending: state.isPending })
|
|
73
|
+
* );
|
|
74
|
+
*
|
|
75
|
+
* // In an event handler
|
|
76
|
+
* const handleChange = (e: Event) => {
|
|
77
|
+
* const target = e.target as HTMLInputElement
|
|
78
|
+
* debouncer.maybeExecute(target.value);
|
|
79
|
+
* };
|
|
80
|
+
*
|
|
81
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
82
|
+
* const { isPending } = debouncer.state();
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(
|
|
86
|
+
fn: TFn,
|
|
87
|
+
options: DebouncerOptions<TFn>,
|
|
88
|
+
selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,
|
|
89
|
+
): AngularDebouncer<TFn, TSelected> {
|
|
90
|
+
const mergedOptions = {
|
|
91
|
+
...injectPacerOptions().debouncer,
|
|
92
|
+
...options,
|
|
93
|
+
} as DebouncerOptions<TFn>
|
|
94
|
+
|
|
95
|
+
const debouncer = new Debouncer<TFn>(fn, mergedOptions)
|
|
96
|
+
const state = injectStore(debouncer.store, selector)
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
...debouncer,
|
|
100
|
+
state,
|
|
101
|
+
} as AngularDebouncer<TFn, TSelected>
|
|
102
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export * from '@tanstack/pacer'
|
|
2
|
+
|
|
3
|
+
export * from './provider/pacer-provider'
|
|
4
|
+
|
|
5
|
+
export * from './async-batcher/injectAsyncBatchedCallback'
|
|
6
|
+
export * from './async-batcher/injectAsyncBatcher'
|
|
7
|
+
|
|
8
|
+
export * from './async-debouncer/injectAsyncDebouncedCallback'
|
|
9
|
+
export * from './async-debouncer/injectAsyncDebouncer'
|
|
10
|
+
|
|
11
|
+
export * from './async-queuer/injectAsyncQueuedSignal'
|
|
12
|
+
export * from './async-queuer/injectAsyncQueuer'
|
|
13
|
+
|
|
14
|
+
export * from './async-rate-limiter/injectAsyncRateLimitedCallback'
|
|
15
|
+
export * from './async-rate-limiter/injectAsyncRateLimiter'
|
|
16
|
+
|
|
17
|
+
export * from './async-throttler/injectAsyncThrottledCallback'
|
|
18
|
+
export * from './async-throttler/injectAsyncThrottler'
|
|
19
|
+
|
|
20
|
+
export * from './batcher/injectBatchedCallback'
|
|
21
|
+
export * from './batcher/injectBatcher'
|
|
22
|
+
|
|
23
|
+
export * from './debouncer/injectDebouncedCallback'
|
|
24
|
+
export * from './debouncer/injectDebouncedSignal'
|
|
25
|
+
export * from './debouncer/injectDebouncedValue'
|
|
26
|
+
export * from './debouncer/injectDebouncer'
|
|
27
|
+
|
|
28
|
+
export * from './queuer/injectQueuedSignal'
|
|
29
|
+
export * from './queuer/injectQueuedValue'
|
|
30
|
+
export * from './queuer/injectQueuer'
|
|
31
|
+
|
|
32
|
+
export * from './rate-limiter/injectRateLimitedCallback'
|
|
33
|
+
export * from './rate-limiter/injectRateLimitedSignal'
|
|
34
|
+
export * from './rate-limiter/injectRateLimitedValue'
|
|
35
|
+
export * from './rate-limiter/injectRateLimiter'
|
|
36
|
+
|
|
37
|
+
export * from './throttler/injectThrottledCallback'
|
|
38
|
+
export * from './throttler/injectThrottledSignal'
|
|
39
|
+
export * from './throttler/injectThrottledValue'
|
|
40
|
+
export * from './throttler/injectThrottler'
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { InjectionToken, inject } from '@angular/core'
|
|
2
|
+
import type {
|
|
3
|
+
AnyAsyncFunction,
|
|
4
|
+
AnyFunction,
|
|
5
|
+
AsyncBatcherOptions,
|
|
6
|
+
AsyncDebouncerOptions,
|
|
7
|
+
AsyncQueuerOptions,
|
|
8
|
+
AsyncRateLimiterOptions,
|
|
9
|
+
AsyncThrottlerOptions,
|
|
10
|
+
BatcherOptions,
|
|
11
|
+
DebouncerOptions,
|
|
12
|
+
QueuerOptions,
|
|
13
|
+
RateLimiterOptions,
|
|
14
|
+
ThrottlerOptions,
|
|
15
|
+
} from '@tanstack/pacer'
|
|
16
|
+
|
|
17
|
+
export interface PacerProviderOptions {
|
|
18
|
+
asyncBatcher?: Partial<AsyncBatcherOptions<any>>
|
|
19
|
+
asyncDebouncer?: Partial<AsyncDebouncerOptions<AnyAsyncFunction>>
|
|
20
|
+
asyncQueuer?: Partial<AsyncQueuerOptions<any>>
|
|
21
|
+
asyncRateLimiter?: Partial<AsyncRateLimiterOptions<AnyAsyncFunction>>
|
|
22
|
+
asyncThrottler?: Partial<AsyncThrottlerOptions<AnyAsyncFunction>>
|
|
23
|
+
batcher?: Partial<BatcherOptions<any>>
|
|
24
|
+
debouncer?: Partial<DebouncerOptions<AnyFunction>>
|
|
25
|
+
queuer?: Partial<QueuerOptions<any>>
|
|
26
|
+
rateLimiter?: Partial<RateLimiterOptions<AnyFunction>>
|
|
27
|
+
throttler?: Partial<ThrottlerOptions<AnyFunction>>
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const DEFAULT_OPTIONS: PacerProviderOptions = {}
|
|
31
|
+
|
|
32
|
+
export const PACER_OPTIONS = new InjectionToken<PacerProviderOptions>(
|
|
33
|
+
'PACER_OPTIONS',
|
|
34
|
+
{
|
|
35
|
+
providedIn: 'root',
|
|
36
|
+
factory: () => DEFAULT_OPTIONS,
|
|
37
|
+
},
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
export function injectPacerOptions(): PacerProviderOptions {
|
|
41
|
+
try {
|
|
42
|
+
return inject(PACER_OPTIONS, { optional: true }) ?? DEFAULT_OPTIONS
|
|
43
|
+
} catch {
|
|
44
|
+
return DEFAULT_OPTIONS
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { PACER_OPTIONS } from './pacer-context'
|
|
2
|
+
import type { Provider } from '@angular/core'
|
|
3
|
+
import type { PacerProviderOptions } from './pacer-context'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Provides default options for all Pacer utilities in the Angular application.
|
|
7
|
+
* Use this function when configuring your Angular application to set default options
|
|
8
|
+
* that will be used by all Pacer utilities throughout your app.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* // In your app.config.ts (standalone)
|
|
13
|
+
* export const appConfig: ApplicationConfig = {
|
|
14
|
+
* providers: [
|
|
15
|
+
* providePacerOptions({
|
|
16
|
+
* debouncer: { wait: 300 },
|
|
17
|
+
* throttler: { wait: 100 },
|
|
18
|
+
* }),
|
|
19
|
+
* ],
|
|
20
|
+
* };
|
|
21
|
+
*
|
|
22
|
+
* // Or in NgModule (module-based)
|
|
23
|
+
* @NgModule({
|
|
24
|
+
* providers: [
|
|
25
|
+
* providePacerOptions({
|
|
26
|
+
* debouncer: { wait: 300 },
|
|
27
|
+
* }),
|
|
28
|
+
* ],
|
|
29
|
+
* })
|
|
30
|
+
* export class AppModule {}
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export function providePacerOptions(options: PacerProviderOptions): Provider {
|
|
34
|
+
return {
|
|
35
|
+
provide: PACER_OPTIONS,
|
|
36
|
+
useValue: options,
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { computed } from '@angular/core'
|
|
2
|
+
import { injectQueuer } from './injectQueuer'
|
|
3
|
+
import type { AngularQueuer } from './injectQueuer'
|
|
4
|
+
import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
|
|
5
|
+
|
|
6
|
+
export type QueuedSignal<TValue, TSelected = {}> = (() => Array<TValue>) & {
|
|
7
|
+
/**
|
|
8
|
+
* Add an item to the queue.
|
|
9
|
+
*/
|
|
10
|
+
readonly addItem: AngularQueuer<TValue, TSelected>['addItem']
|
|
11
|
+
/**
|
|
12
|
+
* The queuer instance with additional control methods and state signals.
|
|
13
|
+
*/
|
|
14
|
+
readonly queuer: AngularQueuer<TValue, TSelected>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* An Angular function that creates a queuer with managed state, combining Angular's signals with queuing functionality.
|
|
19
|
+
* This function provides both the current queue state and queue control methods.
|
|
20
|
+
*
|
|
21
|
+
* The queue state is automatically updated whenever items are added, removed, or reordered in the queue.
|
|
22
|
+
* All queue operations are reflected in the state array returned by the function.
|
|
23
|
+
*
|
|
24
|
+
* The function returns a callable object:
|
|
25
|
+
* - `queued()`: Get the current queue items as an array
|
|
26
|
+
* - `queued.addItem(...)`: Add an item to the queue
|
|
27
|
+
* - `queued.queue`: The queuer instance with additional control methods
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // Default behavior - track items
|
|
32
|
+
* const queued = injectQueuedSignal(
|
|
33
|
+
* (item) => console.log('Processing:', item),
|
|
34
|
+
* { started: true, wait: 1000 }
|
|
35
|
+
* );
|
|
36
|
+
*
|
|
37
|
+
* // Add items
|
|
38
|
+
* queued.addItem('task1');
|
|
39
|
+
*
|
|
40
|
+
* // Access items
|
|
41
|
+
* console.log(queued()); // ['task1']
|
|
42
|
+
*
|
|
43
|
+
* // Control the queue
|
|
44
|
+
* queued.queuer.start();
|
|
45
|
+
* queued.queuer.stop();
|
|
46
|
+
* ```
|
|
47
|
+
*/
|
|
48
|
+
export function injectQueuedSignal<
|
|
49
|
+
TValue,
|
|
50
|
+
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
51
|
+
QueuerState<TValue>,
|
|
52
|
+
'items'
|
|
53
|
+
>,
|
|
54
|
+
>(
|
|
55
|
+
fn: (item: TValue) => void,
|
|
56
|
+
options: QueuerOptions<TValue> = {},
|
|
57
|
+
selector: (state: QueuerState<TValue>) => TSelected = (state) =>
|
|
58
|
+
({ items: state.items }) as TSelected,
|
|
59
|
+
): QueuedSignal<TValue, TSelected> {
|
|
60
|
+
const queuer = injectQueuer(fn, options, selector)
|
|
61
|
+
|
|
62
|
+
const items = computed(() => queuer.state().items as Array<TValue>)
|
|
63
|
+
|
|
64
|
+
const queued = Object.assign(items, {
|
|
65
|
+
addItem: queuer.addItem.bind(queuer),
|
|
66
|
+
queuer,
|
|
67
|
+
}) as QueuedSignal<TValue, TSelected>
|
|
68
|
+
|
|
69
|
+
return queued
|
|
70
|
+
}
|