@tanstack/angular-pacer 0.24.0 → 0.25.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +2 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.js +1 -2
- package/dist/async-batcher/injectAsyncBatcher.d.ts +4 -6
- package/dist/async-batcher/injectAsyncBatcher.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +2 -4
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +1 -2
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +4 -6
- package/dist/async-debouncer/injectAsyncDebouncer.js +1 -2
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +3 -5
- package/dist/async-queuer/injectAsyncQueuedSignal.js +1 -2
- package/dist/async-queuer/injectAsyncQueuer.d.ts +4 -6
- package/dist/async-queuer/injectAsyncQueuer.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +2 -4
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +1 -2
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +4 -6
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +1 -2
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +2 -4
- package/dist/async-throttler/injectAsyncThrottledCallback.js +1 -2
- package/dist/async-throttler/injectAsyncThrottler.d.ts +4 -6
- package/dist/async-throttler/injectAsyncThrottler.js +1 -2
- package/dist/batcher/injectBatchedCallback.d.ts +2 -4
- package/dist/batcher/injectBatchedCallback.js +1 -2
- package/dist/batcher/injectBatcher.d.ts +4 -6
- package/dist/batcher/injectBatcher.js +1 -2
- package/dist/debouncer/injectDebouncedCallback.d.ts +2 -4
- package/dist/debouncer/injectDebouncedCallback.js +1 -2
- package/dist/debouncer/injectDebouncedSignal.d.ts +3 -5
- package/dist/debouncer/injectDebouncedSignal.js +1 -2
- package/dist/debouncer/injectDebouncedValue.d.ts +3 -5
- package/dist/debouncer/injectDebouncedValue.js +1 -2
- package/dist/debouncer/injectDebouncer.d.ts +4 -6
- package/dist/debouncer/injectDebouncer.js +1 -2
- package/dist/provider/pacer-context.d.ts +4 -6
- package/dist/provider/pacer-context.js +1 -2
- package/dist/provider/pacer-provider.d.ts +2 -4
- package/dist/provider/pacer-provider.js +1 -2
- package/dist/queuer/injectQueuedSignal.d.ts +3 -5
- package/dist/queuer/injectQueuedSignal.js +1 -2
- package/dist/queuer/injectQueuedValue.d.ts +3 -5
- package/dist/queuer/injectQueuedValue.js +1 -2
- package/dist/queuer/injectQueuer.d.ts +4 -6
- package/dist/queuer/injectQueuer.js +1 -2
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +2 -4
- package/dist/rate-limiter/injectRateLimitedCallback.js +1 -2
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +3 -5
- package/dist/rate-limiter/injectRateLimitedSignal.js +1 -2
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +3 -5
- package/dist/rate-limiter/injectRateLimitedValue.js +1 -2
- package/dist/rate-limiter/injectRateLimiter.d.ts +4 -6
- package/dist/rate-limiter/injectRateLimiter.js +1 -2
- package/dist/throttler/injectThrottledCallback.d.ts +2 -4
- package/dist/throttler/injectThrottledCallback.js +1 -2
- package/dist/throttler/injectThrottledSignal.d.ts +3 -7
- package/dist/throttler/injectThrottledSignal.js +1 -4
- package/dist/throttler/injectThrottledValue.d.ts +3 -7
- package/dist/throttler/injectThrottledValue.js +1 -2
- package/dist/throttler/injectThrottler.d.ts +4 -8
- package/dist/throttler/injectThrottler.js +1 -4
- package/package.json +24 -71
- package/dist/async-batcher/index.cjs +0 -13
- package/dist/async-batcher/index.d.cts +0 -4
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs +0 -49
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatchedCallback.d.cts +0 -44
- package/dist/async-batcher/injectAsyncBatchedCallback.js.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.cjs +0 -85
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +0 -1
- package/dist/async-batcher/injectAsyncBatcher.d.cts +0 -84
- package/dist/async-batcher/injectAsyncBatcher.js.map +0 -1
- package/dist/async-debouncer/index.cjs +0 -13
- package/dist/async-debouncer/index.d.cts +0 -4
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs +0 -49
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.cts +0 -42
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +0 -107
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +0 -1
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +0 -107
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +0 -1
- package/dist/async-queuer/index.cjs +0 -13
- package/dist/async-queuer/index.d.cts +0 -4
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs +0 -53
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +0 -49
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.cjs +0 -85
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +0 -1
- package/dist/async-queuer/injectAsyncQueuer.d.cts +0 -84
- package/dist/async-queuer/injectAsyncQueuer.js.map +0 -1
- package/dist/async-rate-limiter/index.cjs +0 -13
- package/dist/async-rate-limiter/index.d.cts +0 -4
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs +0 -52
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.cts +0 -45
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +0 -69
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +0 -1
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +0 -71
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +0 -1
- package/dist/async-retryer/index.cjs +0 -8
- package/dist/async-retryer/index.d.cts +0 -1
- package/dist/async-throttler/index.cjs +0 -13
- package/dist/async-throttler/index.d.cts +0 -4
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs +0 -50
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottledCallback.d.cts +0 -43
- package/dist/async-throttler/injectAsyncThrottledCallback.js.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.cjs +0 -86
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +0 -1
- package/dist/async-throttler/injectAsyncThrottler.d.cts +0 -86
- package/dist/async-throttler/injectAsyncThrottler.js.map +0 -1
- package/dist/batcher/index.cjs +0 -13
- package/dist/batcher/index.d.cts +0 -4
- package/dist/batcher/injectBatchedCallback.cjs +0 -47
- package/dist/batcher/injectBatchedCallback.cjs.map +0 -1
- package/dist/batcher/injectBatchedCallback.d.cts +0 -42
- package/dist/batcher/injectBatchedCallback.js.map +0 -1
- package/dist/batcher/injectBatcher.cjs +0 -75
- package/dist/batcher/injectBatcher.cjs.map +0 -1
- package/dist/batcher/injectBatcher.d.cts +0 -76
- package/dist/batcher/injectBatcher.js.map +0 -1
- package/dist/debouncer/index.cjs +0 -17
- package/dist/debouncer/index.d.cts +0 -6
- package/dist/debouncer/injectDebouncedCallback.cjs +0 -46
- package/dist/debouncer/injectDebouncedCallback.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedCallback.d.cts +0 -42
- package/dist/debouncer/injectDebouncedCallback.js.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.cjs +0 -68
- package/dist/debouncer/injectDebouncedSignal.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedSignal.d.cts +0 -59
- package/dist/debouncer/injectDebouncedSignal.js.map +0 -1
- package/dist/debouncer/injectDebouncedValue.cjs +0 -17
- package/dist/debouncer/injectDebouncedValue.cjs.map +0 -1
- package/dist/debouncer/injectDebouncedValue.d.cts +0 -75
- package/dist/debouncer/injectDebouncedValue.js.map +0 -1
- package/dist/debouncer/injectDebouncer.cjs +0 -95
- package/dist/debouncer/injectDebouncer.cjs.map +0 -1
- package/dist/debouncer/injectDebouncer.d.cts +0 -97
- package/dist/debouncer/injectDebouncer.js.map +0 -1
- package/dist/index.cjs +0 -65
- package/dist/index.d.cts +0 -30
- package/dist/provider/index.cjs +0 -7
- package/dist/provider/index.d.cts +0 -3
- package/dist/provider/pacer-context.cjs +0 -20
- package/dist/provider/pacer-context.cjs.map +0 -1
- package/dist/provider/pacer-context.d.cts +0 -20
- package/dist/provider/pacer-context.js.map +0 -1
- package/dist/provider/pacer-provider.cjs +0 -41
- package/dist/provider/pacer-provider.cjs.map +0 -1
- package/dist/provider/pacer-provider.d.cts +0 -35
- package/dist/provider/pacer-provider.js.map +0 -1
- package/dist/queuer/index.cjs +0 -15
- package/dist/queuer/index.d.cts +0 -5
- package/dist/queuer/injectQueuedSignal.cjs +0 -47
- package/dist/queuer/injectQueuedSignal.cjs.map +0 -1
- package/dist/queuer/injectQueuedSignal.d.cts +0 -43
- package/dist/queuer/injectQueuedSignal.js.map +0 -1
- package/dist/queuer/injectQueuedValue.cjs +0 -23
- package/dist/queuer/injectQueuedValue.cjs.map +0 -1
- package/dist/queuer/injectQueuedValue.d.cts +0 -33
- package/dist/queuer/injectQueuedValue.js.map +0 -1
- package/dist/queuer/injectQueuer.cjs +0 -80
- package/dist/queuer/injectQueuer.cjs.map +0 -1
- package/dist/queuer/injectQueuer.d.cts +0 -81
- package/dist/queuer/injectQueuer.js.map +0 -1
- package/dist/rate-limiter/index.cjs +0 -17
- package/dist/rate-limiter/index.d.cts +0 -6
- package/dist/rate-limiter/injectRateLimitedCallback.cjs +0 -45
- package/dist/rate-limiter/injectRateLimitedCallback.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedCallback.d.cts +0 -41
- package/dist/rate-limiter/injectRateLimitedCallback.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.cjs +0 -62
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +0 -53
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.cjs +0 -17
- package/dist/rate-limiter/injectRateLimitedValue.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimitedValue.d.cts +0 -48
- package/dist/rate-limiter/injectRateLimitedValue.js.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.cjs +0 -100
- package/dist/rate-limiter/injectRateLimiter.cjs.map +0 -1
- package/dist/rate-limiter/injectRateLimiter.d.cts +0 -102
- package/dist/rate-limiter/injectRateLimiter.js.map +0 -1
- package/dist/throttler/index.cjs +0 -17
- package/dist/throttler/index.d.cts +0 -6
- package/dist/throttler/injectThrottledCallback.cjs +0 -45
- package/dist/throttler/injectThrottledCallback.cjs.map +0 -1
- package/dist/throttler/injectThrottledCallback.d.cts +0 -41
- package/dist/throttler/injectThrottledCallback.js.map +0 -1
- package/dist/throttler/injectThrottledSignal.cjs +0 -69
- package/dist/throttler/injectThrottledSignal.cjs.map +0 -1
- package/dist/throttler/injectThrottledSignal.d.cts +0 -60
- package/dist/throttler/injectThrottledSignal.js.map +0 -1
- package/dist/throttler/injectThrottledValue.cjs +0 -17
- package/dist/throttler/injectThrottledValue.cjs.map +0 -1
- package/dist/throttler/injectThrottledValue.d.cts +0 -73
- package/dist/throttler/injectThrottledValue.js.map +0 -1
- package/dist/throttler/injectThrottler.cjs +0 -95
- package/dist/throttler/injectThrottler.cjs.map +0 -1
- package/dist/throttler/injectThrottler.d.cts +0 -97
- package/dist/throttler/injectThrottler.js.map +0 -1
- package/dist/types/index.cjs +0 -8
- package/dist/types/index.d.cts +0 -1
- package/dist/utils/index.cjs +0 -8
- package/dist/utils/index.d.cts +0 -1
- package/src/async-batcher/index.ts +0 -4
- package/src/async-batcher/injectAsyncBatchedCallback.ts +0 -48
- package/src/async-batcher/injectAsyncBatcher.ts +0 -126
- package/src/async-debouncer/index.ts +0 -4
- package/src/async-debouncer/injectAsyncDebouncedCallback.ts +0 -50
- package/src/async-debouncer/injectAsyncDebouncer.ts +0 -152
- package/src/async-queuer/index.ts +0 -4
- package/src/async-queuer/injectAsyncQueuedSignal.ts +0 -74
- package/src/async-queuer/injectAsyncQueuer.ts +0 -126
- package/src/async-rate-limiter/index.ts +0 -4
- package/src/async-rate-limiter/injectAsyncRateLimitedCallback.ts +0 -52
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +0 -115
- package/src/async-retryer/index.ts +0 -1
- package/src/async-throttler/index.ts +0 -4
- package/src/async-throttler/injectAsyncThrottledCallback.ts +0 -50
- package/src/async-throttler/injectAsyncThrottler.ts +0 -131
- package/src/batcher/index.ts +0 -4
- package/src/batcher/injectBatchedCallback.ts +0 -46
- package/src/batcher/injectBatcher.ts +0 -114
- package/src/debouncer/index.ts +0 -6
- package/src/debouncer/injectDebouncedCallback.ts +0 -46
- package/src/debouncer/injectDebouncedSignal.ts +0 -94
- package/src/debouncer/injectDebouncedValue.ts +0 -125
- package/src/debouncer/injectDebouncer.ts +0 -137
- package/src/index.ts +0 -40
- package/src/provider/index.ts +0 -2
- package/src/provider/pacer-context.ts +0 -46
- package/src/provider/pacer-provider.ts +0 -38
- package/src/queuer/index.ts +0 -5
- package/src/queuer/injectQueuedSignal.ts +0 -65
- package/src/queuer/injectQueuedValue.ts +0 -99
- package/src/queuer/injectQueuer.ts +0 -118
- package/src/rate-limiter/index.ts +0 -6
- package/src/rate-limiter/injectRateLimitedCallback.ts +0 -45
- package/src/rate-limiter/injectRateLimitedSignal.ts +0 -88
- package/src/rate-limiter/injectRateLimitedValue.ts +0 -98
- package/src/rate-limiter/injectRateLimiter.ts +0 -140
- package/src/throttler/index.ts +0 -6
- package/src/throttler/injectThrottledCallback.ts +0 -45
- package/src/throttler/injectThrottledSignal.ts +0 -95
- package/src/throttler/injectThrottledValue.ts +0 -123
- package/src/throttler/injectThrottler.ts +0 -137
- package/src/types/index.ts +0 -1
- package/src/utils/index.ts +0 -1
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncRateLimiter } from '@tanstack/pacer/async-rate-limiter'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
AsyncRateLimiterOptions,
|
|
10
|
-
AsyncRateLimiterState,
|
|
11
|
-
} from '@tanstack/pacer/async-rate-limiter'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncRateLimiterOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncRateLimiterOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the rate limiter instance.
|
|
19
|
-
* When provided, replaces the default cleanup (abort).
|
|
20
|
-
*/
|
|
21
|
-
onUnmount?: (rateLimiter: AngularAsyncRateLimiter<TFn, TSelected>) => void
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface AngularAsyncRateLimiter<
|
|
25
|
-
TFn extends AnyAsyncFunction,
|
|
26
|
-
TSelected = {},
|
|
27
|
-
> extends Omit<AsyncRateLimiter<TFn>, 'store'> {
|
|
28
|
-
/**
|
|
29
|
-
* Reactive state signal that will be updated when the async rate limiter state changes
|
|
30
|
-
*
|
|
31
|
-
* Use this instead of `rateLimiter.store.state`
|
|
32
|
-
*/
|
|
33
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
34
|
-
/**
|
|
35
|
-
* @deprecated Use `rateLimiter.state` instead of `rateLimiter.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<AsyncRateLimiterState<TFn>>>
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* An Angular function that creates and manages an AsyncRateLimiter instance.
|
|
43
|
-
*
|
|
44
|
-
* This is a lower-level function that provides direct access to the AsyncRateLimiter's functionality.
|
|
45
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
46
|
-
*
|
|
47
|
-
* This function provides async rate limiting functionality with promise support, error handling,
|
|
48
|
-
* 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 aborts in-flight work when the component is destroyed.
|
|
63
|
-
* Use the `onUnmount` option to customize this.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* ```ts
|
|
67
|
-
* // Default behavior - no reactive state subscriptions
|
|
68
|
-
* const rateLimiter = injectAsyncRateLimiter(
|
|
69
|
-
* async (id: string) => {
|
|
70
|
-
* const response = await fetch(`/api/data/${id}`);
|
|
71
|
-
* return response.json();
|
|
72
|
-
* },
|
|
73
|
-
* { limit: 5, window: 60000, windowType: 'sliding' }
|
|
74
|
-
* );
|
|
75
|
-
*
|
|
76
|
-
* // In an event handler
|
|
77
|
-
* const handleRequest = async (id: string) => {
|
|
78
|
-
* const result = await rateLimiter.maybeExecute(id);
|
|
79
|
-
* console.log('Result:', result);
|
|
80
|
-
* };
|
|
81
|
-
* ```
|
|
82
|
-
*/
|
|
83
|
-
export function injectAsyncRateLimiter<
|
|
84
|
-
TFn extends AnyAsyncFunction,
|
|
85
|
-
TSelected = {},
|
|
86
|
-
>(
|
|
87
|
-
fn: TFn,
|
|
88
|
-
options: AngularAsyncRateLimiterOptions<TFn, TSelected>,
|
|
89
|
-
selector: (state: AsyncRateLimiterState<TFn>) => TSelected = () =>
|
|
90
|
-
({}) as TSelected,
|
|
91
|
-
): AngularAsyncRateLimiter<TFn, TSelected> {
|
|
92
|
-
const mergedOptions = {
|
|
93
|
-
...injectPacerOptions().asyncRateLimiter,
|
|
94
|
-
...options,
|
|
95
|
-
} as AngularAsyncRateLimiterOptions<TFn, TSelected>
|
|
96
|
-
|
|
97
|
-
const rateLimiter = new AsyncRateLimiter<TFn>(fn, mergedOptions)
|
|
98
|
-
const state = injectSelector(rateLimiter.store, selector)
|
|
99
|
-
|
|
100
|
-
const result = {
|
|
101
|
-
...rateLimiter,
|
|
102
|
-
state,
|
|
103
|
-
} as AngularAsyncRateLimiter<TFn, TSelected>
|
|
104
|
-
|
|
105
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
106
|
-
destroyRef?.onDestroy(() => {
|
|
107
|
-
if (mergedOptions.onUnmount) {
|
|
108
|
-
mergedOptions.onUnmount(result)
|
|
109
|
-
} else {
|
|
110
|
-
rateLimiter.abort()
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
return result
|
|
115
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from '@tanstack/pacer/async-retryer'
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import { injectAsyncThrottler } from './injectAsyncThrottler'
|
|
2
|
-
import type { AsyncThrottlerOptions } from '@tanstack/pacer/async-throttler'
|
|
3
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates an async throttled version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectAsyncThrottler` that provides
|
|
8
|
-
* a simplified API for basic async throttling needs.
|
|
9
|
-
*
|
|
10
|
-
* The throttled function will execute at most once within the specified wait time.
|
|
11
|
-
*
|
|
12
|
-
* This function provides a simpler API compared to `injectAsyncThrottler`, making it ideal for basic
|
|
13
|
-
* async throttling needs. However, it does not expose the underlying AsyncThrottler instance.
|
|
14
|
-
*
|
|
15
|
-
* For advanced usage requiring features like:
|
|
16
|
-
* - Manual cancellation
|
|
17
|
-
* - Access to execution counts
|
|
18
|
-
* - Error handling callbacks
|
|
19
|
-
* - Retry support
|
|
20
|
-
*
|
|
21
|
-
* Consider using the `injectAsyncThrottler` function instead.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* // Throttle an async update handler
|
|
26
|
-
* const handleUpdate = injectAsyncThrottledCallback(
|
|
27
|
-
* async (data: Data) => {
|
|
28
|
-
* const response = await fetch('/api/update', {
|
|
29
|
-
* method: 'POST',
|
|
30
|
-
* body: JSON.stringify(data)
|
|
31
|
-
* });
|
|
32
|
-
* return response.json();
|
|
33
|
-
* },
|
|
34
|
-
* { wait: 1000 }
|
|
35
|
-
* );
|
|
36
|
-
*
|
|
37
|
-
* // Use in an event handler
|
|
38
|
-
* const result = await handleUpdate(updateData);
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
export function injectAsyncThrottledCallback<TFn extends AnyAsyncFunction>(
|
|
42
|
-
fn: TFn,
|
|
43
|
-
options: AsyncThrottlerOptions<TFn>,
|
|
44
|
-
): (...args: Parameters<TFn>) => Promise<Awaited<ReturnType<TFn>> | undefined> {
|
|
45
|
-
const throttler = injectAsyncThrottler(fn, options)
|
|
46
|
-
return async (...args: Parameters<TFn>) => {
|
|
47
|
-
const result = await throttler.maybeExecute(...args)
|
|
48
|
-
return result !== undefined ? await result : undefined
|
|
49
|
-
}
|
|
50
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { AsyncThrottler } from '@tanstack/pacer/async-throttler'
|
|
4
|
-
import { injectPacerOptions } from '../provider/pacer-context'
|
|
5
|
-
import type { Signal } from '@angular/core'
|
|
6
|
-
import type { Store } from '@tanstack/angular-store'
|
|
7
|
-
import type { AnyAsyncFunction } from '@tanstack/pacer/types'
|
|
8
|
-
import type {
|
|
9
|
-
AsyncThrottlerOptions,
|
|
10
|
-
AsyncThrottlerState,
|
|
11
|
-
} from '@tanstack/pacer/async-throttler'
|
|
12
|
-
|
|
13
|
-
export interface AngularAsyncThrottlerOptions<
|
|
14
|
-
TFn extends AnyAsyncFunction,
|
|
15
|
-
TSelected = {},
|
|
16
|
-
> extends AsyncThrottlerOptions<TFn> {
|
|
17
|
-
/**
|
|
18
|
-
* Optional callback invoked when the component is destroyed. Receives the throttler 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?: (throttler: AngularAsyncThrottler<TFn, TSelected>) => void
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export interface AngularAsyncThrottler<
|
|
26
|
-
TFn extends AnyAsyncFunction,
|
|
27
|
-
TSelected = {},
|
|
28
|
-
> extends Omit<AsyncThrottler<TFn>, 'store'> {
|
|
29
|
-
/**
|
|
30
|
-
* Reactive state signal that will be updated when the async throttler state changes
|
|
31
|
-
*
|
|
32
|
-
* Use this instead of `throttler.store.state`
|
|
33
|
-
*/
|
|
34
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
35
|
-
/**
|
|
36
|
-
* @deprecated Use `throttler.state` instead of `throttler.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<AsyncThrottlerState<TFn>>>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* An Angular function that creates and manages an AsyncThrottler instance.
|
|
44
|
-
*
|
|
45
|
-
* This is a lower-level function that provides direct access to the AsyncThrottler's functionality.
|
|
46
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
47
|
-
*
|
|
48
|
-
* This function provides async throttling functionality with promise support, error handling,
|
|
49
|
-
* retry capabilities, and abort support.
|
|
50
|
-
*
|
|
51
|
-
* The throttler will execute the function at most once within the specified wait time.
|
|
52
|
-
*
|
|
53
|
-
* ## State Management and Selector
|
|
54
|
-
*
|
|
55
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
56
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
57
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
58
|
-
*
|
|
59
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
60
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
61
|
-
* full control over when your component tracks state changes.
|
|
62
|
-
*
|
|
63
|
-
* ## Cleanup on Destroy
|
|
64
|
-
*
|
|
65
|
-
* By default, the function cancels any pending execution and aborts in-flight work when the component is destroyed.
|
|
66
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
67
|
-
*
|
|
68
|
-
* ```ts
|
|
69
|
-
* const throttler = injectAsyncThrottler(fn, {
|
|
70
|
-
* wait: 1000,
|
|
71
|
-
* onUnmount: (t) => t.flush()
|
|
72
|
-
* });
|
|
73
|
-
* ```
|
|
74
|
-
*
|
|
75
|
-
* When using onUnmount with flush, guard your callbacks since the component may already be destroyed.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```ts
|
|
79
|
-
* // Default behavior - no reactive state subscriptions
|
|
80
|
-
* const throttler = injectAsyncThrottler(
|
|
81
|
-
* async (data: Data) => {
|
|
82
|
-
* const response = await fetch('/api/update', {
|
|
83
|
-
* method: 'POST',
|
|
84
|
-
* body: JSON.stringify(data)
|
|
85
|
-
* });
|
|
86
|
-
* return response.json();
|
|
87
|
-
* },
|
|
88
|
-
* { wait: 1000 }
|
|
89
|
-
* );
|
|
90
|
-
*
|
|
91
|
-
* // In an event handler
|
|
92
|
-
* const handleUpdate = async (data: Data) => {
|
|
93
|
-
* const result = await throttler.maybeExecute(data);
|
|
94
|
-
* console.log('Update result:', result);
|
|
95
|
-
* };
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
export function injectAsyncThrottler<
|
|
99
|
-
TFn extends AnyAsyncFunction,
|
|
100
|
-
TSelected = {},
|
|
101
|
-
>(
|
|
102
|
-
fn: TFn,
|
|
103
|
-
options: AngularAsyncThrottlerOptions<TFn, TSelected>,
|
|
104
|
-
selector: (state: AsyncThrottlerState<TFn>) => TSelected = () =>
|
|
105
|
-
({}) as TSelected,
|
|
106
|
-
): AngularAsyncThrottler<TFn, TSelected> {
|
|
107
|
-
const mergedOptions = {
|
|
108
|
-
...injectPacerOptions().asyncThrottler,
|
|
109
|
-
...options,
|
|
110
|
-
} as AngularAsyncThrottlerOptions<TFn, TSelected>
|
|
111
|
-
|
|
112
|
-
const throttler = new AsyncThrottler<TFn>(fn, mergedOptions)
|
|
113
|
-
const state = injectSelector(throttler.store, selector)
|
|
114
|
-
|
|
115
|
-
const result = {
|
|
116
|
-
...throttler,
|
|
117
|
-
state,
|
|
118
|
-
} as AngularAsyncThrottler<TFn, TSelected>
|
|
119
|
-
|
|
120
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
121
|
-
destroyRef?.onDestroy(() => {
|
|
122
|
-
if (mergedOptions.onUnmount) {
|
|
123
|
-
mergedOptions.onUnmount(result)
|
|
124
|
-
} else {
|
|
125
|
-
throttler.cancel()
|
|
126
|
-
throttler.abort()
|
|
127
|
-
}
|
|
128
|
-
})
|
|
129
|
-
|
|
130
|
-
return result
|
|
131
|
-
}
|
package/src/batcher/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { injectBatcher } from './injectBatcher'
|
|
2
|
-
import type { BatcherOptions } from '@tanstack/pacer/batcher'
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* An Angular function that creates a batched version of a callback function.
|
|
6
|
-
* This function is essentially a wrapper around `injectBatcher` that provides
|
|
7
|
-
* a simplified API for basic batching needs.
|
|
8
|
-
*
|
|
9
|
-
* The batched function will collect items and process them in batches based on
|
|
10
|
-
* the configured conditions (maxSize, wait time, etc.).
|
|
11
|
-
*
|
|
12
|
-
* This function provides a simpler API compared to `injectBatcher`, making it ideal for basic
|
|
13
|
-
* batching needs. However, it does not expose the underlying Batcher instance.
|
|
14
|
-
*
|
|
15
|
-
* For advanced usage requiring features like:
|
|
16
|
-
* - Manual flushing
|
|
17
|
-
* - Access to batch state
|
|
18
|
-
* - State tracking
|
|
19
|
-
*
|
|
20
|
-
* Consider using the `injectBatcher` function instead.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* ```ts
|
|
24
|
-
* // Batch API calls
|
|
25
|
-
* const batchApiCall = injectBatchedCallback(
|
|
26
|
-
* (items) => {
|
|
27
|
-
* return fetch('/api/batch', {
|
|
28
|
-
* method: 'POST',
|
|
29
|
-
* body: JSON.stringify(items)
|
|
30
|
-
* });
|
|
31
|
-
* },
|
|
32
|
-
* { maxSize: 10, wait: 1000 }
|
|
33
|
-
* );
|
|
34
|
-
*
|
|
35
|
-
* // Items will be batched and sent together
|
|
36
|
-
* batchApiCall('item1');
|
|
37
|
-
* batchApiCall('item2');
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function injectBatchedCallback<TValue>(
|
|
41
|
-
fn: (items: Array<TValue>) => void,
|
|
42
|
-
options: BatcherOptions<TValue>,
|
|
43
|
-
): (item: TValue) => void {
|
|
44
|
-
const batcher = injectBatcher(fn, options)
|
|
45
|
-
return (item: TValue) => batcher.addItem(item)
|
|
46
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { DestroyRef, inject } from '@angular/core'
|
|
2
|
-
import { injectSelector } from '@tanstack/angular-store'
|
|
3
|
-
import { Batcher } from '@tanstack/pacer/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 { BatcherOptions, BatcherState } from '@tanstack/pacer/batcher'
|
|
8
|
-
|
|
9
|
-
export interface AngularBatcherOptions<
|
|
10
|
-
TValue,
|
|
11
|
-
TSelected = {},
|
|
12
|
-
> extends BatcherOptions<TValue> {
|
|
13
|
-
/**
|
|
14
|
-
* Optional callback invoked when the component is destroyed. Receives the batcher instance.
|
|
15
|
-
* When provided, replaces the default cleanup (cancel); use it to call flush(), cancel(), add logging, etc.
|
|
16
|
-
*/
|
|
17
|
-
onUnmount?: (batcher: AngularBatcher<TValue, TSelected>) => void
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export interface AngularBatcher<TValue, TSelected = {}> extends Omit<
|
|
21
|
-
Batcher<TValue>,
|
|
22
|
-
'store'
|
|
23
|
-
> {
|
|
24
|
-
/**
|
|
25
|
-
* Reactive state signal that will be updated when the batcher state changes
|
|
26
|
-
*
|
|
27
|
-
* Use this instead of `batcher.store.state`
|
|
28
|
-
*/
|
|
29
|
-
readonly state: Signal<Readonly<TSelected>>
|
|
30
|
-
/**
|
|
31
|
-
* @deprecated Use `batcher.state` instead of `batcher.store.state` if you want to read reactive state.
|
|
32
|
-
* The state on the store object is not reactive in Angular signals.
|
|
33
|
-
*/
|
|
34
|
-
readonly store: Store<Readonly<BatcherState<TValue>>>
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* An Angular function that creates and manages a Batcher instance.
|
|
39
|
-
*
|
|
40
|
-
* This is a lower-level function that provides direct access to the Batcher's functionality.
|
|
41
|
-
* This allows you to integrate it with any state management solution you prefer.
|
|
42
|
-
*
|
|
43
|
-
* The Batcher collects items and processes them in batches based on configurable conditions:
|
|
44
|
-
* - Maximum batch size
|
|
45
|
-
* - Time-based batching (process after X milliseconds)
|
|
46
|
-
* - Custom batch processing logic via getShouldExecute
|
|
47
|
-
*
|
|
48
|
-
* ## State Management and Selector
|
|
49
|
-
*
|
|
50
|
-
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
51
|
-
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
52
|
-
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
53
|
-
*
|
|
54
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
55
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
56
|
-
* full control over when your component tracks state changes.
|
|
57
|
-
*
|
|
58
|
-
* ## Cleanup on Destroy
|
|
59
|
-
*
|
|
60
|
-
* By default, the function cancels any pending batch when the component is destroyed.
|
|
61
|
-
* Use the `onUnmount` option to customize this. For example, to flush pending work instead:
|
|
62
|
-
*
|
|
63
|
-
* ```ts
|
|
64
|
-
* const batcher = injectBatcher(fn, {
|
|
65
|
-
* maxSize: 5,
|
|
66
|
-
* onUnmount: (b) => b.flush()
|
|
67
|
-
* });
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* @example
|
|
71
|
-
* ```ts
|
|
72
|
-
* // Default behavior - no reactive state subscriptions
|
|
73
|
-
* const batcher = injectBatcher(
|
|
74
|
-
* (items) => console.log('Processing batch:', items),
|
|
75
|
-
* { maxSize: 5, wait: 2000 }
|
|
76
|
-
* );
|
|
77
|
-
*
|
|
78
|
-
* // Add items
|
|
79
|
-
* batcher.addItem('task1');
|
|
80
|
-
*
|
|
81
|
-
* // Access the selected state
|
|
82
|
-
* const { items, isPending } = batcher.state();
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
export function injectBatcher<TValue, TSelected = {}>(
|
|
86
|
-
fn: (items: Array<TValue>) => void,
|
|
87
|
-
options: AngularBatcherOptions<TValue, TSelected> = {},
|
|
88
|
-
selector: (state: BatcherState<TValue>) => TSelected = () =>
|
|
89
|
-
({}) as TSelected,
|
|
90
|
-
): AngularBatcher<TValue, TSelected> {
|
|
91
|
-
const mergedOptions = {
|
|
92
|
-
...injectPacerOptions().batcher,
|
|
93
|
-
...options,
|
|
94
|
-
} as AngularBatcherOptions<TValue, TSelected>
|
|
95
|
-
|
|
96
|
-
const batcher = new Batcher<TValue>(fn, mergedOptions)
|
|
97
|
-
const state = injectSelector(batcher.store, selector)
|
|
98
|
-
|
|
99
|
-
const result = {
|
|
100
|
-
...batcher,
|
|
101
|
-
state,
|
|
102
|
-
} as AngularBatcher<TValue, TSelected>
|
|
103
|
-
|
|
104
|
-
const destroyRef = inject(DestroyRef, { optional: true })
|
|
105
|
-
destroyRef?.onDestroy(() => {
|
|
106
|
-
if (mergedOptions.onUnmount) {
|
|
107
|
-
mergedOptions.onUnmount(result)
|
|
108
|
-
} else {
|
|
109
|
-
batcher.cancel()
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
|
|
113
|
-
return result
|
|
114
|
-
}
|
package/src/debouncer/index.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { injectDebouncer } from './injectDebouncer'
|
|
2
|
-
import type { DebouncerOptions } from '@tanstack/pacer/debouncer'
|
|
3
|
-
import type { AnyFunction } from '@tanstack/pacer/types'
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* An Angular function that creates a debounced version of a callback function.
|
|
7
|
-
* This function is essentially a wrapper around `injectDebouncer` that provides
|
|
8
|
-
* a simplified API for basic 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 `injectDebouncer`, making it ideal for basic
|
|
15
|
-
* debouncing needs. However, it does not expose the underlying Debouncer 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 `injectDebouncer` function instead.
|
|
23
|
-
*
|
|
24
|
-
* @example
|
|
25
|
-
* ```ts
|
|
26
|
-
* // Debounce a search handler
|
|
27
|
-
* const handleSearch = injectDebouncedCallback((query: string) => {
|
|
28
|
-
* fetchSearchResults(query);
|
|
29
|
-
* }, {
|
|
30
|
-
* wait: 500 // Wait 500ms between executions
|
|
31
|
-
* });
|
|
32
|
-
*
|
|
33
|
-
* // Use in an input
|
|
34
|
-
* <input
|
|
35
|
-
* type="search"
|
|
36
|
-
* (input)="handleSearch($event.target.value)"
|
|
37
|
-
* />
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export function injectDebouncedCallback<TFn extends AnyFunction>(
|
|
41
|
-
fn: TFn,
|
|
42
|
-
options: DebouncerOptions<TFn>,
|
|
43
|
-
): (...args: Parameters<TFn>) => void {
|
|
44
|
-
const debouncer = injectDebouncer(fn, options)
|
|
45
|
-
return (...args: Parameters<TFn>) => debouncer.maybeExecute(...args)
|
|
46
|
-
}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { signal } from '@angular/core'
|
|
2
|
-
import { injectDebouncer } from './injectDebouncer'
|
|
3
|
-
import type { AngularDebouncer } from './injectDebouncer'
|
|
4
|
-
import type {
|
|
5
|
-
DebouncerOptions,
|
|
6
|
-
DebouncerState,
|
|
7
|
-
} from '@tanstack/pacer/debouncer'
|
|
8
|
-
|
|
9
|
-
type Setter<T> = (value: T | ((prev: T) => T)) => void
|
|
10
|
-
|
|
11
|
-
export interface DebouncedSignal<TValue, TSelected = {}> {
|
|
12
|
-
(): TValue
|
|
13
|
-
set: Setter<TValue>
|
|
14
|
-
debouncer: AngularDebouncer<Setter<TValue>, TSelected>
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* An Angular function that creates a debounced state signal, combining Angular's signal with debouncing functionality.
|
|
19
|
-
* This function provides both the current debounced value and methods to update it.
|
|
20
|
-
*
|
|
21
|
-
* The state value is only updated after the specified wait time has elapsed since the last update attempt.
|
|
22
|
-
* If another update is attempted before the wait time expires, the timer resets and starts waiting again.
|
|
23
|
-
* This is useful for handling frequent state updates that should be throttled, like search input values
|
|
24
|
-
* or window resize dimensions.
|
|
25
|
-
*
|
|
26
|
-
* The function returns a callable object:
|
|
27
|
-
* - `debounced()`: Get the current debounced value
|
|
28
|
-
* - `debounced.set(...)`: Set or update the debounced value (debounced via maybeExecute)
|
|
29
|
-
* - `debounced.debouncer`: The debouncer instance with additional control methods and state signals
|
|
30
|
-
*
|
|
31
|
-
* ## State Management and Selector
|
|
32
|
-
*
|
|
33
|
-
* The function uses TanStack Store for reactive state management via the underlying debouncer instance.
|
|
34
|
-
* The `selector` parameter allows you to specify which debouncer state changes will trigger signal updates,
|
|
35
|
-
* optimizing performance by preventing unnecessary subscriptions when irrelevant state changes occur.
|
|
36
|
-
*
|
|
37
|
-
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
38
|
-
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
39
|
-
* full control over when your component tracks state changes. Only when you provide a selector will
|
|
40
|
-
* the reactive system track the selected state values.
|
|
41
|
-
*
|
|
42
|
-
* Available debouncer state properties:
|
|
43
|
-
* - `canLeadingExecute`: Whether the debouncer can execute on the leading edge
|
|
44
|
-
* - `executionCount`: Number of function executions that have been completed
|
|
45
|
-
* - `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
|
|
46
|
-
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
47
|
-
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* const debouncedQuery = injectDebouncedSignal('', { wait: 500 })
|
|
52
|
-
*
|
|
53
|
-
* // Get value
|
|
54
|
-
* console.log(debouncedQuery())
|
|
55
|
-
*
|
|
56
|
-
* // Set/update value (debounced)
|
|
57
|
-
* debouncedQuery.set('hello')
|
|
58
|
-
*
|
|
59
|
-
* // Access debouncer
|
|
60
|
-
* console.log(debouncedQuery.debouncer.state().isPending)
|
|
61
|
-
* ```
|
|
62
|
-
*/
|
|
63
|
-
export function injectDebouncedSignal<TValue, TSelected = {}>(
|
|
64
|
-
value: TValue,
|
|
65
|
-
initialOptions: DebouncerOptions<Setter<TValue>>,
|
|
66
|
-
selector?: (state: DebouncerState<Setter<TValue>>) => TSelected,
|
|
67
|
-
): DebouncedSignal<TValue, TSelected> {
|
|
68
|
-
const debouncedValue = signal<TValue>(value)
|
|
69
|
-
|
|
70
|
-
const debouncer = injectDebouncer(
|
|
71
|
-
(newValue: TValue | ((prev: TValue) => TValue)) => {
|
|
72
|
-
if (typeof newValue === 'function') {
|
|
73
|
-
debouncedValue.update(newValue as (prev: TValue) => TValue)
|
|
74
|
-
} else {
|
|
75
|
-
debouncedValue.set(newValue)
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
initialOptions,
|
|
79
|
-
selector,
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
const set: Setter<TValue> = (
|
|
83
|
-
newValue: TValue | ((prev: TValue) => TValue),
|
|
84
|
-
) => {
|
|
85
|
-
debouncer.maybeExecute(newValue)
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const debounced = Object.assign(() => debouncedValue(), {
|
|
89
|
-
set,
|
|
90
|
-
debouncer,
|
|
91
|
-
}) as DebouncedSignal<TValue, TSelected>
|
|
92
|
-
|
|
93
|
-
return debounced
|
|
94
|
-
}
|