@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,103 +0,0 @@
|
|
|
1
|
-
import { Signal } from "@angular/core";
|
|
2
|
-
import { Store } from "@tanstack/angular-store";
|
|
3
|
-
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { RateLimiter, RateLimiterOptions, RateLimiterState } from "@tanstack/pacer/rate-limiter";
|
|
5
|
-
|
|
6
|
-
//#region src/rate-limiter/injectRateLimiter.d.ts
|
|
7
|
-
interface AngularRateLimiterOptions<TFn extends AnyFunction, TSelected = {}> extends RateLimiterOptions<TFn> {
|
|
8
|
-
/**
|
|
9
|
-
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
10
|
-
*/
|
|
11
|
-
onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void;
|
|
12
|
-
}
|
|
13
|
-
interface AngularRateLimiter<TFn extends AnyFunction, TSelected = {}> extends Omit<RateLimiter<TFn>, 'store'> {
|
|
14
|
-
/**
|
|
15
|
-
* Reactive state signal that will be updated when the rate limiter state changes
|
|
16
|
-
*
|
|
17
|
-
* Use this instead of `rateLimiter.store.state`
|
|
18
|
-
*/
|
|
19
|
-
readonly state: Signal<Readonly<TSelected>>;
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.
|
|
22
|
-
* The state on the store object is not reactive in Angular signals.
|
|
23
|
-
*/
|
|
24
|
-
readonly store: Store<Readonly<RateLimiterState>>;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* An Angular function that creates and manages a RateLimiter instance.
|
|
28
|
-
*
|
|
29
|
-
* This is a lower-level function that provides direct access to the RateLimiter's functionality.
|
|
30
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
31
|
-
*
|
|
32
|
-
* Rate limiting is a simple "hard limit" approach that allows executions until a maximum count is reached within
|
|
33
|
-
* a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,
|
|
34
|
-
* it does not attempt to space out or collapse executions intelligently.
|
|
35
|
-
*
|
|
36
|
-
* The rate limiter supports two types of windows:
|
|
37
|
-
* - 'fixed': A strict window that resets after the window period. All executions within the window count
|
|
38
|
-
* towards the limit, and the window resets completely after the period.
|
|
39
|
-
* - 'sliding': A rolling window that allows executions as old ones expire. This provides a more
|
|
40
|
-
* consistent rate of execution over time.
|
|
41
|
-
*
|
|
42
|
-
* For smoother execution patterns:
|
|
43
|
-
* - Use throttling when you want consistent spacing between executions (e.g. UI updates)
|
|
44
|
-
* - Use debouncing when you want to collapse rapid-fire events (e.g. search input)
|
|
45
|
-
* - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)
|
|
46
|
-
*
|
|
47
|
-
* ## State Management and Selector
|
|
48
|
-
*
|
|
49
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
50
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
51
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
52
|
-
*
|
|
53
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
54
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
55
|
-
* full control over when your component tracks state changes.
|
|
56
|
-
*
|
|
57
|
-
* Available state properties:
|
|
58
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
59
|
-
* - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations
|
|
60
|
-
* - `rejectionCount`: Number of function executions that have been rejected due to rate limiting
|
|
61
|
-
*
|
|
62
|
-
* ## Cleanup on Destroy
|
|
63
|
-
*
|
|
64
|
-
* Use the `onUnmount` option to run a callback when the component is destroyed.
|
|
65
|
-
*
|
|
66
|
-
* @example
|
|
67
|
-
* ```ts
|
|
68
|
-
* // Default behavior - no reactive state subscriptions
|
|
69
|
-
* const rateLimiter = injectRateLimiter(apiCall, {
|
|
70
|
-
* limit: 5,
|
|
71
|
-
* window: 60000,
|
|
72
|
-
* windowType: 'sliding',
|
|
73
|
-
* });
|
|
74
|
-
*
|
|
75
|
-
* // Opt-in to track execution count changes
|
|
76
|
-
* const rateLimiter = injectRateLimiter(
|
|
77
|
-
* apiCall,
|
|
78
|
-
* {
|
|
79
|
-
* limit: 5,
|
|
80
|
-
* window: 60000,
|
|
81
|
-
* windowType: 'sliding',
|
|
82
|
-
* },
|
|
83
|
-
* (state) => ({ executionCount: state.executionCount })
|
|
84
|
-
* );
|
|
85
|
-
*
|
|
86
|
-
* // Monitor rate limit status
|
|
87
|
-
* const handleClick = () => {
|
|
88
|
-
* const remaining = rateLimiter.getRemainingInWindow();
|
|
89
|
-
* if (remaining > 0) {
|
|
90
|
-
* rateLimiter.maybeExecute(data);
|
|
91
|
-
* } else {
|
|
92
|
-
* showRateLimitWarning();
|
|
93
|
-
* }
|
|
94
|
-
* };
|
|
95
|
-
*
|
|
96
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
97
|
-
* const { executionCount, rejectionCount } = rateLimiter.state();
|
|
98
|
-
* ```
|
|
99
|
-
*/
|
|
100
|
-
declare function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularRateLimiterOptions<TFn, TSelected>, selector?: (state: RateLimiterState) => TSelected): AngularRateLimiter<TFn, TSelected>;
|
|
101
|
-
//#endregion
|
|
102
|
-
export { AngularRateLimiter, AngularRateLimiterOptions, injectRateLimiter };
|
|
103
|
-
//# sourceMappingURL=injectRateLimiter.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectRateLimiter.js","names":[],"sources":["../../src/rate-limiter/injectRateLimiter.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectSelector } from '@tanstack/angular-store'\nimport { RateLimiter } from '@tanstack/pacer/rate-limiter'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n RateLimiterOptions,\n RateLimiterState,\n} from '@tanstack/pacer/rate-limiter'\n\nexport interface AngularRateLimiterOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends RateLimiterOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the rate limiter instance.\n */\n onUnmount?: (rateLimiter: AngularRateLimiter<TFn, TSelected>) => void\n}\n\nexport interface AngularRateLimiter<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<RateLimiter<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the rate limiter state changes\n *\n * Use this instead of `rateLimiter.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `rateLimiter.state` instead of `rateLimiter.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<RateLimiterState>>\n}\n\n/**\n * An Angular function that creates and manages a RateLimiter instance.\n *\n * This is a lower-level function that provides direct access to the RateLimiter's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * Rate limiting is a simple \"hard limit\" approach that allows executions until a maximum count is reached within\n * a time window, then blocks all subsequent calls until the window resets. Unlike throttling or debouncing,\n * it does not attempt to space out or collapse executions intelligently.\n *\n * The rate limiter supports two types of windows:\n * - 'fixed': A strict window that resets after the window period. All executions within the window count\n * towards the limit, and the window resets completely after the period.\n * - 'sliding': A rolling window that allows executions as old ones expire. This provides a more\n * consistent rate of execution over time.\n *\n * For smoother execution patterns:\n * - Use throttling when you want consistent spacing between executions (e.g. UI updates)\n * - Use debouncing when you want to collapse rapid-fire events (e.g. search input)\n * - Use rate limiting only when you need to enforce hard limits (e.g. API rate limits)\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `executionCount`: Number of function executions that have been completed\n * - `executionTimes`: Array of timestamps when executions occurred for rate limiting calculations\n * - `rejectionCount`: Number of function executions that have been rejected due to rate limiting\n *\n * ## Cleanup on Destroy\n *\n * Use the `onUnmount` option to run a callback when the component is destroyed.\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const rateLimiter = injectRateLimiter(apiCall, {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * });\n *\n * // Opt-in to track execution count changes\n * const rateLimiter = injectRateLimiter(\n * apiCall,\n * {\n * limit: 5,\n * window: 60000,\n * windowType: 'sliding',\n * },\n * (state) => ({ executionCount: state.executionCount })\n * );\n *\n * // Monitor rate limit status\n * const handleClick = () => {\n * const remaining = rateLimiter.getRemainingInWindow();\n * if (remaining > 0) {\n * rateLimiter.maybeExecute(data);\n * } else {\n * showRateLimitWarning();\n * }\n * };\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { executionCount, rejectionCount } = rateLimiter.state();\n * ```\n */\nexport function injectRateLimiter<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: AngularRateLimiterOptions<TFn, TSelected>,\n selector: (state: RateLimiterState) => TSelected = () => ({}) as TSelected,\n): AngularRateLimiter<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().rateLimiter,\n ...options,\n } as AngularRateLimiterOptions<TFn, TSelected>\n\n const rateLimiter = new RateLimiter<TFn>(fn, mergedOptions)\n const state = injectSelector(rateLimiter.store, selector)\n\n const result = {\n ...rateLimiter,\n state,\n } as AngularRateLimiter<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiHA,SAAgB,kBACd,IACA,SACA,kBAA0D,EAAE,GACxB;CACpC,MAAM,gBAAgB;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,cAAc,IAAI,YAAiB,IAAI,cAAc;CAC3D,MAAM,QAAQ,eAAe,YAAY,OAAO,SAAS;CAEzD,MAAM,SAAS;EACb,GAAG;EACH;EACD;CAGD,AADmB,OAAO,YAAY,EAAE,UAAU,MAAM,CAC9C,EAAE,gBAAgB;EAC1B,IAAI,cAAc,WAChB,cAAc,UAAU,OAAO;GAEjC;CAEF,OAAO"}
|
package/dist/throttler/index.cjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
2
|
-
const require_injectThrottler = require('./injectThrottler.cjs');
|
|
3
|
-
const require_injectThrottledCallback = require('./injectThrottledCallback.cjs');
|
|
4
|
-
const require_injectThrottledSignal = require('./injectThrottledSignal.cjs');
|
|
5
|
-
const require_injectThrottledValue = require('./injectThrottledValue.cjs');
|
|
6
|
-
|
|
7
|
-
exports.injectThrottledCallback = require_injectThrottledCallback.injectThrottledCallback;
|
|
8
|
-
exports.injectThrottledSignal = require_injectThrottledSignal.injectThrottledSignal;
|
|
9
|
-
exports.injectThrottledValue = require_injectThrottledValue.injectThrottledValue;
|
|
10
|
-
exports.injectThrottler = require_injectThrottler.injectThrottler;
|
|
11
|
-
var _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
12
|
-
Object.keys(_tanstack_pacer_throttler).forEach(function (k) {
|
|
13
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
14
|
-
enumerable: true,
|
|
15
|
-
get: function () { return _tanstack_pacer_throttler[k]; }
|
|
16
|
-
});
|
|
17
|
-
});
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { injectThrottledCallback } from "./injectThrottledCallback.cjs";
|
|
2
|
-
import { AngularThrottler, AngularThrottlerOptions, injectThrottler } from "./injectThrottler.cjs";
|
|
3
|
-
import { ThrottledSignal, injectThrottledSignal } from "./injectThrottledSignal.cjs";
|
|
4
|
-
import { injectThrottledValue } from "./injectThrottledValue.cjs";
|
|
5
|
-
export * from "@tanstack/pacer/throttler";
|
|
6
|
-
export { AngularThrottler, AngularThrottlerOptions, ThrottledSignal, injectThrottledCallback, injectThrottledSignal, injectThrottledValue, injectThrottler };
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const require_injectThrottler = require('./injectThrottler.cjs');
|
|
2
|
-
|
|
3
|
-
//#region src/throttler/injectThrottledCallback.ts
|
|
4
|
-
/**
|
|
5
|
-
* An Angular function that creates a throttled version of a callback function.
|
|
6
|
-
* This function is essentially a wrapper around `injectThrottler` that provides
|
|
7
|
-
* a simplified API for basic throttling needs.
|
|
8
|
-
*
|
|
9
|
-
* The throttled function will execute at most once within the specified wait time.
|
|
10
|
-
* If called multiple times within the wait period, only the first call (if leading is enabled)
|
|
11
|
-
* or the last call (if trailing is enabled) will execute.
|
|
12
|
-
*
|
|
13
|
-
* This function provides a simpler API compared to `injectThrottler`, making it ideal for basic
|
|
14
|
-
* throttling needs. However, it does not expose the underlying Throttler instance.
|
|
15
|
-
*
|
|
16
|
-
* For advanced usage requiring features like:
|
|
17
|
-
* - Manual cancellation
|
|
18
|
-
* - Access to execution counts
|
|
19
|
-
* - State tracking
|
|
20
|
-
*
|
|
21
|
-
* Consider using the `injectThrottler` function instead.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* // Throttle a scroll handler
|
|
26
|
-
* const handleScroll = injectThrottledCallback((scrollY: number) => {
|
|
27
|
-
* updateScrollPosition(scrollY);
|
|
28
|
-
* }, {
|
|
29
|
-
* wait: 100 // Execute at most once per 100ms
|
|
30
|
-
* });
|
|
31
|
-
*
|
|
32
|
-
* // Use in an event listener
|
|
33
|
-
* window.addEventListener('scroll', () => {
|
|
34
|
-
* handleScroll(window.scrollY);
|
|
35
|
-
* });
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
function injectThrottledCallback(fn, options) {
|
|
39
|
-
const throttler = require_injectThrottler.injectThrottler(fn, options);
|
|
40
|
-
return (...args) => throttler.maybeExecute(...args);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
//#endregion
|
|
44
|
-
exports.injectThrottledCallback = injectThrottledCallback;
|
|
45
|
-
//# sourceMappingURL=injectThrottledCallback.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledCallback.cjs","names":["injectThrottler"],"sources":["../../src/throttler/injectThrottledCallback.ts"],"sourcesContent":["import { injectThrottler } from './injectThrottler'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * An Angular function that creates a throttled version of a callback function.\n * This function is essentially a wrapper around `injectThrottler` that provides\n * a simplified API for basic throttling needs.\n *\n * The throttled function will execute at most once within the specified wait time.\n * If called multiple times within the wait period, only the first call (if leading is enabled)\n * or the last call (if trailing is enabled) will execute.\n *\n * This function provides a simpler API compared to `injectThrottler`, making it ideal for basic\n * throttling needs. However, it does not expose the underlying Throttler instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - State tracking\n *\n * Consider using the `injectThrottler` function instead.\n *\n * @example\n * ```ts\n * // Throttle a scroll handler\n * const handleScroll = injectThrottledCallback((scrollY: number) => {\n * updateScrollPosition(scrollY);\n * }, {\n * wait: 100 // Execute at most once per 100ms\n * });\n *\n * // Use in an event listener\n * window.addEventListener('scroll', () => {\n * handleScroll(window.scrollY);\n * });\n * ```\n */\nexport function injectThrottledCallback<TFn extends AnyFunction>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const throttler = injectThrottler(fn, options)\n return (...args: Parameters<TFn>) => throttler.maybeExecute(...args)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,wBACd,IACA,SACoC;CACpC,MAAM,YAAYA,wCAAgB,IAAI,QAAQ;CAC9C,QAAQ,GAAG,SAA0B,UAAU,aAAa,GAAG,KAAK"}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { AnyFunction } from "@tanstack/pacer/types";
|
|
2
|
-
import { ThrottlerOptions } from "@tanstack/pacer/throttler";
|
|
3
|
-
|
|
4
|
-
//#region src/throttler/injectThrottledCallback.d.ts
|
|
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
|
-
declare function injectThrottledCallback<TFn extends AnyFunction>(fn: TFn, options: ThrottlerOptions<TFn>): (...args: Parameters<TFn>) => void;
|
|
40
|
-
//#endregion
|
|
41
|
-
export { injectThrottledCallback };
|
|
42
|
-
//# sourceMappingURL=injectThrottledCallback.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledCallback.js","names":[],"sources":["../../src/throttler/injectThrottledCallback.ts"],"sourcesContent":["import { injectThrottler } from './injectThrottler'\nimport type { ThrottlerOptions } from '@tanstack/pacer/throttler'\nimport type { AnyFunction } from '@tanstack/pacer/types'\n\n/**\n * An Angular function that creates a throttled version of a callback function.\n * This function is essentially a wrapper around `injectThrottler` that provides\n * a simplified API for basic throttling needs.\n *\n * The throttled function will execute at most once within the specified wait time.\n * If called multiple times within the wait period, only the first call (if leading is enabled)\n * or the last call (if trailing is enabled) will execute.\n *\n * This function provides a simpler API compared to `injectThrottler`, making it ideal for basic\n * throttling needs. However, it does not expose the underlying Throttler instance.\n *\n * For advanced usage requiring features like:\n * - Manual cancellation\n * - Access to execution counts\n * - State tracking\n *\n * Consider using the `injectThrottler` function instead.\n *\n * @example\n * ```ts\n * // Throttle a scroll handler\n * const handleScroll = injectThrottledCallback((scrollY: number) => {\n * updateScrollPosition(scrollY);\n * }, {\n * wait: 100 // Execute at most once per 100ms\n * });\n *\n * // Use in an event listener\n * window.addEventListener('scroll', () => {\n * handleScroll(window.scrollY);\n * });\n * ```\n */\nexport function injectThrottledCallback<TFn extends AnyFunction>(\n fn: TFn,\n options: ThrottlerOptions<TFn>,\n): (...args: Parameters<TFn>) => void {\n const throttler = injectThrottler(fn, options)\n return (...args: Parameters<TFn>) => throttler.maybeExecute(...args)\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsCA,SAAgB,wBACd,IACA,SACoC;CACpC,MAAM,YAAY,gBAAgB,IAAI,QAAQ;CAC9C,QAAQ,GAAG,SAA0B,UAAU,aAAa,GAAG,KAAK"}
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
const require_injectThrottler = require('./injectThrottler.cjs');
|
|
2
|
-
let _angular_core = require("@angular/core");
|
|
3
|
-
|
|
4
|
-
//#region src/throttler/injectThrottledSignal.ts
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.
|
|
7
|
-
* This function provides both the current throttled value and methods to update it.
|
|
8
|
-
*
|
|
9
|
-
* The state value is updated at most once within the specified wait time.
|
|
10
|
-
* This is useful for handling frequent state updates that should be rate-limited, like scroll positions
|
|
11
|
-
* or mouse movements.
|
|
12
|
-
*
|
|
13
|
-
* The function returns a callable object:
|
|
14
|
-
* - `throttled()`: Get the current throttled value
|
|
15
|
-
* - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)
|
|
16
|
-
* - `throttled.throttler`: The throttler instance with additional control methods and state signals
|
|
17
|
-
*
|
|
18
|
-
* ## State Management and Selector
|
|
19
|
-
*
|
|
20
|
-
* The function uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
21
|
-
* The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,
|
|
22
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
23
|
-
*
|
|
24
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
25
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
26
|
-
* full control over when your component tracks state changes.
|
|
27
|
-
*
|
|
28
|
-
* Available throttler state properties:
|
|
29
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
30
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
31
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
32
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
33
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
34
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
35
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
36
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* const throttledScrollY = injectThrottledSignal(0, { wait: 100 })
|
|
41
|
-
*
|
|
42
|
-
* // Get value
|
|
43
|
-
* console.log(throttledScrollY())
|
|
44
|
-
*
|
|
45
|
-
* // Set/update value (throttled)
|
|
46
|
-
* throttledScrollY.set(window.scrollY)
|
|
47
|
-
*
|
|
48
|
-
* // Access throttler
|
|
49
|
-
* console.log(throttledScrollY.throttler.state().isPending)
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
function injectThrottledSignal(value, initialOptions, selector) {
|
|
53
|
-
const throttledValue = (0, _angular_core.signal)(value);
|
|
54
|
-
const throttler = require_injectThrottler.injectThrottler((newValue) => {
|
|
55
|
-
if (typeof newValue === "function") throttledValue.update(newValue);
|
|
56
|
-
else throttledValue.set(newValue);
|
|
57
|
-
}, initialOptions, selector);
|
|
58
|
-
const set = (newValue) => {
|
|
59
|
-
throttler.maybeExecute(newValue);
|
|
60
|
-
};
|
|
61
|
-
return Object.assign(() => throttledValue(), {
|
|
62
|
-
set,
|
|
63
|
-
throttler
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
//#endregion
|
|
68
|
-
exports.injectThrottledSignal = injectThrottledSignal;
|
|
69
|
-
//# sourceMappingURL=injectThrottledSignal.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledSignal.cjs","names":["injectThrottler"],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface ThrottledSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n throttler: AngularThrottler<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.\n * This function provides both the current throttled value and methods to update it.\n *\n * The state value is updated at most once within the specified wait time.\n * This is useful for handling frequent state updates that should be rate-limited, like scroll positions\n * or mouse movements.\n *\n * The function returns a callable object:\n * - `throttled()`: Get the current throttled value\n * - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)\n * - `throttled.throttler`: The throttler instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```ts\n * const throttledScrollY = injectThrottledSignal(0, { wait: 100 })\n *\n * // Get value\n * console.log(throttledScrollY())\n *\n * // Set/update value (throttled)\n * throttledScrollY.set(window.scrollY)\n *\n * // Access throttler\n * console.log(throttledScrollY.throttler.state().isPending)\n * ```\n */\nexport function injectThrottledSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const throttledValue = signal<TValue>(value)\n\n const throttler = injectThrottler(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n throttledValue.update(newValue as (prev: TValue) => TValue)\n } else {\n throttledValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n throttler.maybeExecute(newValue)\n }\n\n const throttled = Object.assign(() => throttledValue(), {\n set,\n throttler,\n }) as ThrottledSignal<TValue, TSelected>\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,sBACd,OACA,gBACA,UACoC;CACpC,MAAM,2CAAgC,MAAM;CAE5C,MAAM,YAAYA,yCACf,aAAkD;EACjD,IAAI,OAAO,aAAa,YACtB,eAAe,OAAO,SAAqC;OAE3D,eAAe,IAAI,SAAS;IAGhC,gBACA,SACD;CAED,MAAM,OACJ,aACG;EACH,UAAU,aAAa,SAAS;;CAQlC,OALkB,OAAO,aAAa,gBAAgB,EAAE;EACtD;EACA;EACD,CAEe"}
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { AngularThrottler } from "./injectThrottler.cjs";
|
|
2
|
-
import { ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
3
|
-
|
|
4
|
-
//#region src/throttler/injectThrottledSignal.d.ts
|
|
5
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
6
|
-
interface ThrottledSignal<TValue, TSelected = {}> {
|
|
7
|
-
(): TValue;
|
|
8
|
-
set: Setter<TValue>;
|
|
9
|
-
throttler: AngularThrottler<Setter<TValue>, TSelected>;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.
|
|
13
|
-
* This function provides both the current throttled value and methods to update it.
|
|
14
|
-
*
|
|
15
|
-
* The state value is updated at most once within the specified wait time.
|
|
16
|
-
* This is useful for handling frequent state updates that should be rate-limited, like scroll positions
|
|
17
|
-
* or mouse movements.
|
|
18
|
-
*
|
|
19
|
-
* The function returns a callable object:
|
|
20
|
-
* - `throttled()`: Get the current throttled value
|
|
21
|
-
* - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)
|
|
22
|
-
* - `throttled.throttler`: The throttler instance with additional control methods and state signals
|
|
23
|
-
*
|
|
24
|
-
* ## State Management and Selector
|
|
25
|
-
*
|
|
26
|
-
* The function uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
27
|
-
* The `selector` parameter allows you to specify which throttler 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
|
-
* Available throttler state properties:
|
|
35
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
36
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
37
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
38
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
39
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
40
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
41
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
42
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
43
|
-
*
|
|
44
|
-
* @example
|
|
45
|
-
* ```ts
|
|
46
|
-
* const throttledScrollY = injectThrottledSignal(0, { wait: 100 })
|
|
47
|
-
*
|
|
48
|
-
* // Get value
|
|
49
|
-
* console.log(throttledScrollY())
|
|
50
|
-
*
|
|
51
|
-
* // Set/update value (throttled)
|
|
52
|
-
* throttledScrollY.set(window.scrollY)
|
|
53
|
-
*
|
|
54
|
-
* // Access throttler
|
|
55
|
-
* console.log(throttledScrollY.throttler.state().isPending)
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
declare function injectThrottledSignal<TValue, TSelected = {}>(value: TValue, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): ThrottledSignal<TValue, TSelected>;
|
|
59
|
-
//#endregion
|
|
60
|
-
export { ThrottledSignal, injectThrottledSignal };
|
|
61
|
-
//# sourceMappingURL=injectThrottledSignal.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledSignal.js","names":[],"sources":["../../src/throttler/injectThrottledSignal.ts"],"sourcesContent":["import { signal } from '@angular/core'\nimport { injectThrottler } from './injectThrottler'\nimport type { AngularThrottler } from './injectThrottler'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\nexport interface ThrottledSignal<TValue, TSelected = {}> {\n (): TValue\n set: Setter<TValue>\n throttler: AngularThrottler<Setter<TValue>, TSelected>\n}\n\n/**\n * An Angular function that creates a throttled state signal, combining Angular's signal with throttling functionality.\n * This function provides both the current throttled value and methods to update it.\n *\n * The state value is updated at most once within the specified wait time.\n * This is useful for handling frequent state updates that should be rate-limited, like scroll positions\n * or mouse movements.\n *\n * The function returns a callable object:\n * - `throttled()`: Get the current throttled value\n * - `throttled.set(...)`: Set or update the throttled value (throttled via maybeExecute)\n * - `throttled.throttler`: The throttler instance with additional control methods and state signals\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```ts\n * const throttledScrollY = injectThrottledSignal(0, { wait: 100 })\n *\n * // Get value\n * console.log(throttledScrollY())\n *\n * // Set/update value (throttled)\n * throttledScrollY.set(window.scrollY)\n *\n * // Access throttler\n * console.log(throttledScrollY.throttler.state().isPending)\n * ```\n */\nexport function injectThrottledSignal<TValue, TSelected = {}>(\n value: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const throttledValue = signal<TValue>(value)\n\n const throttler = injectThrottler(\n (newValue: TValue | ((prev: TValue) => TValue)) => {\n if (typeof newValue === 'function') {\n throttledValue.update(newValue as (prev: TValue) => TValue)\n } else {\n throttledValue.set(newValue)\n }\n },\n initialOptions,\n selector,\n )\n\n const set: Setter<TValue> = (\n newValue: TValue | ((prev: TValue) => TValue),\n ) => {\n throttler.maybeExecute(newValue)\n }\n\n const throttled = Object.assign(() => throttledValue(), {\n set,\n throttler,\n }) as ThrottledSignal<TValue, TSelected>\n\n return throttled\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+DA,SAAgB,sBACd,OACA,gBACA,UACoC;CACpC,MAAM,iBAAiB,OAAe,MAAM;CAE5C,MAAM,YAAY,iBACf,aAAkD;EACjD,IAAI,OAAO,aAAa,YACtB,eAAe,OAAO,SAAqC;OAE3D,eAAe,IAAI,SAAS;IAGhC,gBACA,SACD;CAED,MAAM,OACJ,aACG;EACH,UAAU,aAAa,SAAS;;CAQlC,OALkB,OAAO,aAAa,gBAAgB,EAAE;EACtD;EACA;EACD,CAEe"}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
const require_injectThrottledSignal = require('./injectThrottledSignal.cjs');
|
|
2
|
-
let _angular_core = require("@angular/core");
|
|
3
|
-
|
|
4
|
-
//#region src/throttler/injectThrottledValue.ts
|
|
5
|
-
function injectThrottledValue(value, initialValueOrOptions, initialOptionsOrSelector, maybeSelector) {
|
|
6
|
-
const hasInitialValue = initialOptionsOrSelector !== void 0 && !(typeof initialOptionsOrSelector === "function") || maybeSelector !== void 0;
|
|
7
|
-
const throttledValue = require_injectThrottledSignal.injectThrottledSignal(hasInitialValue ? initialValueOrOptions : void 0, hasInitialValue ? initialOptionsOrSelector : initialValueOrOptions, hasInitialValue ? maybeSelector : initialOptionsOrSelector);
|
|
8
|
-
(0, _angular_core.effect)(() => {
|
|
9
|
-
const latest = value();
|
|
10
|
-
throttledValue.set(latest);
|
|
11
|
-
});
|
|
12
|
-
return throttledValue;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
exports.injectThrottledValue = injectThrottledValue;
|
|
17
|
-
//# sourceMappingURL=injectThrottledValue.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledValue.cjs","names":["injectThrottledSignal"],"sources":["../../src/throttler/injectThrottledValue.ts"],"sourcesContent":["import { effect } from '@angular/core'\nimport { injectThrottledSignal } from './injectThrottledSignal'\nimport type { ThrottledSignal } from './injectThrottledSignal'\nimport type { Signal } from '@angular/core'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\n/**\n * An Angular function that creates a throttled value that updates at most once within a specified time window.\n * Unlike injectThrottledSignal, this function automatically tracks changes to the input signal\n * and updates the throttled value accordingly.\n *\n * The throttled value will update at most once within the specified wait time, regardless of\n * how frequently the input value changes.\n *\n * This is useful for deriving throttled values from signals that change frequently,\n * like scroll positions or mouse coordinates, where you want to limit how often downstream effects\n * or calculations occur.\n *\n * The function returns a throttled signal object containing:\n * - A Signal that provides the current throttled value\n * - The throttler instance with control methods\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const scrollY = signal(0)\n * const throttledScrollY = injectThrottledValue(scrollY, {\n * wait: 100, // Update at most once per 100ms\n * })\n *\n * // Opt-in to reactive updates when pending state changes\n * const throttledScrollYWithState = injectThrottledValue(\n * scrollY,\n * { wait: 100 },\n * (state) => ({ isPending: state.isPending }),\n * )\n *\n * // Read the throttled signal value\n * effect(() => {\n * updateUI(throttledScrollY())\n * })\n *\n * // Access throttler state via the returned object's state() signal\n * console.log('Is pending:', throttledScrollYWithState.state().isPending)\n *\n * // Control the throttler\n * throttledScrollY.cancel() // Cancel any pending updates\n * ```\n */\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected>\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialValue: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected>\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialValueOrOptions: TValue | ThrottlerOptions<Setter<TValue>>,\n initialOptionsOrSelector?:\n | ThrottlerOptions<Setter<TValue>>\n | ((state: ThrottlerState<Setter<TValue>>) => TSelected),\n maybeSelector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const hasSelector = typeof initialOptionsOrSelector === 'function'\n\n const hasInitialValue =\n (initialOptionsOrSelector !== undefined && !hasSelector) ||\n maybeSelector !== undefined\n\n const initialValue = hasInitialValue\n ? (initialValueOrOptions as TValue)\n : (undefined as unknown as TValue)\n const initialOptions = hasInitialValue\n ? (initialOptionsOrSelector as ThrottlerOptions<Setter<TValue>>)\n : (initialValueOrOptions as ThrottlerOptions<Setter<TValue>>)\n const selector = hasInitialValue\n ? maybeSelector\n : (initialOptionsOrSelector as\n | ((state: ThrottlerState<Setter<TValue>>) => TSelected)\n | undefined)\n\n const throttledValue = injectThrottledSignal(\n initialValue,\n initialOptions,\n selector,\n )\n\n effect(() => {\n const latest = value()\n throttledValue.set(latest)\n })\n\n return throttledValue\n}\n"],"mappings":";;;;AAqFA,SAAgB,qBACd,OACA,uBACA,0BAGA,eACoC;CAGpC,MAAM,kBACH,6BAA6B,UAAa,EAHzB,OAAO,6BAA6B,eAItD,kBAAkB;CAcpB,MAAM,iBAAiBA,oDAZF,kBAChB,wBACA,QACkB,kBAClB,2BACA,uBACY,kBACb,gBACC,yBAQJ;CAED,gCAAa;EACX,MAAM,SAAS,OAAO;EACtB,eAAe,IAAI,OAAO;GAC1B;CAEF,OAAO"}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { ThrottledSignal } from "./injectThrottledSignal.cjs";
|
|
2
|
-
import { Signal } from "@angular/core";
|
|
3
|
-
import { ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
4
|
-
|
|
5
|
-
//#region src/throttler/injectThrottledValue.d.ts
|
|
6
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void;
|
|
7
|
-
/**
|
|
8
|
-
* An Angular function that creates a throttled value that updates at most once within a specified time window.
|
|
9
|
-
* Unlike injectThrottledSignal, this function automatically tracks changes to the input signal
|
|
10
|
-
* and updates the throttled value accordingly.
|
|
11
|
-
*
|
|
12
|
-
* The throttled value will update at most once within the specified wait time, regardless of
|
|
13
|
-
* how frequently the input value changes.
|
|
14
|
-
*
|
|
15
|
-
* This is useful for deriving throttled values from signals that change frequently,
|
|
16
|
-
* like scroll positions or mouse coordinates, where you want to limit how often downstream effects
|
|
17
|
-
* or calculations occur.
|
|
18
|
-
*
|
|
19
|
-
* The function returns a throttled signal object containing:
|
|
20
|
-
* - A Signal that provides the current throttled value
|
|
21
|
-
* - The throttler instance with control methods
|
|
22
|
-
*
|
|
23
|
-
* ## State Management and Selector
|
|
24
|
-
*
|
|
25
|
-
* The function uses TanStack Store for reactive state management via the underlying throttler instance.
|
|
26
|
-
* The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,
|
|
27
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
28
|
-
*
|
|
29
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
30
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
31
|
-
* full control over when your component tracks state changes.
|
|
32
|
-
*
|
|
33
|
-
* Available throttler state properties:
|
|
34
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
35
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
36
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
37
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
38
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
39
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
40
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
41
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```ts
|
|
45
|
-
* // Default behavior - no reactive state subscriptions
|
|
46
|
-
* const scrollY = signal(0)
|
|
47
|
-
* const throttledScrollY = injectThrottledValue(scrollY, {
|
|
48
|
-
* wait: 100, // Update at most once per 100ms
|
|
49
|
-
* })
|
|
50
|
-
*
|
|
51
|
-
* // Opt-in to reactive updates when pending state changes
|
|
52
|
-
* const throttledScrollYWithState = injectThrottledValue(
|
|
53
|
-
* scrollY,
|
|
54
|
-
* { wait: 100 },
|
|
55
|
-
* (state) => ({ isPending: state.isPending }),
|
|
56
|
-
* )
|
|
57
|
-
*
|
|
58
|
-
* // Read the throttled signal value
|
|
59
|
-
* effect(() => {
|
|
60
|
-
* updateUI(throttledScrollY())
|
|
61
|
-
* })
|
|
62
|
-
*
|
|
63
|
-
* // Access throttler state via the returned object's state() signal
|
|
64
|
-
* console.log('Is pending:', throttledScrollYWithState.state().isPending)
|
|
65
|
-
*
|
|
66
|
-
* // Control the throttler
|
|
67
|
-
* throttledScrollY.cancel() // Cancel any pending updates
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
declare function injectThrottledValue<TValue, TSelected = {}>(value: Signal<TValue>, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): ThrottledSignal<TValue, TSelected>;
|
|
71
|
-
declare function injectThrottledValue<TValue, TSelected = {}>(value: Signal<TValue>, initialValue: TValue, initialOptions: ThrottlerOptions<Setter<TValue>>, selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected): ThrottledSignal<TValue, TSelected>;
|
|
72
|
-
//#endregion
|
|
73
|
-
export { injectThrottledValue };
|
|
74
|
-
//# sourceMappingURL=injectThrottledValue.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottledValue.js","names":[],"sources":["../../src/throttler/injectThrottledValue.ts"],"sourcesContent":["import { effect } from '@angular/core'\nimport { injectThrottledSignal } from './injectThrottledSignal'\nimport type { ThrottledSignal } from './injectThrottledSignal'\nimport type { Signal } from '@angular/core'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\ntype Setter<T> = (value: T | ((prev: T) => T)) => void\n\n/**\n * An Angular function that creates a throttled value that updates at most once within a specified time window.\n * Unlike injectThrottledSignal, this function automatically tracks changes to the input signal\n * and updates the throttled value accordingly.\n *\n * The throttled value will update at most once within the specified wait time, regardless of\n * how frequently the input value changes.\n *\n * This is useful for deriving throttled values from signals that change frequently,\n * like scroll positions or mouse coordinates, where you want to limit how often downstream effects\n * or calculations occur.\n *\n * The function returns a throttled signal object containing:\n * - A Signal that provides the current throttled value\n * - The throttler instance with control methods\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for reactive state management via the underlying throttler instance.\n * The `selector` parameter allows you to specify which throttler state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available throttler state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const scrollY = signal(0)\n * const throttledScrollY = injectThrottledValue(scrollY, {\n * wait: 100, // Update at most once per 100ms\n * })\n *\n * // Opt-in to reactive updates when pending state changes\n * const throttledScrollYWithState = injectThrottledValue(\n * scrollY,\n * { wait: 100 },\n * (state) => ({ isPending: state.isPending }),\n * )\n *\n * // Read the throttled signal value\n * effect(() => {\n * updateUI(throttledScrollY())\n * })\n *\n * // Access throttler state via the returned object's state() signal\n * console.log('Is pending:', throttledScrollYWithState.state().isPending)\n *\n * // Control the throttler\n * throttledScrollY.cancel() // Cancel any pending updates\n * ```\n */\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected>\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialValue: TValue,\n initialOptions: ThrottlerOptions<Setter<TValue>>,\n selector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected>\nexport function injectThrottledValue<TValue, TSelected = {}>(\n value: Signal<TValue>,\n initialValueOrOptions: TValue | ThrottlerOptions<Setter<TValue>>,\n initialOptionsOrSelector?:\n | ThrottlerOptions<Setter<TValue>>\n | ((state: ThrottlerState<Setter<TValue>>) => TSelected),\n maybeSelector?: (state: ThrottlerState<Setter<TValue>>) => TSelected,\n): ThrottledSignal<TValue, TSelected> {\n const hasSelector = typeof initialOptionsOrSelector === 'function'\n\n const hasInitialValue =\n (initialOptionsOrSelector !== undefined && !hasSelector) ||\n maybeSelector !== undefined\n\n const initialValue = hasInitialValue\n ? (initialValueOrOptions as TValue)\n : (undefined as unknown as TValue)\n const initialOptions = hasInitialValue\n ? (initialOptionsOrSelector as ThrottlerOptions<Setter<TValue>>)\n : (initialValueOrOptions as ThrottlerOptions<Setter<TValue>>)\n const selector = hasInitialValue\n ? maybeSelector\n : (initialOptionsOrSelector as\n | ((state: ThrottlerState<Setter<TValue>>) => TSelected)\n | undefined)\n\n const throttledValue = injectThrottledSignal(\n initialValue,\n initialOptions,\n selector,\n )\n\n effect(() => {\n const latest = value()\n throttledValue.set(latest)\n })\n\n return throttledValue\n}\n"],"mappings":";;;;AAqFA,SAAgB,qBACd,OACA,uBACA,0BAGA,eACoC;CAGpC,MAAM,kBACH,6BAA6B,UAAa,EAHzB,OAAO,6BAA6B,eAItD,kBAAkB;CAcpB,MAAM,iBAAiB,sBAZF,kBAChB,wBACA,QACkB,kBAClB,2BACA,uBACY,kBACb,gBACC,yBAQJ;CAED,aAAa;EACX,MAAM,SAAS,OAAO;EACtB,eAAe,IAAI,OAAO;GAC1B;CAEF,OAAO"}
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
const require_pacer_context = require('../provider/pacer-context.cjs');
|
|
2
|
-
let _angular_core = require("@angular/core");
|
|
3
|
-
let _tanstack_angular_store = require("@tanstack/angular-store");
|
|
4
|
-
let _tanstack_pacer_throttler = require("@tanstack/pacer/throttler");
|
|
5
|
-
|
|
6
|
-
//#region src/throttler/injectThrottler.ts
|
|
7
|
-
/**
|
|
8
|
-
* An Angular function that creates and manages a Throttler instance.
|
|
9
|
-
*
|
|
10
|
-
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
11
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
12
|
-
*
|
|
13
|
-
* This function provides throttling functionality to limit how often a function can be called,
|
|
14
|
-
* ensuring it executes at most once within a specified time window.
|
|
15
|
-
*
|
|
16
|
-
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
17
|
-
* prevent further executions until the wait period has elapsed.
|
|
18
|
-
*
|
|
19
|
-
* ## State Management and Selector
|
|
20
|
-
*
|
|
21
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
22
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
23
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
24
|
-
*
|
|
25
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
26
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
27
|
-
* full control over when your component tracks state changes.
|
|
28
|
-
*
|
|
29
|
-
* Available state properties:
|
|
30
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
31
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
32
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
33
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
34
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
35
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
36
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
37
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
38
|
-
*
|
|
39
|
-
* ## Cleanup on Destroy
|
|
40
|
-
*
|
|
41
|
-
* By default, the function cancels any pending execution when the component is destroyed.
|
|
42
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
43
|
-
*
|
|
44
|
-
* ```ts
|
|
45
|
-
* const throttler = injectThrottler(fn, {
|
|
46
|
-
* wait: 100,
|
|
47
|
-
* onUnmount: (t) => t.flush()
|
|
48
|
-
* });
|
|
49
|
-
* ```
|
|
50
|
-
*
|
|
51
|
-
* @example
|
|
52
|
-
* ```ts
|
|
53
|
-
* // Default behavior - no reactive state subscriptions
|
|
54
|
-
* const throttler = injectThrottler(
|
|
55
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
56
|
-
* { wait: 100 }
|
|
57
|
-
* );
|
|
58
|
-
*
|
|
59
|
-
* // Opt-in to track isPending changes (optimized for loading states)
|
|
60
|
-
* const throttler = injectThrottler(
|
|
61
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
62
|
-
* { wait: 100 },
|
|
63
|
-
* (state) => ({ isPending: state.isPending })
|
|
64
|
-
* );
|
|
65
|
-
*
|
|
66
|
-
* // In an event handler
|
|
67
|
-
* window.addEventListener('scroll', () => {
|
|
68
|
-
* throttler.maybeExecute(window.scrollY);
|
|
69
|
-
* });
|
|
70
|
-
*
|
|
71
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
72
|
-
* const { isPending } = throttler.state();
|
|
73
|
-
* ```
|
|
74
|
-
*/
|
|
75
|
-
function injectThrottler(fn, options, selector = () => ({})) {
|
|
76
|
-
const mergedOptions = {
|
|
77
|
-
...require_pacer_context.injectPacerOptions().throttler,
|
|
78
|
-
...options
|
|
79
|
-
};
|
|
80
|
-
const throttler = new _tanstack_pacer_throttler.Throttler(fn, mergedOptions);
|
|
81
|
-
const state = (0, _tanstack_angular_store.injectSelector)(throttler.store, selector);
|
|
82
|
-
const result = {
|
|
83
|
-
...throttler,
|
|
84
|
-
state
|
|
85
|
-
};
|
|
86
|
-
(0, _angular_core.inject)(_angular_core.DestroyRef, { optional: true })?.onDestroy(() => {
|
|
87
|
-
if (mergedOptions.onUnmount) mergedOptions.onUnmount(result);
|
|
88
|
-
else throttler.cancel();
|
|
89
|
-
});
|
|
90
|
-
return result;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
//#endregion
|
|
94
|
-
exports.injectThrottler = injectThrottler;
|
|
95
|
-
//# sourceMappingURL=injectThrottler.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottler.cjs","names":["injectPacerOptions","Throttler","DestroyRef"],"sources":["../../src/throttler/injectThrottler.ts"],"sourcesContent":["import { DestroyRef, inject } from '@angular/core'\nimport { injectSelector } from '@tanstack/angular-store'\nimport { Throttler } from '@tanstack/pacer/throttler'\nimport { injectPacerOptions } from '../provider/pacer-context'\nimport type { Signal } from '@angular/core'\nimport type { Store } from '@tanstack/angular-store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\n\nexport interface AngularThrottlerOptions<\n TFn extends AnyFunction,\n TSelected = {},\n> extends ThrottlerOptions<TFn> {\n /**\n * Optional callback invoked when the component is destroyed. Receives the throttler instance.\n * When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.\n */\n onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void\n}\n\nexport interface AngularThrottler<\n TFn extends AnyFunction,\n TSelected = {},\n> extends Omit<Throttler<TFn>, 'store'> {\n /**\n * Reactive state signal that will be updated when the throttler state changes\n *\n * Use this instead of `throttler.store.state`\n */\n readonly state: Signal<Readonly<TSelected>>\n /**\n * @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.\n * The state on the store object is not reactive in Angular signals.\n */\n readonly store: Store<Readonly<ThrottlerState<TFn>>>\n}\n\n/**\n * An Angular function that creates and manages a Throttler instance.\n *\n * This is a lower-level function that provides direct access to the Throttler's functionality.\n * This allows you to integrate it with any state management solution you prefer.\n *\n * This function provides throttling functionality to limit how often a function can be called,\n * ensuring it executes at most once within a specified time window.\n *\n * The throttler will execute the function immediately (if leading is enabled) and then\n * prevent further executions until the wait period has elapsed.\n *\n * ## State Management and Selector\n *\n * The function uses TanStack Store for state management and wraps it with Angular signals.\n * The `selector` parameter allows you to specify which state changes will trigger signal updates,\n * optimizing performance by preventing unnecessary updates when irrelevant state changes occur.\n *\n * **By default, there will be no reactive state subscriptions** and you must opt-in to state\n * tracking by providing a selector function. This prevents unnecessary updates and gives you\n * full control over when your component tracks state changes.\n *\n * Available state properties:\n * - `canLeadingExecute`: Whether the throttler can execute on the leading edge\n * - `canTrailingExecute`: Whether the throttler can execute on the trailing edge\n * - `executionCount`: Number of function executions that have been completed\n * - `isPending`: Whether the throttler is waiting for the timeout to trigger execution\n * - `lastArgs`: The arguments from the most recent call to maybeExecute\n * - `lastExecutionTime`: Timestamp of the last execution\n * - `nextExecutionTime`: Timestamp of the next allowed execution\n * - `status`: Current execution status ('disabled' | 'idle' | 'pending')\n *\n * ## Cleanup on Destroy\n *\n * By default, the function cancels any pending execution when the component is destroyed.\n * Use the `onUnmount` option to customize this. For example, to flush pending work instead:\n *\n * ```ts\n * const throttler = injectThrottler(fn, {\n * wait: 100,\n * onUnmount: (t) => t.flush()\n * });\n * ```\n *\n * @example\n * ```ts\n * // Default behavior - no reactive state subscriptions\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 }\n * );\n *\n * // Opt-in to track isPending changes (optimized for loading states)\n * const throttler = injectThrottler(\n * (scrollY: number) => updateScrollPosition(scrollY),\n * { wait: 100 },\n * (state) => ({ isPending: state.isPending })\n * );\n *\n * // In an event handler\n * window.addEventListener('scroll', () => {\n * throttler.maybeExecute(window.scrollY);\n * });\n *\n * // Access the selected state (will be empty object {} unless selector provided)\n * const { isPending } = throttler.state();\n * ```\n */\nexport function injectThrottler<TFn extends AnyFunction, TSelected = {}>(\n fn: TFn,\n options: AngularThrottlerOptions<TFn, TSelected>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): AngularThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().throttler,\n ...options,\n } as AngularThrottlerOptions<TFn, TSelected>\n\n const throttler = new Throttler<TFn>(fn, mergedOptions)\n const state = injectSelector(throttler.store, selector)\n\n const result = {\n ...throttler,\n state,\n } as AngularThrottler<TFn, TSelected>\n\n const destroyRef = inject(DestroyRef, { optional: true })\n destroyRef?.onDestroy(() => {\n if (mergedOptions.onUnmount) {\n mergedOptions.onUnmount(result)\n } else {\n throttler.cancel()\n }\n })\n\n return result\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4GA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC7B;CAClC,MAAM,gBAAgB;EACpB,GAAGA,0CAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAIC,oCAAe,IAAI,cAAc;CACvD,MAAM,oDAAuB,UAAU,OAAO,SAAS;CAEvD,MAAM,SAAS;EACb,GAAG;EACH;EACD;CAGD,0BAD0BC,0BAAY,EAAE,UAAU,MAAM,CAC9C,EAAE,gBAAgB;EAC1B,IAAI,cAAc,WAChB,cAAc,UAAU,OAAO;OAE/B,UAAU,QAAQ;GAEpB;CAEF,OAAO"}
|