@tanstack/angular-pacer 0.23.1 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -6
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +2 -5
- package/dist/async-batcher/injectAsyncBatchedCallback.js +1 -2
- package/dist/async-batcher/injectAsyncBatcher.d.ts +4 -7
- package/dist/async-batcher/injectAsyncBatcher.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +2 -5
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +4 -7
- package/dist/async-debouncer/injectAsyncDebouncer.js +1 -2
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +3 -6
- package/dist/async-queuer/injectAsyncQueuedSignal.js +1 -2
- package/dist/async-queuer/injectAsyncQueuer.d.ts +4 -7
- package/dist/async-queuer/injectAsyncQueuer.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +2 -5
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +4 -7
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +1 -2
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +2 -5
- package/dist/async-throttler/injectAsyncThrottledCallback.js +1 -2
- package/dist/async-throttler/injectAsyncThrottler.d.ts +4 -7
- package/dist/async-throttler/injectAsyncThrottler.js +1 -2
- package/dist/batcher/injectBatchedCallback.d.ts +2 -5
- package/dist/batcher/injectBatchedCallback.js +1 -2
- package/dist/batcher/injectBatcher.d.ts +4 -7
- package/dist/batcher/injectBatcher.js +1 -2
- package/dist/debouncer/injectDebouncedCallback.d.ts +2 -5
- package/dist/debouncer/injectDebouncedCallback.js +1 -2
- package/dist/debouncer/injectDebouncedSignal.d.ts +3 -6
- package/dist/debouncer/injectDebouncedSignal.js +1 -2
- package/dist/debouncer/injectDebouncedValue.d.ts +3 -6
- package/dist/debouncer/injectDebouncedValue.js +1 -2
- package/dist/debouncer/injectDebouncer.d.ts +4 -7
- package/dist/debouncer/injectDebouncer.js +1 -2
- package/dist/provider/pacer-context.d.ts +4 -7
- package/dist/provider/pacer-context.js +1 -2
- package/dist/provider/pacer-provider.d.ts +2 -5
- package/dist/provider/pacer-provider.js +1 -2
- package/dist/queuer/injectQueuedSignal.d.ts +3 -6
- package/dist/queuer/injectQueuedSignal.js +1 -2
- package/dist/queuer/injectQueuedValue.d.ts +3 -6
- package/dist/queuer/injectQueuedValue.js +1 -2
- package/dist/queuer/injectQueuer.d.ts +4 -7
- package/dist/queuer/injectQueuer.js +1 -2
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +2 -5
- package/dist/rate-limiter/injectRateLimitedCallback.js +1 -2
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +3 -6
- package/dist/rate-limiter/injectRateLimitedSignal.js +1 -2
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +3 -6
- package/dist/rate-limiter/injectRateLimitedValue.js +1 -2
- package/dist/rate-limiter/injectRateLimiter.d.ts +4 -7
- package/dist/rate-limiter/injectRateLimiter.js +1 -2
- package/dist/throttler/injectThrottledCallback.d.ts +2 -5
- package/dist/throttler/injectThrottledCallback.js +1 -2
- package/dist/throttler/injectThrottledSignal.d.ts +3 -8
- package/dist/throttler/injectThrottledSignal.js +1 -4
- package/dist/throttler/injectThrottledValue.d.ts +3 -8
- package/dist/throttler/injectThrottledValue.js +1 -2
- package/dist/throttler/injectThrottler.d.ts +4 -9
- package/dist/throttler/injectThrottler.js +1 -4
- package/package.json +25 -72
- package/dist/async-batcher/index.cjs +0 -13
- package/dist/async-batcher/index.d.cts +0 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs +0 -49
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatchedCallback.d.cts +0 -45
- package/dist/async-batcher/injectAsyncBatchedCallback.js.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.cjs +0 -85
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.d.cts +0 -85
- package/dist/async-batcher/injectAsyncBatcher.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -13
- package/dist/async-debouncer/index.d.cts +0 -4
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs +0 -49
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.cts +0 -43
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +0 -107
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +0 -108
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -13
- package/dist/async-queuer/index.d.cts +0 -4
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs +0 -53
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +0 -50
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.cjs +0 -85
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.d.cts +0 -85
- package/dist/async-queuer/injectAsyncQueuer.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -13
- package/dist/async-rate-limiter/index.d.cts +0 -4
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs +0 -52
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.cts +0 -46
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +0 -69
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +0 -72
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +0 -1
- package/dist/async-retryer/index.cjs +0 -9
- package/dist/async-retryer/index.d.cts +0 -1
- package/dist/async-throttler/index.cjs +0 -13
- package/dist/async-throttler/index.d.cts +0 -4
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs +0 -50
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottledCallback.d.cts +0 -44
- package/dist/async-throttler/injectAsyncThrottledCallback.js.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.cjs +0 -86
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.d.cts +0 -87
- package/dist/async-throttler/injectAsyncThrottler.js.map +0 -1
- package/dist/batcher/index.cjs +0 -13
- package/dist/batcher/index.d.cts +0 -4
- package/dist/batcher/injectBatchedCallback.cjs +0 -47
- package/dist/batcher/injectBatchedCallback.cjs.map +0 -1
- package/dist/batcher/injectBatchedCallback.d.cts +0 -43
- package/dist/batcher/injectBatchedCallback.js.map +0 -1
- package/dist/batcher/injectBatcher.cjs +0 -75
- package/dist/batcher/injectBatcher.cjs.map +0 -1
- package/dist/batcher/injectBatcher.d.cts +0 -77
- package/dist/batcher/injectBatcher.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -17
- package/dist/debouncer/index.d.cts +0 -6
- package/dist/debouncer/injectDebouncedCallback.cjs +0 -46
- package/dist/debouncer/injectDebouncedCallback.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedCallback.d.cts +0 -43
- package/dist/debouncer/injectDebouncedCallback.js.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.cjs +0 -68
- package/dist/debouncer/injectDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.d.cts +0 -60
- package/dist/debouncer/injectDebouncedSignal.js.map +0 -1
- package/dist/debouncer/injectDebouncedValue.cjs +0 -17
- package/dist/debouncer/injectDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedValue.d.cts +0 -76
- package/dist/debouncer/injectDebouncedValue.js.map +0 -1
- package/dist/debouncer/injectDebouncer.cjs +0 -95
- package/dist/debouncer/injectDebouncer.cjs.map +0 -1
- package/dist/debouncer/injectDebouncer.d.cts +0 -98
- package/dist/debouncer/injectDebouncer.js.map +0 -1
- package/dist/index.cjs +0 -65
- package/dist/index.d.cts +0 -30
- package/dist/provider/index.cjs +0 -7
- package/dist/provider/index.d.cts +0 -3
- package/dist/provider/pacer-context.cjs +0 -20
- package/dist/provider/pacer-context.cjs.map +0 -1
- package/dist/provider/pacer-context.d.cts +0 -21
- package/dist/provider/pacer-context.js.map +0 -1
- package/dist/provider/pacer-provider.cjs +0 -41
- package/dist/provider/pacer-provider.cjs.map +0 -1
- package/dist/provider/pacer-provider.d.cts +0 -36
- package/dist/provider/pacer-provider.js.map +0 -1
- package/dist/queuer/index.cjs +0 -15
- package/dist/queuer/index.d.cts +0 -5
- package/dist/queuer/injectQueuedSignal.cjs +0 -47
- package/dist/queuer/injectQueuedSignal.cjs.map +0 -1
- package/dist/queuer/injectQueuedSignal.d.cts +0 -44
- package/dist/queuer/injectQueuedSignal.js.map +0 -1
- package/dist/queuer/injectQueuedValue.cjs +0 -23
- package/dist/queuer/injectQueuedValue.cjs.map +0 -1
- package/dist/queuer/injectQueuedValue.d.cts +0 -34
- package/dist/queuer/injectQueuedValue.js.map +0 -1
- package/dist/queuer/injectQueuer.cjs +0 -80
- package/dist/queuer/injectQueuer.cjs.map +0 -1
- package/dist/queuer/injectQueuer.d.cts +0 -82
- package/dist/queuer/injectQueuer.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -17
- package/dist/rate-limiter/index.d.cts +0 -6
- package/dist/rate-limiter/injectRateLimitedCallback.cjs +0 -45
- package/dist/rate-limiter/injectRateLimitedCallback.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedCallback.d.cts +0 -42
- package/dist/rate-limiter/injectRateLimitedCallback.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.cjs +0 -62
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +0 -54
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.cjs +0 -17
- package/dist/rate-limiter/injectRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.d.cts +0 -49
- package/dist/rate-limiter/injectRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.cjs +0 -100
- package/dist/rate-limiter/injectRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.d.cts +0 -103
- package/dist/rate-limiter/injectRateLimiter.js.map +0 -1
- package/dist/throttler/index.cjs +0 -17
- package/dist/throttler/index.d.cts +0 -6
- package/dist/throttler/injectThrottledCallback.cjs +0 -45
- package/dist/throttler/injectThrottledCallback.cjs.map +0 -1
- package/dist/throttler/injectThrottledCallback.d.cts +0 -42
- package/dist/throttler/injectThrottledCallback.js.map +0 -1
- package/dist/throttler/injectThrottledSignal.cjs +0 -69
- package/dist/throttler/injectThrottledSignal.cjs.map +0 -1
- package/dist/throttler/injectThrottledSignal.d.cts +0 -61
- package/dist/throttler/injectThrottledSignal.js.map +0 -1
- package/dist/throttler/injectThrottledValue.cjs +0 -17
- package/dist/throttler/injectThrottledValue.cjs.map +0 -1
- package/dist/throttler/injectThrottledValue.d.cts +0 -74
- package/dist/throttler/injectThrottledValue.js.map +0 -1
- package/dist/throttler/injectThrottler.cjs +0 -95
- package/dist/throttler/injectThrottler.cjs.map +0 -1
- package/dist/throttler/injectThrottler.d.cts +0 -98
- package/dist/throttler/injectThrottler.js.map +0 -1
- package/dist/types/index.cjs +0 -9
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -9
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/index.ts +0 -4
- package/src/async-batcher/injectAsyncBatchedCallback.ts +0 -48
- package/src/async-batcher/injectAsyncBatcher.ts +0 -126
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-debouncer/injectAsyncDebouncedCallback.ts +0 -50
- package/src/async-debouncer/injectAsyncDebouncer.ts +0 -152
- package/src/async-queuer/index.ts +0 -4
- package/src/async-queuer/injectAsyncQueuedSignal.ts +0 -74
- package/src/async-queuer/injectAsyncQueuer.ts +0 -126
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-rate-limiter/injectAsyncRateLimitedCallback.ts +0 -52
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +0 -115
- package/src/async-retryer/index.ts +0 -1
- package/src/async-throttler/index.ts +0 -4
- package/src/async-throttler/injectAsyncThrottledCallback.ts +0 -50
- package/src/async-throttler/injectAsyncThrottler.ts +0 -131
- package/src/batcher/index.ts +0 -4
- package/src/batcher/injectBatchedCallback.ts +0 -46
- package/src/batcher/injectBatcher.ts +0 -114
- package/src/debouncer/index.ts +0 -6
- package/src/debouncer/injectDebouncedCallback.ts +0 -46
- package/src/debouncer/injectDebouncedSignal.ts +0 -94
- package/src/debouncer/injectDebouncedValue.ts +0 -126
- package/src/debouncer/injectDebouncer.ts +0 -137
- package/src/index.ts +0 -40
- package/src/provider/index.ts +0 -2
- package/src/provider/pacer-context.ts +0 -46
- package/src/provider/pacer-provider.ts +0 -38
- package/src/queuer/index.ts +0 -5
- package/src/queuer/injectQueuedSignal.ts +0 -65
- package/src/queuer/injectQueuedValue.ts +0 -101
- package/src/queuer/injectQueuer.ts +0 -118
- package/src/rate-limiter/index.ts +0 -6
- package/src/rate-limiter/injectRateLimitedCallback.ts +0 -45
- package/src/rate-limiter/injectRateLimitedSignal.ts +0 -88
- package/src/rate-limiter/injectRateLimitedValue.ts +0 -99
- package/src/rate-limiter/injectRateLimiter.ts +0 -140
- package/src/throttler/index.ts +0 -6
- package/src/throttler/injectThrottledCallback.ts +0 -45
- package/src/throttler/injectThrottledSignal.ts +0 -95
- package/src/throttler/injectThrottledValue.ts +0 -124
- package/src/throttler/injectThrottler.ts +0 -137
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { signal } from '@angular/core'
|
|
2
|
-
import { injectThrottler } from './injectThrottler'
|
|
3
|
-
import type { AngularThrottler } from './injectThrottler'
|
|
4
|
-
import type {
|
|
5
|
-
ThrottlerOptions,
|
|
6
|
-
ThrottlerState,
|
|
7
|
-
} from '@tanstack/pacer/throttler'
|
|
8
|
-
|
|
9
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
10
|
-
|
|
11
|
-
export interface ThrottledSignal<TValue, TSelected = {}> {
|
|
12
|
-
(): TValue
|
|
13
|
-
set: Setter<TValue>
|
|
14
|
-
throttler: AngularThrottler<Setter<TValue>, TSelected>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.
|
|
19
|
-
* This function provides both the current throttled value and methods to update it.
|
|
20
|
-
*
|
|
21
|
-
* The state value is updated at most once within the specified wait time.
|
|
22
|
-
* This is useful for handling frequent state updates that should be rate-limited, like scroll positions
|
|
23
|
-
* or mouse movements.
|
|
24
|
-
*
|
|
25
|
-
* The function returns a callable object:
|
|
26
|
-
* - `throttled()`: Get the current throttled value
|
|
27
|
-
* - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)
|
|
28
|
-
* - `throttled.throttler`: The throttler instance with additional control methods and state signals
|
|
29
|
-
*
|
|
30
|
-
* ## State Management and Selector
|
|
31
|
-
*
|
|
32
|
-
* The function uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
33
|
-
* The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,
|
|
34
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
35
|
-
*
|
|
36
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
37
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
38
|
-
* full control over when your component tracks state changes.
|
|
39
|
-
*
|
|
40
|
-
* Available throttler state properties:
|
|
41
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
42
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
43
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
44
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
45
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
46
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
47
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
48
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* const throttledScrollY = injectThrottledSignal(0, { wait: 100 })
|
|
53
|
-
*
|
|
54
|
-
* // Get value
|
|
55
|
-
* console.log(throttledScrollY())
|
|
56
|
-
*
|
|
57
|
-
* // Set/update value (throttled)
|
|
58
|
-
* throttledScrollY.set(window.scrollY)
|
|
59
|
-
*
|
|
60
|
-
* // Access throttler
|
|
61
|
-
* console.log(throttledScrollY.throttler.state().isPending)
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
export function injectThrottledSignal<TValue, TSelected = {}>(
|
|
65
|
-
value: TValue,
|
|
66
|
-
initialOptions: ThrottlerOptions<Setter<TValue>>,
|
|
67
|
-
selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,
|
|
68
|
-
): ThrottledSignal<TValue, TSelected> {
|
|
69
|
-
const throttledValue = signal<TValue>(value)
|
|
70
|
-
|
|
71
|
-
const throttler = injectThrottler(
|
|
72
|
-
(newValue: TValue | ((prev: TValue) => TValue)) => {
|
|
73
|
-
if (typeof newValue === 'function') {
|
|
74
|
-
throttledValue.update(newValue as (prev: TValue) => TValue)
|
|
75
|
-
} else {
|
|
76
|
-
throttledValue.set(newValue)
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
initialOptions,
|
|
80
|
-
selector,
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
const set: Setter<TValue> = (
|
|
84
|
-
newValue: TValue | ((prev: TValue) => TValue),
|
|
85
|
-
) => {
|
|
86
|
-
throttler.maybeExecute(newValue)
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
const throttled = Object.assign(() => throttledValue(), {
|
|
90
|
-
set,
|
|
91
|
-
throttler,
|
|
92
|
-
}) as ThrottledSignal<TValue, TSelected>
|
|
93
|
-
|
|
94
|
-
return throttled
|
|
95
|
-
}
|
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
import { effect } from '@angular/core'
|
|
2
|
-
import { injectThrottledSignal } from './injectThrottledSignal'
|
|
3
|
-
import type { ThrottledSignal } from './injectThrottledSignal'
|
|
4
|
-
import type { Signal } from '@angular/core'
|
|
5
|
-
import type {
|
|
6
|
-
ThrottlerOptions,
|
|
7
|
-
ThrottlerState,
|
|
8
|
-
} from '@tanstack/pacer/throttler'
|
|
9
|
-
|
|
10
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* An Angular function that creates a throttled value that updates at most once within a specified time window.
|
|
14
|
-
* Unlike injectThrottledSignal, this function automatically tracks changes to the input signal
|
|
15
|
-
* and updates the throttled value accordingly.
|
|
16
|
-
*
|
|
17
|
-
* The throttled value will update at most once within the specified wait time, regardless of
|
|
18
|
-
* how frequently the input value changes.
|
|
19
|
-
*
|
|
20
|
-
* This is useful for deriving throttled values from signals that change frequently,
|
|
21
|
-
* like scroll positions or mouse coordinates, where you want to limit how often downstream effects
|
|
22
|
-
* or calculations occur.
|
|
23
|
-
*
|
|
24
|
-
* The function returns a throttled signal object containing:
|
|
25
|
-
* - A Signal that provides the current throttled value
|
|
26
|
-
* - The throttler instance with control methods
|
|
27
|
-
*
|
|
28
|
-
* ## State Management and Selector
|
|
29
|
-
*
|
|
30
|
-
* The function uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
31
|
-
* The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,
|
|
32
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
33
|
-
*
|
|
34
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
35
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
36
|
-
* full control over when your component tracks state changes.
|
|
37
|
-
*
|
|
38
|
-
* Available throttler state properties:
|
|
39
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
40
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
41
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
42
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
43
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
44
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
45
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
46
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* // Default behavior - no reactive state subscriptions
|
|
51
|
-
* const scrollY = signal(0)
|
|
52
|
-
* const throttledScrollY = injectThrottledValue(scrollY, {
|
|
53
|
-
* wait: 100, // Update at most once per 100ms
|
|
54
|
-
* })
|
|
55
|
-
*
|
|
56
|
-
* // Opt-in to reactive updates when pending state changes
|
|
57
|
-
* const throttledScrollYWithState = injectThrottledValue(
|
|
58
|
-
* scrollY,
|
|
59
|
-
* { wait: 100 },
|
|
60
|
-
* (state) => ({ isPending: state.isPending }),
|
|
61
|
-
* )
|
|
62
|
-
*
|
|
63
|
-
* // Read the throttled signal value
|
|
64
|
-
* effect(() => {
|
|
65
|
-
* updateUI(throttledScrollY())
|
|
66
|
-
* })
|
|
67
|
-
*
|
|
68
|
-
* // Access throttler state via the returned object's state() signal
|
|
69
|
-
* console.log('Is pending:', throttledScrollYWithState.state().isPending)
|
|
70
|
-
*
|
|
71
|
-
* // Control the throttler
|
|
72
|
-
* throttledScrollY.cancel() // Cancel any pending updates
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
export function injectThrottledValue<TValue, TSelected = {}>(
|
|
76
|
-
value: Signal<TValue>,
|
|
77
|
-
initialOptions: ThrottlerOptions<Setter<TValue>>,
|
|
78
|
-
selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,
|
|
79
|
-
): ThrottledSignal<TValue, TSelected>
|
|
80
|
-
export function injectThrottledValue<TValue, TSelected = {}>(
|
|
81
|
-
value: Signal<TValue>,
|
|
82
|
-
initialValue: TValue,
|
|
83
|
-
initialOptions: ThrottlerOptions<Setter<TValue>>,
|
|
84
|
-
selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,
|
|
85
|
-
): ThrottledSignal<TValue, TSelected>
|
|
86
|
-
export function injectThrottledValue<TValue, TSelected = {}>(
|
|
87
|
-
value: Signal<TValue>,
|
|
88
|
-
initialValueOrOptions: TValue | ThrottlerOptions<Setter<TValue>>,
|
|
89
|
-
initialOptionsOrSelector?:
|
|
90
|
-
| ThrottlerOptions<Setter<TValue>>
|
|
91
|
-
| ((state: ThrottlerState<Setter<TValue>>) => TSelected),
|
|
92
|
-
maybeSelector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,
|
|
93
|
-
): ThrottledSignal<TValue, TSelected> {
|
|
94
|
-
const hasSelector = typeof initialOptionsOrSelector === 'function'
|
|
95
|
-
|
|
96
|
-
const hasInitialValue =
|
|
97
|
-
(initialOptionsOrSelector !== undefined && !hasSelector) ||
|
|
98
|
-
maybeSelector !== undefined
|
|
99
|
-
|
|
100
|
-
const initialValue = hasInitialValue
|
|
101
|
-
? (initialValueOrOptions as TValue)
|
|
102
|
-
: (undefined as unknown as TValue)
|
|
103
|
-
const initialOptions = hasInitialValue
|
|
104
|
-
? (initialOptionsOrSelector as ThrottlerOptions<Setter<TValue>>)
|
|
105
|
-
: (initialValueOrOptions as ThrottlerOptions<Setter<TValue>>)
|
|
106
|
-
const selector = hasInitialValue
|
|
107
|
-
? maybeSelector
|
|
108
|
-
: (initialOptionsOrSelector as
|
|
109
|
-
| ((state: ThrottlerState<Setter<TValue>>) => TSelected)
|
|
110
|
-
| undefined)
|
|
111
|
-
|
|
112
|
-
const throttledValue = injectThrottledSignal(
|
|
113
|
-
initialValue,
|
|
114
|
-
initialOptions,
|
|
115
|
-
selector,
|
|
116
|
-
)
|
|
117
|
-
|
|
118
|
-
effect(() => {
|
|
119
|
-
const latest = value()
|
|
120
|
-
throttledValue.set(latest)
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
return throttledValue
|
|
124
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { Throttler } from '@tanstack/pacer/throttler'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
ThrottlerOptions,
|
|
10
|
-
ThrottlerState,
|
|
11
|
-
} from '@tanstack/pacer/throttler'
|
|
12
|
-
|
|
13
|
-
export interface AngularThrottlerOptions<
|
|
14
|
-
TFn extends AnyFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends ThrottlerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularThrottler<
|
|
25
|
-
TFn extends AnyFunction,
|
|
26
|
-
TSelected = {},
|
|
27
|
-
> extends Omit<Throttler<TFn>, 'store'> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the throttler state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `throttler.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `throttler.state` instead of `throttler.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<ThrottlerState<TFn>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages a Throttler instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* This function provides throttling functionality to limit how often a function can be called,
|
|
48
|
-
* ensuring it executes at most once within a specified time window.
|
|
49
|
-
*
|
|
50
|
-
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
51
|
-
* prevent further executions until the wait period has elapsed.
|
|
52
|
-
*
|
|
53
|
-
* ## State Management and Selector
|
|
54
|
-
*
|
|
55
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
56
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
57
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
58
|
-
*
|
|
59
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
60
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
61
|
-
* full control over when your component tracks state changes.
|
|
62
|
-
*
|
|
63
|
-
* Available state properties:
|
|
64
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
65
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
66
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
67
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
68
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
69
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
70
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
71
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
72
|
-
*
|
|
73
|
-
* ## Cleanup on Destroy
|
|
74
|
-
*
|
|
75
|
-
* By default, the function cancels any pending execution when the component is destroyed.
|
|
76
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
77
|
-
*
|
|
78
|
-
* ```ts
|
|
79
|
-
* const throttler = injectThrottler(fn, {
|
|
80
|
-
* wait: 100,
|
|
81
|
-
* onUnmount: (t) => t.flush()
|
|
82
|
-
* });
|
|
83
|
-
* ```
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* ```ts
|
|
87
|
-
* // Default behavior - no reactive state subscriptions
|
|
88
|
-
* const throttler = injectThrottler(
|
|
89
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
90
|
-
* { wait: 100 }
|
|
91
|
-
* );
|
|
92
|
-
*
|
|
93
|
-
* // Opt-in to track isPending changes (optimized for loading states)
|
|
94
|
-
* const throttler = injectThrottler(
|
|
95
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
96
|
-
* { wait: 100 },
|
|
97
|
-
* (state) => ({ isPending: state.isPending })
|
|
98
|
-
* );
|
|
99
|
-
*
|
|
100
|
-
* // In an event handler
|
|
101
|
-
* window.addEventListener('scroll', () => {
|
|
102
|
-
* throttler.maybeExecute(window.scrollY);
|
|
103
|
-
* });
|
|
104
|
-
*
|
|
105
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
106
|
-
* const { isPending } = throttler.state();
|
|
107
|
-
* ```
|
|
108
|
-
*/
|
|
109
|
-
export function injectThrottler<TFn extends AnyFunction, TSelected = {}>(
|
|
110
|
-
fn: TFn,
|
|
111
|
-
options: AngularThrottlerOptions<TFn, TSelected>,
|
|
112
|
-
selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,
|
|
113
|
-
): AngularThrottler<TFn, TSelected> {
|
|
114
|
-
const mergedOptions = {
|
|
115
|
-
...injectPacerOptions().throttler,
|
|
116
|
-
...options,
|
|
117
|
-
} as AngularThrottlerOptions<TFn, TSelected>
|
|
118
|
-
|
|
119
|
-
const throttler = new Throttler<TFn>(fn, mergedOptions)
|
|
120
|
-
const state = injectSelector(throttler.store, selector)
|
|
121
|
-
|
|
122
|
-
const result = {
|
|
123
|
-
...throttler,
|
|
124
|
-
state,
|
|
125
|
-
} as AngularThrottler<TFn, TSelected>
|
|
126
|
-
|
|
127
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
128
|
-
destroyRef?.onDestroy(() => {
|
|
129
|
-
if (mergedOptions.onUnmount) {
|
|
130
|
-
mergedOptions.onUnmount(result)
|
|
131
|
-
} else {
|
|
132
|
-
throttler.cancel()
|
|
133
|
-
}
|
|
134
|
-
})
|
|
135
|
-
|
|
136
|
-
return result
|
|
137
|
-
}
|
package/src/types/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@tanstack/pacer/types'
|
package/src/utils/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@tanstack/pacer/utils'
|