@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,98 +0,0 @@
|
|
|
1
|
-
import { Signal } from "@angular/core";
|
|
2
|
-
import { Store } from "@tanstack/angular-store";
|
|
3
|
-
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
-
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
5
|
-
|
|
6
|
-
//#region src/throttler/injectThrottler.d.ts
|
|
7
|
-
interface AngularThrottlerOptions<TFn extends AnyFunction, TSelected = {}> extends ThrottlerOptions<TFn> {
|
|
8
|
-
/**
|
|
9
|
-
* Optional callback invoked when the component is destroyed. Receives the throttler instance.
|
|
10
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
11
|
-
*/
|
|
12
|
-
onUnmount?: (throttler: AngularThrottler<TFn, TSelected>) => void;
|
|
13
|
-
}
|
|
14
|
-
interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
15
|
-
/**
|
|
16
|
-
* Reactive state signal that will be updated when the throttler state changes
|
|
17
|
-
*
|
|
18
|
-
* Use this instead of `throttler.store.state`
|
|
19
|
-
*/
|
|
20
|
-
readonly state: Signal<Readonly<TSelected>>;
|
|
21
|
-
/**
|
|
22
|
-
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
23
|
-
* The state on the store object is not reactive in Angular signals.
|
|
24
|
-
*/
|
|
25
|
-
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* An Angular function that creates and manages a Throttler instance.
|
|
29
|
-
*
|
|
30
|
-
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
31
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
32
|
-
*
|
|
33
|
-
* This function provides throttling functionality to limit how often a function can be called,
|
|
34
|
-
* ensuring it executes at most once within a specified time window.
|
|
35
|
-
*
|
|
36
|
-
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
37
|
-
* prevent further executions until the wait period has elapsed.
|
|
38
|
-
*
|
|
39
|
-
* ## State Management and Selector
|
|
40
|
-
*
|
|
41
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
42
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
43
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
44
|
-
*
|
|
45
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
46
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
47
|
-
* full control over when your component tracks state changes.
|
|
48
|
-
*
|
|
49
|
-
* Available state properties:
|
|
50
|
-
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
51
|
-
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
52
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
53
|
-
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
54
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
55
|
-
* - `lastExecutionTime`: Timestamp of the last execution
|
|
56
|
-
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
57
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
58
|
-
*
|
|
59
|
-
* ## Cleanup on Destroy
|
|
60
|
-
*
|
|
61
|
-
* By default, the function cancels any pending execution when the component is destroyed.
|
|
62
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
63
|
-
*
|
|
64
|
-
* ```ts
|
|
65
|
-
* const throttler = injectThrottler(fn, {
|
|
66
|
-
* wait: 100,
|
|
67
|
-
* onUnmount: (t) => t.flush()
|
|
68
|
-
* });
|
|
69
|
-
* ```
|
|
70
|
-
*
|
|
71
|
-
* @example
|
|
72
|
-
* ```ts
|
|
73
|
-
* // Default behavior - no reactive state subscriptions
|
|
74
|
-
* const throttler = injectThrottler(
|
|
75
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
76
|
-
* { wait: 100 }
|
|
77
|
-
* );
|
|
78
|
-
*
|
|
79
|
-
* // Opt-in to track isPending changes (optimized for loading states)
|
|
80
|
-
* const throttler = injectThrottler(
|
|
81
|
-
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
82
|
-
* { wait: 100 },
|
|
83
|
-
* (state) => ({ isPending: state.isPending })
|
|
84
|
-
* );
|
|
85
|
-
*
|
|
86
|
-
* // In an event handler
|
|
87
|
-
* window.addEventListener('scroll', () => {
|
|
88
|
-
* throttler.maybeExecute(window.scrollY);
|
|
89
|
-
* });
|
|
90
|
-
*
|
|
91
|
-
* // Access the selected state (will be empty object {} unless selector provided)
|
|
92
|
-
* const { isPending } = throttler.state();
|
|
93
|
-
* ```
|
|
94
|
-
*/
|
|
95
|
-
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: AngularThrottlerOptions<TFn, TSelected>, selector?: (state: ThrottlerState<TFn>) => TSelected): AngularThrottler<TFn, TSelected>;
|
|
96
|
-
//#endregion
|
|
97
|
-
export { AngularThrottler, AngularThrottlerOptions, injectThrottler };
|
|
98
|
-
//# sourceMappingURL=injectThrottler.d.cts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"injectThrottler.js","names":[],"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,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ;CAED,MAAM,YAAY,IAAI,UAAe,IAAI,cAAc;CACvD,MAAM,QAAQ,eAAe,UAAU,OAAO,SAAS;CAEvD,MAAM,SAAS;EACb,GAAG;EACH;EACD;CAGD,AADmB,OAAO,YAAY,EAAE,UAAU,MAAM,CAC9C,EAAE,gBAAgB;EAC1B,IAAI,cAAc,WAChB,cAAc,UAAU,OAAO;OAE/B,UAAU,QAAQ;GAEpB;CAEF,OAAO"}
|
package/dist/types/index.cjs
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var _tanstack_pacer_types = require("@tanstack/pacer/types");
|
|
4
|
-
Object.keys(_tanstack_pacer_types).forEach(function (k) {
|
|
5
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function () { return _tanstack_pacer_types[k]; }
|
|
8
|
-
});
|
|
9
|
-
});
|
package/dist/types/index.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "@tanstack/pacer/types";
|
package/dist/utils/index.cjs
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var _tanstack_pacer_utils = require("@tanstack/pacer/utils");
|
|
4
|
-
Object.keys(_tanstack_pacer_utils).forEach(function (k) {
|
|
5
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
6
|
-
enumerable: true,
|
|
7
|
-
get: function () { return _tanstack_pacer_utils[k]; }
|
|
8
|
-
});
|
|
9
|
-
});
|
package/dist/utils/index.d.cts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "@tanstack/pacer/utils";
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import { injectAsyncBatcher } from './injectAsyncBatcher'
|
|
2
|
-
import type { AsyncBatcherOptions } from '@tanstack/pacer/async-batcher'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* An Angular function that creates an async batched version of a callback function.
|
|
6
|
-
* This function is essentially a wrapper around `injectAsyncBatcher` that provides
|
|
7
|
-
* a simplified API for basic async batching needs.
|
|
8
|
-
*
|
|
9
|
-
* The batched function will collect items and process them in batches asynchronously based on
|
|
10
|
-
* the configured conditions (maxSize, wait time, etc.).
|
|
11
|
-
*
|
|
12
|
-
* This function provides a simpler API compared to `injectAsyncBatcher`, making it ideal for basic
|
|
13
|
-
* async batching needs. However, it does not expose the underlying AsyncBatcher instance.
|
|
14
|
-
*
|
|
15
|
-
* For advanced usage requiring features like:
|
|
16
|
-
* - Manual flushing
|
|
17
|
-
* - Access to batch state
|
|
18
|
-
* - Error handling callbacks
|
|
19
|
-
* - Retry support
|
|
20
|
-
*
|
|
21
|
-
* Consider using the `injectAsyncBatcher` function instead.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* // Batch async API calls
|
|
26
|
-
* const batchApiCall = injectAsyncBatchedCallback(
|
|
27
|
-
* async (items: Array<Data>) => {
|
|
28
|
-
* const response = await fetch('/api/batch', {
|
|
29
|
-
* method: 'POST',
|
|
30
|
-
* body: JSON.stringify(items)
|
|
31
|
-
* });
|
|
32
|
-
* return response.json();
|
|
33
|
-
* },
|
|
34
|
-
* { maxSize: 10, wait: 1000 }
|
|
35
|
-
* );
|
|
36
|
-
*
|
|
37
|
-
* // Items will be batched and sent together
|
|
38
|
-
* await batchApiCall(data1);
|
|
39
|
-
* await batchApiCall(data2);
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
export function injectAsyncBatchedCallback<TValue>(
|
|
43
|
-
fn: (items: Array<TValue>) => Promise<any>,
|
|
44
|
-
options: AsyncBatcherOptions<TValue>,
|
|
45
|
-
): (item: TValue) => Promise<void> {
|
|
46
|
-
const batcher = injectAsyncBatcher(fn, options)
|
|
47
|
-
return (item: TValue) => batcher.addItem(item)
|
|
48
|
-
}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
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 {
|
|
8
|
-
AsyncBatcherOptions,
|
|
9
|
-
AsyncBatcherState,
|
|
10
|
-
} from '@tanstack/pacer/async-batcher'
|
|
11
|
-
|
|
12
|
-
export interface AngularAsyncBatcherOptions<
|
|
13
|
-
TValue,
|
|
14
|
-
TSelected = {},
|
|
15
|
-
> extends AsyncBatcherOptions<TValue> {
|
|
16
|
-
/**
|
|
17
|
-
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
18
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
19
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (batcher: AngularAsyncBatcher<TValue, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
25
|
-
AsyncBatcher<TValue>,
|
|
26
|
-
'store'
|
|
27
|
-
> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the async batcher state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `batcher.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
36
|
-
* The state on the store object is not reactive in Angular signals.
|
|
37
|
-
*/
|
|
38
|
-
readonly store: Store<Readonly<AsyncBatcherState<TValue>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages an AsyncBatcher instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the AsyncBatcher's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* The AsyncBatcher collects items and processes them in batches asynchronously with support for
|
|
48
|
-
* promise-based processing, error handling, retry capabilities, and abort support.
|
|
49
|
-
*
|
|
50
|
-
* ## State Management and Selector
|
|
51
|
-
*
|
|
52
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
53
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
54
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
55
|
-
*
|
|
56
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
57
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
58
|
-
* full control over when your component tracks state changes.
|
|
59
|
-
*
|
|
60
|
-
* ## Cleanup on Destroy
|
|
61
|
-
*
|
|
62
|
-
* By default, the function cancels any pending batch and aborts in-flight work when the component is destroyed.
|
|
63
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
64
|
-
*
|
|
65
|
-
* ```ts
|
|
66
|
-
* const batcher = injectAsyncBatcher(fn, {
|
|
67
|
-
* maxSize: 10,
|
|
68
|
-
* onUnmount: (b) => b.flush()
|
|
69
|
-
* });
|
|
70
|
-
* ```
|
|
71
|
-
*
|
|
72
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
73
|
-
*
|
|
74
|
-
* @example
|
|
75
|
-
* ```ts
|
|
76
|
-
* // Default behavior - no reactive state subscriptions
|
|
77
|
-
* const batcher = injectAsyncBatcher(
|
|
78
|
-
* async (items: Array<Data>) => {
|
|
79
|
-
* const response = await fetch('/api/batch', {
|
|
80
|
-
* method: 'POST',
|
|
81
|
-
* body: JSON.stringify(items)
|
|
82
|
-
* });
|
|
83
|
-
* return response.json();
|
|
84
|
-
* },
|
|
85
|
-
* { maxSize: 10, wait: 2000 }
|
|
86
|
-
* );
|
|
87
|
-
*
|
|
88
|
-
* // Add items
|
|
89
|
-
* batcher.addItem(data1);
|
|
90
|
-
* batcher.addItem(data2);
|
|
91
|
-
*
|
|
92
|
-
* // Access the selected state
|
|
93
|
-
* const { items, isExecuting } = batcher.state();
|
|
94
|
-
* ```
|
|
95
|
-
*/
|
|
96
|
-
export function injectAsyncBatcher<TValue, TSelected = {}>(
|
|
97
|
-
fn: (items: Array<TValue>) => Promise<any>,
|
|
98
|
-
options: AngularAsyncBatcherOptions<TValue, TSelected> = {},
|
|
99
|
-
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
100
|
-
({}) as TSelected,
|
|
101
|
-
): AngularAsyncBatcher<TValue, TSelected> {
|
|
102
|
-
const mergedOptions = {
|
|
103
|
-
...injectPacerOptions().asyncBatcher,
|
|
104
|
-
...options,
|
|
105
|
-
} as AngularAsyncBatcherOptions<TValue, TSelected>
|
|
106
|
-
|
|
107
|
-
const batcher = new AsyncBatcher<TValue>(fn, mergedOptions)
|
|
108
|
-
const state = injectSelector(batcher.store, selector)
|
|
109
|
-
|
|
110
|
-
const result = {
|
|
111
|
-
...batcher,
|
|
112
|
-
state,
|
|
113
|
-
} as AngularAsyncBatcher<TValue, TSelected>
|
|
114
|
-
|
|
115
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
116
|
-
destroyRef?.onDestroy(() => {
|
|
117
|
-
if (mergedOptions.onUnmount) {
|
|
118
|
-
mergedOptions.onUnmount(result)
|
|
119
|
-
} else {
|
|
120
|
-
batcher.cancel()
|
|
121
|
-
batcher.abort()
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
|
|
125
|
-
return result
|
|
126
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { injectAsyncDebouncer } from './injectAsyncDebouncer'
|
|
2
|
-
import type { AsyncDebouncerOptions } from '@tanstack/pacer/async-debouncer'
|
|
3
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates an async debounced version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectAsyncDebouncer` that provides
|
|
8
|
-
* a simplified API for basic async debouncing needs.
|
|
9
|
-
*
|
|
10
|
-
* The debounced function will only execute after the specified wait time has elapsed
|
|
11
|
-
* since its last invocation. If called again before the wait time expires, the timer
|
|
12
|
-
* resets and starts waiting again.
|
|
13
|
-
*
|
|
14
|
-
* This function provides a simpler API compared to `injectAsyncDebouncer`, making it ideal for basic
|
|
15
|
-
* async debouncing needs. However, it does not expose the underlying AsyncDebouncer instance.
|
|
16
|
-
*
|
|
17
|
-
* For advanced usage requiring features like:
|
|
18
|
-
* - Manual cancellation
|
|
19
|
-
* - Access to execution counts
|
|
20
|
-
* - Error handling callbacks
|
|
21
|
-
* - Retry support
|
|
22
|
-
*
|
|
23
|
-
* Consider using the `injectAsyncDebouncer` function instead.
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* ```ts
|
|
27
|
-
* // Debounce an async search handler
|
|
28
|
-
* const handleSearch = injectAsyncDebouncedCallback(
|
|
29
|
-
* async (query: string) => {
|
|
30
|
-
* const response = await fetch(`/api/search?q=${query}`);
|
|
31
|
-
* return response.json();
|
|
32
|
-
* },
|
|
33
|
-
* { wait: 500 }
|
|
34
|
-
* );
|
|
35
|
-
*
|
|
36
|
-
* // Use in an input
|
|
37
|
-
* const results = await handleSearch(searchQuery);
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function injectAsyncDebouncedCallback<TFn extends AnyAsyncFunction>(
|
|
41
|
-
fn: TFn,
|
|
42
|
-
options: AsyncDebouncerOptions<TFn>,
|
|
43
|
-
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>> | undefined> {
|
|
44
|
-
const debouncer = injectAsyncDebouncer(fn, options)
|
|
45
|
-
return async (...args: Parameters<TFn>) => {
|
|
46
|
-
const result = await debouncer.maybeExecute(...args)
|
|
47
|
-
// Not sure if this is the best way to handle this,
|
|
48
|
-
return result !== undefined ? await result : undefined
|
|
49
|
-
}
|
|
50
|
-
}
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncDebouncer } from '@tanstack/pacer/async-debouncer'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
AsyncDebouncerOptions,
|
|
10
|
-
AsyncDebouncerState,
|
|
11
|
-
} from '@tanstack/pacer/async-debouncer'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncDebouncerOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncDebouncerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the debouncer instance.
|
|
19
|
-
* When provided, replaces the default cleanup (cancel + abort); use it to call flush(), cancel(), add logging, etc.
|
|
20
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
21
|
-
*/
|
|
22
|
-
onUnmount?: (debouncer: AngularAsyncDebouncer<TFn, TSelected>) => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface AngularAsyncDebouncer<
|
|
26
|
-
TFn extends AnyAsyncFunction,
|
|
27
|
-
TSelected = {},
|
|
28
|
-
> extends Omit<AsyncDebouncer<TFn>, 'store'> {
|
|
29
|
-
/**
|
|
30
|
-
* Reactive state signal that will be updated when the async debouncer state changes
|
|
31
|
-
*
|
|
32
|
-
* Use this instead of `debouncer.store.state`
|
|
33
|
-
*/
|
|
34
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use `debouncer.state` instead of `debouncer.store.state` if you want to read reactive state.
|
|
37
|
-
* The state on the store object is not reactive in Angular signals.
|
|
38
|
-
*/
|
|
39
|
-
readonly store: Store<Readonly<AsyncDebouncerState<TFn>>>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* An Angular function that creates and manages an AsyncDebouncer instance.
|
|
44
|
-
*
|
|
45
|
-
* This is a lower-level function that provides direct access to the AsyncDebouncer's functionality.
|
|
46
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
47
|
-
*
|
|
48
|
-
* This function provides async debouncing functionality with promise support, error handling,
|
|
49
|
-
* retry capabilities, and abort support.
|
|
50
|
-
*
|
|
51
|
-
* The debouncer will only execute the function after the specified wait time has elapsed
|
|
52
|
-
* since the last call. If the function is called again before the wait time expires, the
|
|
53
|
-
* timer resets and starts waiting again.
|
|
54
|
-
*
|
|
55
|
-
* ## State Management and Selector
|
|
56
|
-
*
|
|
57
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
58
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
59
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
60
|
-
*
|
|
61
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
62
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
63
|
-
* full control over when your component tracks state changes.
|
|
64
|
-
*
|
|
65
|
-
* Available state properties:
|
|
66
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
67
|
-
* - `errorCount`: Number of function executions that have resulted in errors
|
|
68
|
-
* - `isExecuting`: Whether the debounced function is currently executing asynchronously
|
|
69
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
70
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
71
|
-
* - `lastResult`: The result from the most recent successful function execution
|
|
72
|
-
* - `settleCount`: Number of function executions that have completed (either successfully or with errors)
|
|
73
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending' | 'executing' | 'settled')
|
|
74
|
-
* - `successCount`: Number of function executions that have completed successfully
|
|
75
|
-
*
|
|
76
|
-
* ## Cleanup on Destroy
|
|
77
|
-
*
|
|
78
|
-
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
79
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
80
|
-
*
|
|
81
|
-
* ```ts
|
|
82
|
-
* const debouncer = injectAsyncDebouncer(fn, {
|
|
83
|
-
* wait: 500,
|
|
84
|
-
* onUnmount: (d) => d.flush()
|
|
85
|
-
* });
|
|
86
|
-
* ```
|
|
87
|
-
*
|
|
88
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
89
|
-
*
|
|
90
|
-
* @example
|
|
91
|
-
* ```ts
|
|
92
|
-
* // Default behavior - no reactive state subscriptions
|
|
93
|
-
* const debouncer = injectAsyncDebouncer(
|
|
94
|
-
* async (query: string) => {
|
|
95
|
-
* const response = await fetch(`/api/search?q=${query}`);
|
|
96
|
-
* return response.json();
|
|
97
|
-
* },
|
|
98
|
-
* { wait: 500 }
|
|
99
|
-
* );
|
|
100
|
-
*
|
|
101
|
-
* // Opt-in to track isExecuting changes (optimized for loading states)
|
|
102
|
-
* const debouncer = injectAsyncDebouncer(
|
|
103
|
-
* async (query: string) => fetchSearchResults(query),
|
|
104
|
-
* { wait: 500 },
|
|
105
|
-
* (state) => ({ isExecuting: state.isExecuting, isPending: state.isPending })
|
|
106
|
-
* );
|
|
107
|
-
*
|
|
108
|
-
* // In an event handler
|
|
109
|
-
* const handleChange = async (e: Event) => {
|
|
110
|
-
* const target = e.target as HTMLInputElement;
|
|
111
|
-
* const result = await debouncer.maybeExecute(target.value);
|
|
112
|
-
* console.log('Search results:', result);
|
|
113
|
-
* };
|
|
114
|
-
*
|
|
115
|
-
* // Access the selected state
|
|
116
|
-
* const { isExecuting, errorCount } = debouncer.state();
|
|
117
|
-
* ```
|
|
118
|
-
*/
|
|
119
|
-
export function injectAsyncDebouncer<
|
|
120
|
-
TFn extends AnyAsyncFunction,
|
|
121
|
-
TSelected = {},
|
|
122
|
-
>(
|
|
123
|
-
fn: TFn,
|
|
124
|
-
options: AngularAsyncDebouncerOptions<TFn, TSelected>,
|
|
125
|
-
selector: (state: AsyncDebouncerState<TFn>) => TSelected = () =>
|
|
126
|
-
({}) as TSelected,
|
|
127
|
-
): AngularAsyncDebouncer<TFn, TSelected> {
|
|
128
|
-
const mergedOptions = {
|
|
129
|
-
...injectPacerOptions().asyncDebouncer,
|
|
130
|
-
...options,
|
|
131
|
-
} as AngularAsyncDebouncerOptions<TFn, TSelected>
|
|
132
|
-
|
|
133
|
-
const debouncer = new AsyncDebouncer<TFn>(fn, mergedOptions)
|
|
134
|
-
const state = injectSelector(debouncer.store, selector)
|
|
135
|
-
|
|
136
|
-
const result = {
|
|
137
|
-
...debouncer,
|
|
138
|
-
state,
|
|
139
|
-
} as AngularAsyncDebouncer<TFn, TSelected>
|
|
140
|
-
|
|
141
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
142
|
-
destroyRef?.onDestroy(() => {
|
|
143
|
-
if (mergedOptions.onUnmount) {
|
|
144
|
-
mergedOptions.onUnmount(result)
|
|
145
|
-
} else {
|
|
146
|
-
debouncer.cancel()
|
|
147
|
-
debouncer.abort()
|
|
148
|
-
}
|
|
149
|
-
})
|
|
150
|
-
|
|
151
|
-
return result
|
|
152
|
-
}
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
import { computed } from '@angular/core'
|
|
2
|
-
import { injectAsyncQueuer } from './injectAsyncQueuer'
|
|
3
|
-
import type { AngularAsyncQueuer } from './injectAsyncQueuer'
|
|
4
|
-
import type {
|
|
5
|
-
AsyncQueuerOptions,
|
|
6
|
-
AsyncQueuerState,
|
|
7
|
-
} from '@tanstack/pacer/async-queuer'
|
|
8
|
-
|
|
9
|
-
export interface AsyncQueuedSignal<TValue, TSelected = {}> {
|
|
10
|
-
(): Array<TValue>
|
|
11
|
-
addItem: AngularAsyncQueuer<TValue, TSelected>['addItem']
|
|
12
|
-
queuer: AngularAsyncQueuer<TValue, TSelected>
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* An Angular function that creates an async queuer with managed state, combining Angular's signals with async queuing functionality.
|
|
17
|
-
* This function provides both the current queue state and queue control methods.
|
|
18
|
-
*
|
|
19
|
-
* The queue state is automatically updated whenever items are added, removed, or processed in the queue.
|
|
20
|
-
* All queue operations are reflected in the state array returned by the function.
|
|
21
|
-
*
|
|
22
|
-
* The function returns a callable object:
|
|
23
|
-
* - `queued()`: Get the current queue items as an array
|
|
24
|
-
* - `queued.addItem(...)`: Add an item to the queue
|
|
25
|
-
* - `queued.queue`: The queuer instance with additional control methods
|
|
26
|
-
*
|
|
27
|
-
* @example
|
|
28
|
-
* ```ts
|
|
29
|
-
* // Default behavior - track items
|
|
30
|
-
* const queued = injectAsyncQueuedSignal(
|
|
31
|
-
* async (item) => {
|
|
32
|
-
* const response = await fetch('/api/process', {
|
|
33
|
-
* method: 'POST',
|
|
34
|
-
* body: JSON.stringify(item)
|
|
35
|
-
* });
|
|
36
|
-
* return response.json();
|
|
37
|
-
* },
|
|
38
|
-
* { concurrency: 2, wait: 1000 }
|
|
39
|
-
* );
|
|
40
|
-
*
|
|
41
|
-
* // Add items
|
|
42
|
-
* queued.addItem(data1);
|
|
43
|
-
*
|
|
44
|
-
* // Access items
|
|
45
|
-
* console.log(queued()); // [data1, ...]
|
|
46
|
-
*
|
|
47
|
-
* // Control the queue
|
|
48
|
-
* queued.queuer.start();
|
|
49
|
-
* queued.queuer.stop();
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export function injectAsyncQueuedSignal<
|
|
53
|
-
TValue,
|
|
54
|
-
TSelected extends Pick<AsyncQueuerState<TValue>, 'items'> = Pick<
|
|
55
|
-
AsyncQueuerState<TValue>,
|
|
56
|
-
'items'
|
|
57
|
-
>,
|
|
58
|
-
>(
|
|
59
|
-
fn: (value: TValue) => Promise<any>,
|
|
60
|
-
options: AsyncQueuerOptions<TValue> = {},
|
|
61
|
-
selector: (state: AsyncQueuerState<TValue>) => TSelected = (state) =>
|
|
62
|
-
({ items: state.items }) as TSelected,
|
|
63
|
-
): AsyncQueuedSignal<TValue, TSelected> {
|
|
64
|
-
const queuer = injectAsyncQueuer(fn, options, selector)
|
|
65
|
-
|
|
66
|
-
const items = computed(() => queuer.state().items as Array<TValue>)
|
|
67
|
-
|
|
68
|
-
const queued = Object.assign(items, {
|
|
69
|
-
addItem: queuer.addItem.bind(queuer),
|
|
70
|
-
queuer,
|
|
71
|
-
}) as AsyncQueuedSignal<TValue, TSelected>
|
|
72
|
-
|
|
73
|
-
return queued
|
|
74
|
-
}
|