@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,45 +0,0 @@
|
|
|
1
|
-
import { injectRateLimiter } from './injectRateLimiter'
|
|
2
|
-
import type { RateLimiterOptions } from '@tanstack/pacer/rate-limiter'
|
|
3
|
-
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates a rate-limited version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectRateLimiter` that provides
|
|
8
|
-
* a simplified API for basic rate limiting needs.
|
|
9
|
-
*
|
|
10
|
-
* Rate limiting is a simple "hard limit" approach - it allows all calls until the limit
|
|
11
|
-
* is reached, then blocks subsequent calls until the window resets. Unlike throttling
|
|
12
|
-
* or debouncing, it does not attempt to space out or intelligently collapse calls.
|
|
13
|
-
*
|
|
14
|
-
* This function provides a simpler API compared to `injectRateLimiter`, making it ideal for basic
|
|
15
|
-
* rate limiting needs. However, it does not expose the underlying RateLimiter instance.
|
|
16
|
-
*
|
|
17
|
-
* For advanced usage requiring features like:
|
|
18
|
-
* - Manual cancellation/reset
|
|
19
|
-
* - Access to execution counts
|
|
20
|
-
* - State tracking
|
|
21
|
-
*
|
|
22
|
-
* Consider using the `injectRateLimiter` function instead.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* // Rate limit API calls to maximum 5 calls per minute
|
|
27
|
-
* const makeApiCall = injectRateLimitedCallback(
|
|
28
|
-
* (data: ApiData) => {
|
|
29
|
-
* return fetch('/api/endpoint', { method: 'POST', body: JSON.stringify(data) });
|
|
30
|
-
* },
|
|
31
|
-
* {
|
|
32
|
-
* limit: 5,
|
|
33
|
-
* window: 60000, // 1 minute
|
|
34
|
-
* windowType: 'sliding',
|
|
35
|
-
* }
|
|
36
|
-
* );
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export function injectRateLimitedCallback<TFn extends AnyFunction>(
|
|
40
|
-
fn: TFn,
|
|
41
|
-
options: RateLimiterOptions<TFn>,
|
|
42
|
-
): (...args: Parameters<TFn>) => boolean {
|
|
43
|
-
const rateLimiter = injectRateLimiter(fn, options)
|
|
44
|
-
return (...args: Parameters<TFn>) => rateLimiter.maybeExecute(...args)
|
|
45
|
-
}
|
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
import { signal } from '@angular/core'
|
|
2
|
-
import { injectRateLimiter } from './injectRateLimiter'
|
|
3
|
-
import type { AngularRateLimiter } from './injectRateLimiter'
|
|
4
|
-
import type {
|
|
5
|
-
RateLimiterOptions,
|
|
6
|
-
RateLimiterState,
|
|
7
|
-
} from '@tanstack/pacer/rate-limiter'
|
|
8
|
-
|
|
9
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
10
|
-
|
|
11
|
-
export interface RateLimitedSignal<TValue, TSelected = {}> {
|
|
12
|
-
(): TValue
|
|
13
|
-
set: Setter<TValue>
|
|
14
|
-
rateLimiter: AngularRateLimiter<Setter<TValue>, TSelected>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* An Angular function that creates a rate-limited state signal, combining Angular's signal with rate limiting functionality.
|
|
19
|
-
* This function provides both the current rate-limited value and methods to update it.
|
|
20
|
-
*
|
|
21
|
-
* Rate limiting is a simple "hard limit" approach - it allows all updates until the limit is reached, then blocks
|
|
22
|
-
* subsequent updates until the window resets. Unlike throttling or debouncing, it does not attempt to space out
|
|
23
|
-
* or intelligently collapse updates.
|
|
24
|
-
*
|
|
25
|
-
* The function returns a callable object:
|
|
26
|
-
* - `rateLimited()`: Get the current rate-limited value
|
|
27
|
-
* - `rateLimited.set(...)`: Set or update the rate-limited value (rate-limited via maybeExecute)
|
|
28
|
-
* - `rateLimited.rateLimiter`: The rate limiter 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 rate limiter instance.
|
|
33
|
-
* The `selector` parameter allows you to specify which rate limiter 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
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* // Default behavior - no reactive state subscriptions
|
|
43
|
-
* const rateLimited = injectRateLimitedSignal(0, {
|
|
44
|
-
* limit: 5,
|
|
45
|
-
* window: 60000,
|
|
46
|
-
* windowType: 'sliding'
|
|
47
|
-
* });
|
|
48
|
-
*
|
|
49
|
-
* // Opt-in to reactive updates when limit state changes
|
|
50
|
-
* const rateLimited = injectRateLimitedSignal(
|
|
51
|
-
* 0,
|
|
52
|
-
* { limit: 5, window: 60000 },
|
|
53
|
-
* (state) => ({ rejectionCount: state.rejectionCount })
|
|
54
|
-
* );
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
export function injectRateLimitedSignal<TValue, TSelected = {}>(
|
|
58
|
-
value: TValue,
|
|
59
|
-
initialOptions: RateLimiterOptions<Setter<TValue>>,
|
|
60
|
-
selector?: (state: RateLimiterState) => TSelected,
|
|
61
|
-
): RateLimitedSignal<TValue, TSelected> {
|
|
62
|
-
const rateLimitedValue = signal<TValue>(value)
|
|
63
|
-
|
|
64
|
-
const rateLimiter = injectRateLimiter(
|
|
65
|
-
(newValue: TValue | ((prev: TValue) => TValue)) => {
|
|
66
|
-
if (typeof newValue === 'function') {
|
|
67
|
-
rateLimitedValue.update(newValue as (prev: TValue) => TValue)
|
|
68
|
-
} else {
|
|
69
|
-
rateLimitedValue.set(newValue)
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
initialOptions,
|
|
73
|
-
selector,
|
|
74
|
-
)
|
|
75
|
-
|
|
76
|
-
const set: Setter<TValue> = (
|
|
77
|
-
newValue: TValue | ((prev: TValue) => TValue),
|
|
78
|
-
) => {
|
|
79
|
-
rateLimiter.maybeExecute(newValue)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
const rateLimited = Object.assign(() => rateLimitedValue(), {
|
|
83
|
-
set,
|
|
84
|
-
rateLimiter,
|
|
85
|
-
}) as RateLimitedSignal<TValue, TSelected>
|
|
86
|
-
|
|
87
|
-
return rateLimited
|
|
88
|
-
}
|
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
import { effect } from '@angular/core'
|
|
2
|
-
import { injectRateLimitedSignal } from './injectRateLimitedSignal'
|
|
3
|
-
import type { RateLimitedSignal } from './injectRateLimitedSignal'
|
|
4
|
-
import type { Signal } from '@angular/core'
|
|
5
|
-
import type {
|
|
6
|
-
RateLimiterOptions,
|
|
7
|
-
RateLimiterState,
|
|
8
|
-
} from '@tanstack/pacer/rate-limiter'
|
|
9
|
-
|
|
10
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* An Angular function that creates a rate-limited value that updates at most a certain number of times within a time window.
|
|
14
|
-
* Unlike injectRateLimitedSignal, this function automatically tracks changes to the input signal
|
|
15
|
-
* and updates the rate-limited value accordingly.
|
|
16
|
-
*
|
|
17
|
-
* The rate-limited value will update according to the configured rate limit, blocking updates
|
|
18
|
-
* once the limit is reached until the window resets.
|
|
19
|
-
*
|
|
20
|
-
* The function returns a rate-limited signal object containing:
|
|
21
|
-
* - A Signal that provides the current rate-limited value
|
|
22
|
-
* - The rate limiter instance with control methods
|
|
23
|
-
*
|
|
24
|
-
* ## State Management and Selector
|
|
25
|
-
*
|
|
26
|
-
* The function uses TanStack Store for reactive state management via the underlying rate limiter instance.
|
|
27
|
-
* The `selector` parameter allows you to specify which rate limiter state changes will trigger signal updates,
|
|
28
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
29
|
-
*
|
|
30
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
31
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
32
|
-
* full control over when your component tracks state changes.
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* // Default behavior - no reactive state subscriptions
|
|
37
|
-
* const value = signal(0)
|
|
38
|
-
* const rateLimited = injectRateLimitedValue(value, {
|
|
39
|
-
* limit: 5,
|
|
40
|
-
* window: 60000,
|
|
41
|
-
* windowType: 'sliding',
|
|
42
|
-
* })
|
|
43
|
-
*
|
|
44
|
-
* // rateLimited() will update at most 5 times per 60 seconds
|
|
45
|
-
* effect(() => {
|
|
46
|
-
* updateUI(rateLimited())
|
|
47
|
-
* })
|
|
48
|
-
* ```
|
|
49
|
-
*/
|
|
50
|
-
export function injectRateLimitedValue<TValue, TSelected = {}>(
|
|
51
|
-
value: Signal<TValue>,
|
|
52
|
-
initialOptions: RateLimiterOptions<Setter<TValue>>,
|
|
53
|
-
selector?: (state: RateLimiterState) => TSelected,
|
|
54
|
-
): RateLimitedSignal<TValue, TSelected>
|
|
55
|
-
export function injectRateLimitedValue<TValue, TSelected = {}>(
|
|
56
|
-
value: Signal<TValue>,
|
|
57
|
-
initialValue: TValue,
|
|
58
|
-
initialOptions: RateLimiterOptions<Setter<TValue>>,
|
|
59
|
-
selector?: (state: RateLimiterState) => TSelected,
|
|
60
|
-
): RateLimitedSignal<TValue, TSelected>
|
|
61
|
-
export function injectRateLimitedValue<TValue, TSelected = {}>(
|
|
62
|
-
value: Signal<TValue>,
|
|
63
|
-
initialValueOrOptions: TValue | RateLimiterOptions<Setter<TValue>>,
|
|
64
|
-
initialOptionsOrSelector?:
|
|
65
|
-
| RateLimiterOptions<Setter<TValue>>
|
|
66
|
-
| ((state: RateLimiterState) => TSelected),
|
|
67
|
-
maybeSelector?: (state: RateLimiterState) => TSelected,
|
|
68
|
-
): RateLimitedSignal<TValue, TSelected> {
|
|
69
|
-
const hasSelector = typeof initialOptionsOrSelector === 'function'
|
|
70
|
-
|
|
71
|
-
const hasInitialValue =
|
|
72
|
-
(initialOptionsOrSelector !== undefined && !hasSelector) ||
|
|
73
|
-
maybeSelector !== undefined
|
|
74
|
-
|
|
75
|
-
const initialValue = hasInitialValue
|
|
76
|
-
? (initialValueOrOptions as TValue)
|
|
77
|
-
: (undefined as unknown as TValue)
|
|
78
|
-
const initialOptions = hasInitialValue
|
|
79
|
-
? (initialOptionsOrSelector as RateLimiterOptions<Setter<TValue>>)
|
|
80
|
-
: (initialValueOrOptions as RateLimiterOptions<Setter<TValue>>)
|
|
81
|
-
const selector = hasInitialValue
|
|
82
|
-
? maybeSelector
|
|
83
|
-
: (initialOptionsOrSelector as
|
|
84
|
-
((state: RateLimiterState) => TSelected) | undefined)
|
|
85
|
-
|
|
86
|
-
const rateLimited = injectRateLimitedSignal(
|
|
87
|
-
initialValue,
|
|
88
|
-
initialOptions,
|
|
89
|
-
selector,
|
|
90
|
-
)
|
|
91
|
-
|
|
92
|
-
effect(() => {
|
|
93
|
-
const latest = value()
|
|
94
|
-
rateLimited.set(latest)
|
|
95
|
-
})
|
|
96
|
-
|
|
97
|
-
return rateLimited
|
|
98
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { RateLimiter } from '@tanstack/pacer/rate-limiter'
|
|
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
|
-
RateLimiterOptions,
|
|
10
|
-
RateLimiterState,
|
|
11
|
-
} from '@tanstack/pacer/rate-limiter'
|
|
12
|
-
|
|
13
|
-
export interface AngularRateLimiterOptions<
|
|
14
|
-
TFn extends AnyFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends RateLimiterOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
19
|
-
*/
|
|
20
|
-
onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface AngularRateLimiter<
|
|
24
|
-
TFn extends AnyFunction,
|
|
25
|
-
TSelected = {},
|
|
26
|
-
> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
27
|
-
/**
|
|
28
|
-
* Reactive state signal that will be updated when the rate limiter state changes
|
|
29
|
-
*
|
|
30
|
-
* Use this instead of `rateLimiter.store.state`
|
|
31
|
-
*/
|
|
32
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
33
|
-
/**
|
|
34
|
-
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
35
|
-
* The state on the store object is not reactive in Angular signals.
|
|
36
|
-
*/
|
|
37
|
-
readonly store: Store<Readonly<RateLimiterState>>
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* An Angular function that creates and manages a RateLimiter instance.
|
|
42
|
-
*
|
|
43
|
-
* This is a lower-level function that provides direct access to the RateLimiter's functionality.
|
|
44
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
45
|
-
*
|
|
46
|
-
* Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within
|
|
47
|
-
* a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
|
|
48
|
-
* it does not attempt to space out or collapse executions intelligently.
|
|
49
|
-
*
|
|
50
|
-
* The rate limiter supports two types of windows:
|
|
51
|
-
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
52
|
-
* towards the limit, and the window resets completely after the period.
|
|
53
|
-
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
54
|
-
* consistent rate of execution over time.
|
|
55
|
-
*
|
|
56
|
-
* For smoother execution patterns:
|
|
57
|
-
* - Use throttling when you want consistent spacing between executions (e.g. UI updates)
|
|
58
|
-
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
59
|
-
* - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)
|
|
60
|
-
*
|
|
61
|
-
* ## State Management and Selector
|
|
62
|
-
*
|
|
63
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
64
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
65
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
66
|
-
*
|
|
67
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
68
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
69
|
-
* full control over when your component tracks state changes.
|
|
70
|
-
*
|
|
71
|
-
* Available state properties:
|
|
72
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
73
|
-
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
74
|
-
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
75
|
-
*
|
|
76
|
-
* ## Cleanup on Destroy
|
|
77
|
-
*
|
|
78
|
-
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
79
|
-
*
|
|
80
|
-
* @example
|
|
81
|
-
* ```ts
|
|
82
|
-
* // Default behavior - no reactive state subscriptions
|
|
83
|
-
* const rateLimiter = injectRateLimiter(apiCall, {
|
|
84
|
-
* limit: 5,
|
|
85
|
-
* window: 60000,
|
|
86
|
-
* windowType: 'sliding',
|
|
87
|
-
* });
|
|
88
|
-
*
|
|
89
|
-
* // Opt-in to track execution count changes
|
|
90
|
-
* const rateLimiter = injectRateLimiter(
|
|
91
|
-
* apiCall,
|
|
92
|
-
* {
|
|
93
|
-
* limit: 5,
|
|
94
|
-
* window: 60000,
|
|
95
|
-
* windowType: 'sliding',
|
|
96
|
-
* },
|
|
97
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
98
|
-
* );
|
|
99
|
-
*
|
|
100
|
-
* // Monitor rate limit status
|
|
101
|
-
* const handleClick = () => {
|
|
102
|
-
* const remaining = rateLimiter.getRemainingInWindow();
|
|
103
|
-
* if (remaining > 0) {
|
|
104
|
-
* rateLimiter.maybeExecute(data);
|
|
105
|
-
* } else {
|
|
106
|
-
* showRateLimitWarning();
|
|
107
|
-
* }
|
|
108
|
-
* };
|
|
109
|
-
*
|
|
110
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
111
|
-
* const { executionCount, rejectionCount } = rateLimiter.state();
|
|
112
|
-
* ```
|
|
113
|
-
*/
|
|
114
|
-
export function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(
|
|
115
|
-
fn: TFn,
|
|
116
|
-
options: AngularRateLimiterOptions<TFn, TSelected>,
|
|
117
|
-
selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,
|
|
118
|
-
): AngularRateLimiter<TFn, TSelected> {
|
|
119
|
-
const mergedOptions = {
|
|
120
|
-
...injectPacerOptions().rateLimiter,
|
|
121
|
-
...options,
|
|
122
|
-
} as AngularRateLimiterOptions<TFn, TSelected>
|
|
123
|
-
|
|
124
|
-
const rateLimiter = new RateLimiter<TFn>(fn, mergedOptions)
|
|
125
|
-
const state = injectSelector(rateLimiter.store, selector)
|
|
126
|
-
|
|
127
|
-
const result = {
|
|
128
|
-
...rateLimiter,
|
|
129
|
-
state,
|
|
130
|
-
} as AngularRateLimiter<TFn, TSelected>
|
|
131
|
-
|
|
132
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
133
|
-
destroyRef?.onDestroy(() => {
|
|
134
|
-
if (mergedOptions.onUnmount) {
|
|
135
|
-
mergedOptions.onUnmount(result)
|
|
136
|
-
}
|
|
137
|
-
})
|
|
138
|
-
|
|
139
|
-
return result
|
|
140
|
-
}
|
package/src/throttler/index.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { injectThrottler } from './injectThrottler'
|
|
2
|
-
import type { ThrottlerOptions } from '@tanstack/pacer/throttler'
|
|
3
|
-
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates a throttled version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectThrottler` that provides
|
|
8
|
-
* a simplified API for basic throttling needs.
|
|
9
|
-
*
|
|
10
|
-
* The throttled function will execute at most once within the specified wait time.
|
|
11
|
-
* If called multiple times within the wait period, only the first call (if leading is enabled)
|
|
12
|
-
* or the last call (if trailing is enabled) will execute.
|
|
13
|
-
*
|
|
14
|
-
* This function provides a simpler API compared to `injectThrottler`, making it ideal for basic
|
|
15
|
-
* throttling needs. However, it does not expose the underlying Throttler 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 `injectThrottler` function instead.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* // Throttle a scroll handler
|
|
27
|
-
* const handleScroll = injectThrottledCallback((scrollY: number) => {
|
|
28
|
-
* updateScrollPosition(scrollY);
|
|
29
|
-
* }, {
|
|
30
|
-
* wait: 100 // Execute at most once per 100ms
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* // Use in an event listener
|
|
34
|
-
* window.addEventListener('scroll', () => {
|
|
35
|
-
* handleScroll(window.scrollY);
|
|
36
|
-
* });
|
|
37
|
-
* ```
|
|
38
|
-
*/
|
|
39
|
-
export function injectThrottledCallback<TFn extends AnyFunction>(
|
|
40
|
-
fn: TFn,
|
|
41
|
-
options: ThrottlerOptions<TFn>,
|
|
42
|
-
): (...args: Parameters<TFn>) => void {
|
|
43
|
-
const throttler = injectThrottler(fn, options)
|
|
44
|
-
return (...args: Parameters<TFn>) => throttler.maybeExecute(...args)
|
|
45
|
-
}
|
|
@@ -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,123 +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) | undefined)
|
|
110
|
-
|
|
111
|
-
const throttledValue = injectThrottledSignal(
|
|
112
|
-
initialValue,
|
|
113
|
-
initialOptions,
|
|
114
|
-
selector,
|
|
115
|
-
)
|
|
116
|
-
|
|
117
|
-
effect(() => {
|
|
118
|
-
const latest = value()
|
|
119
|
-
throttledValue.set(latest)
|
|
120
|
-
})
|
|
121
|
-
|
|
122
|
-
return throttledValue
|
|
123
|
-
}
|