@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,125 +0,0 @@
|
|
|
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) | undefined)
|
|
112
|
-
|
|
113
|
-
const debounced = injectDebouncedSignal(
|
|
114
|
-
initialValue,
|
|
115
|
-
initialOptions,
|
|
116
|
-
selector,
|
|
117
|
-
)
|
|
118
|
-
|
|
119
|
-
effect(() => {
|
|
120
|
-
const latest = value()
|
|
121
|
-
debounced.set(latest)
|
|
122
|
-
})
|
|
123
|
-
|
|
124
|
-
return debounced
|
|
125
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { Debouncer } from '@tanstack/pacer/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 { AnyFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
DebouncerOptions,
|
|
10
|
-
DebouncerState,
|
|
11
|
-
} from '@tanstack/pacer/debouncer'
|
|
12
|
-
|
|
13
|
-
export interface AngularDebouncerOptions<
|
|
14
|
-
TFn extends AnyFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends DebouncerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (debouncer: AngularDebouncer<TFn, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularDebouncer<
|
|
25
|
-
TFn extends AnyFunction,
|
|
26
|
-
TSelected = {},
|
|
27
|
-
> extends Omit<Debouncer<TFn>, 'store'> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the debouncer state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `debouncer.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `debouncer.state` instead of `debouncer.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<DebouncerState<TFn>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages a Debouncer instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the Debouncer's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* This function provides debouncing functionality to limit how often a function can be called,
|
|
48
|
-
* waiting for a specified delay before executing the latest call. This is useful for handling
|
|
49
|
-
* frequent events like window resizing, scroll events, or real-time search inputs.
|
|
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
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
68
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
69
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
70
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
71
|
-
*
|
|
72
|
-
* ## Cleanup on Destroy
|
|
73
|
-
*
|
|
74
|
-
* By default, the function cancels any pending execution when the component is destroyed.
|
|
75
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
76
|
-
*
|
|
77
|
-
* ```ts
|
|
78
|
-
* const debouncer = injectDebouncer(fn, {
|
|
79
|
-
* wait: 500,
|
|
80
|
-
* onUnmount: (d) => d.flush()
|
|
81
|
-
* });
|
|
82
|
-
* ```
|
|
83
|
-
*
|
|
84
|
-
* @example
|
|
85
|
-
* ```ts
|
|
86
|
-
* // Default behavior - no reactive state subscriptions
|
|
87
|
-
* const debouncer = injectDebouncer(
|
|
88
|
-
* (query: string) => fetchSearchResults(query),
|
|
89
|
-
* { wait: 500 }
|
|
90
|
-
* );
|
|
91
|
-
*
|
|
92
|
-
* // Opt-in to track isPending changes (optimized for loading states)
|
|
93
|
-
* const debouncer = injectDebouncer(
|
|
94
|
-
* (query: string) => fetchSearchResults(query),
|
|
95
|
-
* { wait: 500 },
|
|
96
|
-
* (state) => ({ isPending: state.isPending })
|
|
97
|
-
* );
|
|
98
|
-
*
|
|
99
|
-
* // In an event handler
|
|
100
|
-
* const handleChange = (e: Event) => {
|
|
101
|
-
* const target = e.target as HTMLInputElement
|
|
102
|
-
* debouncer.maybeExecute(target.value);
|
|
103
|
-
* };
|
|
104
|
-
*
|
|
105
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
106
|
-
* const { isPending } = debouncer.state();
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
export function injectDebouncer<TFn extends AnyFunction, TSelected = {}>(
|
|
110
|
-
fn: TFn,
|
|
111
|
-
options: AngularDebouncerOptions<TFn, TSelected>,
|
|
112
|
-
selector: (state: DebouncerState<TFn>) => TSelected = () => ({}) as TSelected,
|
|
113
|
-
): AngularDebouncer<TFn, TSelected> {
|
|
114
|
-
const mergedOptions = {
|
|
115
|
-
...injectPacerOptions().debouncer,
|
|
116
|
-
...options,
|
|
117
|
-
} as AngularDebouncerOptions<TFn, TSelected>
|
|
118
|
-
|
|
119
|
-
const debouncer = new Debouncer<TFn>(fn, mergedOptions)
|
|
120
|
-
const state = injectSelector(debouncer.store, selector)
|
|
121
|
-
|
|
122
|
-
const result = {
|
|
123
|
-
...debouncer,
|
|
124
|
-
state,
|
|
125
|
-
} as AngularDebouncer<TFn, TSelected>
|
|
126
|
-
|
|
127
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
128
|
-
destroyRef?.onDestroy(() => {
|
|
129
|
-
if (mergedOptions.onUnmount) {
|
|
130
|
-
mergedOptions.onUnmount(result)
|
|
131
|
-
} else {
|
|
132
|
-
debouncer.cancel()
|
|
133
|
-
}
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
return result
|
|
137
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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'
|
package/src/provider/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
}
|
package/src/queuer/index.ts
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
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 interface QueuedSignal<TValue, TSelected = {}> {
|
|
7
|
-
(): Array<TValue>
|
|
8
|
-
addItem: AngularQueuer<TValue, TSelected>['addItem']
|
|
9
|
-
queuer: AngularQueuer<TValue, TSelected>
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* An Angular function that creates a queuer with managed state, combining Angular's signals with queuing functionality.
|
|
14
|
-
* This function provides both the current queue state and queue control methods.
|
|
15
|
-
*
|
|
16
|
-
* The queue state is automatically updated whenever items are added, removed, or reordered in the queue.
|
|
17
|
-
* All queue operations are reflected in the state array returned by the function.
|
|
18
|
-
*
|
|
19
|
-
* The function returns a callable object:
|
|
20
|
-
* - `queued()`: Get the current queue items as an array
|
|
21
|
-
* - `queued.addItem(...)`: Add an item to the queue
|
|
22
|
-
* - `queued.queue`: The queuer instance with additional control methods
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* // Default behavior - track items
|
|
27
|
-
* const queued = injectQueuedSignal(
|
|
28
|
-
* (item) => console.log('Processing:', item),
|
|
29
|
-
* { started: true, wait: 1000 }
|
|
30
|
-
* );
|
|
31
|
-
*
|
|
32
|
-
* // Add items
|
|
33
|
-
* queued.addItem('task1');
|
|
34
|
-
*
|
|
35
|
-
* // Access items
|
|
36
|
-
* console.log(queued()); // ['task1']
|
|
37
|
-
*
|
|
38
|
-
* // Control the queue
|
|
39
|
-
* queued.queuer.start();
|
|
40
|
-
* queued.queuer.stop();
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export function injectQueuedSignal<
|
|
44
|
-
TValue,
|
|
45
|
-
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
46
|
-
QueuerState<TValue>,
|
|
47
|
-
'items'
|
|
48
|
-
>,
|
|
49
|
-
>(
|
|
50
|
-
fn: (item: TValue) => void,
|
|
51
|
-
options: QueuerOptions<TValue> = {},
|
|
52
|
-
selector: (state: QueuerState<TValue>) => TSelected = (state) =>
|
|
53
|
-
({ items: state.items }) as TSelected,
|
|
54
|
-
): QueuedSignal<TValue, TSelected> {
|
|
55
|
-
const queuer = injectQueuer(fn, options, selector)
|
|
56
|
-
|
|
57
|
-
const items = computed(() => queuer.state().items as Array<TValue>)
|
|
58
|
-
|
|
59
|
-
const queued = Object.assign(items, {
|
|
60
|
-
addItem: queuer.addItem.bind(queuer),
|
|
61
|
-
queuer,
|
|
62
|
-
}) as QueuedSignal<TValue, TSelected>
|
|
63
|
-
|
|
64
|
-
return queued
|
|
65
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { effect, linkedSignal, signal } from '@angular/core'
|
|
2
|
-
import { injectQueuedSignal } from './injectQueuedSignal'
|
|
3
|
-
import type { QueuedSignal } from './injectQueuedSignal'
|
|
4
|
-
import type { Signal } from '@angular/core'
|
|
5
|
-
import type { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* An Angular function that creates a queued value that processes state changes in order with an optional delay.
|
|
9
|
-
* This function uses injectQueuedSignal internally to manage a queue of state changes and apply them sequentially.
|
|
10
|
-
*
|
|
11
|
-
* The queued value will process changes in the order they are received, with optional delays between
|
|
12
|
-
* processing each change. This is useful for handling state updates that need to be processed
|
|
13
|
-
* in a specific order, like animations or sequential UI updates.
|
|
14
|
-
*
|
|
15
|
-
* The function returns a tuple containing:
|
|
16
|
-
* - A Signal that provides the current queued value
|
|
17
|
-
* - The queuer instance with control methods
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* const initialValue = signal('initial')
|
|
22
|
-
* const queued = injectQueuedValue(initialValue, {
|
|
23
|
-
* wait: 500,
|
|
24
|
-
* started: true,
|
|
25
|
-
* })
|
|
26
|
-
*
|
|
27
|
-
* // Add changes to the queue
|
|
28
|
-
* queued.addItem('new value')
|
|
29
|
-
* ```
|
|
30
|
-
*/
|
|
31
|
-
export function injectQueuedValue<
|
|
32
|
-
TValue,
|
|
33
|
-
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
34
|
-
QueuerState<TValue>,
|
|
35
|
-
'items'
|
|
36
|
-
>,
|
|
37
|
-
>(
|
|
38
|
-
value: Signal<TValue>,
|
|
39
|
-
options?: QueuerOptions<TValue>,
|
|
40
|
-
selector?: (state: QueuerState<TValue>) => TSelected,
|
|
41
|
-
): QueuedSignal<TValue, TSelected>
|
|
42
|
-
export function injectQueuedValue<
|
|
43
|
-
TValue,
|
|
44
|
-
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
45
|
-
QueuerState<TValue>,
|
|
46
|
-
'items'
|
|
47
|
-
>,
|
|
48
|
-
>(
|
|
49
|
-
value: Signal<TValue>,
|
|
50
|
-
initialValue: TValue,
|
|
51
|
-
options?: QueuerOptions<TValue>,
|
|
52
|
-
selector?: (state: QueuerState<TValue>) => TSelected,
|
|
53
|
-
): QueuedSignal<TValue, TSelected>
|
|
54
|
-
export function injectQueuedValue<
|
|
55
|
-
TValue,
|
|
56
|
-
TSelected extends Pick<QueuerState<TValue>, 'items'> = Pick<
|
|
57
|
-
QueuerState<TValue>,
|
|
58
|
-
'items'
|
|
59
|
-
>,
|
|
60
|
-
>(
|
|
61
|
-
value: Signal<TValue>,
|
|
62
|
-
initialValueOrOptions?: TValue | QueuerOptions<TValue>,
|
|
63
|
-
initialOptionsOrSelector?:
|
|
64
|
-
QueuerOptions<TValue> | ((state: QueuerState<TValue>) => TSelected),
|
|
65
|
-
maybeSelector?: (state: QueuerState<TValue>) => TSelected,
|
|
66
|
-
): QueuedSignal<TValue, TSelected> {
|
|
67
|
-
const hasSelector = typeof initialOptionsOrSelector === 'function'
|
|
68
|
-
const hasInitialValue =
|
|
69
|
-
(initialOptionsOrSelector !== undefined && !hasSelector) ||
|
|
70
|
-
maybeSelector !== undefined
|
|
71
|
-
|
|
72
|
-
const initialValue = hasInitialValue
|
|
73
|
-
? (initialValueOrOptions as TValue)
|
|
74
|
-
: value()
|
|
75
|
-
const initialOptions = hasInitialValue
|
|
76
|
-
? (initialOptionsOrSelector as QueuerOptions<TValue>)
|
|
77
|
-
: (initialValueOrOptions as QueuerOptions<TValue>)
|
|
78
|
-
const selector = hasInitialValue
|
|
79
|
-
? maybeSelector
|
|
80
|
-
: (initialOptionsOrSelector as
|
|
81
|
-
((state: QueuerState<TValue>) => TSelected) | undefined)
|
|
82
|
-
|
|
83
|
-
const linkedValue = linkedSignal(() => value())
|
|
84
|
-
const queuedValue = signal<TValue>(initialValue)
|
|
85
|
-
|
|
86
|
-
const queued = injectQueuedSignal(
|
|
87
|
-
(item) => {
|
|
88
|
-
queuedValue.set(item)
|
|
89
|
-
},
|
|
90
|
-
initialOptions,
|
|
91
|
-
selector,
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
effect(() => {
|
|
95
|
-
queued.addItem(linkedValue())
|
|
96
|
-
})
|
|
97
|
-
|
|
98
|
-
return queued
|
|
99
|
-
}
|
|
@@ -1,118 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { Queuer } from '@tanstack/pacer/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 { QueuerOptions, QueuerState } from '@tanstack/pacer/queuer'
|
|
8
|
-
|
|
9
|
-
export interface AngularQueuerOptions<
|
|
10
|
-
TValue,
|
|
11
|
-
TSelected = {},
|
|
12
|
-
> extends QueuerOptions<TValue> {
|
|
13
|
-
/**
|
|
14
|
-
* Optional callback invoked when the component is destroyed. Receives the queuer instance.
|
|
15
|
-
* When provided, replaces the default cleanup (stop); use it to call flush(), stop(), add logging, etc.
|
|
16
|
-
*/
|
|
17
|
-
onUnmount?: (queuer: AngularQueuer<TValue, TSelected>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AngularQueuer<TValue, TSelected = {}> extends Omit<
|
|
21
|
-
Queuer<TValue>,
|
|
22
|
-
'store'
|
|
23
|
-
> {
|
|
24
|
-
/**
|
|
25
|
-
* Reactive state signal that will be updated when the queuer state changes
|
|
26
|
-
*
|
|
27
|
-
* Use this instead of `queuer.store.state`
|
|
28
|
-
*/
|
|
29
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
30
|
-
/**
|
|
31
|
-
* @deprecated Use `queuer.state` instead of `queuer.store.state` if you want to read reactive state.
|
|
32
|
-
* The state on the store object is not reactive in Angular signals.
|
|
33
|
-
*/
|
|
34
|
-
readonly store: Store<Readonly<QueuerState<TValue>>>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* An Angular function that creates and manages a Queuer instance.
|
|
39
|
-
*
|
|
40
|
-
* This is a lower-level function that provides direct access to the Queuer's functionality.
|
|
41
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
42
|
-
*
|
|
43
|
-
* The Queuer processes items synchronously in order, with optional delays between processing each item.
|
|
44
|
-
* The queuer includes an internal tick mechanism that can be started and stopped, making it useful as a scheduler.
|
|
45
|
-
*
|
|
46
|
-
* ## State Management and Selector
|
|
47
|
-
*
|
|
48
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
49
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
50
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
51
|
-
*
|
|
52
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
53
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
54
|
-
* full control over when your component tracks state changes.
|
|
55
|
-
*
|
|
56
|
-
* @example
|
|
57
|
-
* ```ts
|
|
58
|
-
* // Default behavior - no reactive state subscriptions
|
|
59
|
-
* const queue = injectQueuer(
|
|
60
|
-
* (item) => console.log('Processing:', item),
|
|
61
|
-
* { started: true, wait: 1000 }
|
|
62
|
-
* );
|
|
63
|
-
*
|
|
64
|
-
* // Opt-in to track queue contents changes
|
|
65
|
-
* const queue = injectQueuer(
|
|
66
|
-
* (item) => console.log('Processing:', item),
|
|
67
|
-
* { started: true, wait: 1000 },
|
|
68
|
-
* (state) => ({ items: state.items, size: state.size })
|
|
69
|
-
* );
|
|
70
|
-
*
|
|
71
|
-
* // Add items
|
|
72
|
-
* queue.addItem('task1');
|
|
73
|
-
*
|
|
74
|
-
* // Access the selected state
|
|
75
|
-
* const { items, isRunning } = queue.state();
|
|
76
|
-
* ```
|
|
77
|
-
*
|
|
78
|
-
* ## Cleanup on Destroy
|
|
79
|
-
*
|
|
80
|
-
* By default, the function stops the queuer when the component is destroyed.
|
|
81
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending items instead:
|
|
82
|
-
*
|
|
83
|
-
* ```ts
|
|
84
|
-
* const queue = injectQueuer(fn, {
|
|
85
|
-
* started: true,
|
|
86
|
-
* onUnmount: (q) => q.flush()
|
|
87
|
-
* });
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
export function injectQueuer<TValue, TSelected = {}>(
|
|
91
|
-
fn: (item: TValue) => void,
|
|
92
|
-
options: AngularQueuerOptions<TValue, TSelected> = {},
|
|
93
|
-
selector: (state: QueuerState<TValue>) => TSelected = () => ({}) as TSelected,
|
|
94
|
-
): AngularQueuer<TValue, TSelected> {
|
|
95
|
-
const mergedOptions = {
|
|
96
|
-
...injectPacerOptions().queuer,
|
|
97
|
-
...options,
|
|
98
|
-
} as AngularQueuerOptions<TValue, TSelected>
|
|
99
|
-
|
|
100
|
-
const queuer = new Queuer<TValue>(fn, mergedOptions)
|
|
101
|
-
const state = injectSelector(queuer.store, selector)
|
|
102
|
-
|
|
103
|
-
const result = {
|
|
104
|
-
...queuer,
|
|
105
|
-
state,
|
|
106
|
-
} as AngularQueuer<TValue, TSelected>
|
|
107
|
-
|
|
108
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
109
|
-
destroyRef?.onDestroy(() => {
|
|
110
|
-
if (mergedOptions.onUnmount) {
|
|
111
|
-
mergedOptions.onUnmount(result)
|
|
112
|
-
} else {
|
|
113
|
-
queuer.stop()
|
|
114
|
-
}
|
|
115
|
-
})
|
|
116
|
-
|
|
117
|
-
return result
|
|
118
|
-
}
|