@tanstack/angular-pacer 0.20.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 +165 -0
- package/dist/async-batcher/index.cjs +13 -0
- package/dist/async-batcher/index.d.cts +4 -0
- package/dist/async-batcher/index.d.ts +4 -0
- package/dist/async-batcher/index.js +6 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs +49 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.cjs.map +1 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.d.cts +45 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.d.ts +45 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.js +49 -0
- package/dist/async-batcher/injectAsyncBatchedCallback.js.map +1 -0
- package/dist/async-batcher/injectAsyncBatcher.cjs +61 -0
- package/dist/async-batcher/injectAsyncBatcher.cjs.map +1 -0
- package/dist/async-batcher/injectAsyncBatcher.d.cts +63 -0
- package/dist/async-batcher/injectAsyncBatcher.d.ts +63 -0
- package/dist/async-batcher/injectAsyncBatcher.js +61 -0
- package/dist/async-batcher/injectAsyncBatcher.js.map +1 -0
- package/dist/async-debouncer/index.cjs +13 -0
- package/dist/async-debouncer/index.d.cts +4 -0
- package/dist/async-debouncer/index.d.ts +4 -0
- package/dist/async-debouncer/index.js +6 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs +49 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.cjs.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.cts +43 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.d.ts +43 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js +49 -0
- package/dist/async-debouncer/injectAsyncDebouncedCallback.js.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncer.cjs +83 -0
- package/dist/async-debouncer/injectAsyncDebouncer.cjs.map +1 -0
- package/dist/async-debouncer/injectAsyncDebouncer.d.cts +86 -0
- package/dist/async-debouncer/injectAsyncDebouncer.d.ts +86 -0
- package/dist/async-debouncer/injectAsyncDebouncer.js +83 -0
- package/dist/async-debouncer/injectAsyncDebouncer.js.map +1 -0
- package/dist/async-queuer/index.cjs +13 -0
- package/dist/async-queuer/index.d.cts +4 -0
- package/dist/async-queuer/index.d.ts +4 -0
- package/dist/async-queuer/index.js +6 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs +53 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.cjs.map +1 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.d.cts +55 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.d.ts +55 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.js +53 -0
- package/dist/async-queuer/injectAsyncQueuedSignal.js.map +1 -0
- package/dist/async-queuer/injectAsyncQueuer.cjs +61 -0
- package/dist/async-queuer/injectAsyncQueuer.cjs.map +1 -0
- package/dist/async-queuer/injectAsyncQueuer.d.cts +63 -0
- package/dist/async-queuer/injectAsyncQueuer.d.ts +63 -0
- package/dist/async-queuer/injectAsyncQueuer.js +61 -0
- package/dist/async-queuer/injectAsyncQueuer.js.map +1 -0
- package/dist/async-rate-limiter/index.cjs +13 -0
- package/dist/async-rate-limiter/index.d.cts +4 -0
- package/dist/async-rate-limiter/index.d.ts +4 -0
- package/dist/async-rate-limiter/index.js +6 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs +52 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.cjs.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.cts +46 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.d.ts +46 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js +52 -0
- package/dist/async-rate-limiter/injectAsyncRateLimitedCallback.js.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs +57 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.cjs.map +1 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.cts +60 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.d.ts +60 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js +57 -0
- package/dist/async-rate-limiter/injectAsyncRateLimiter.js.map +1 -0
- package/dist/async-retryer/index.cjs +9 -0
- package/dist/async-retryer/index.d.cts +1 -0
- package/dist/async-retryer/index.d.ts +1 -0
- package/dist/async-retryer/index.js +3 -0
- package/dist/async-throttler/index.cjs +13 -0
- package/dist/async-throttler/index.d.cts +4 -0
- package/dist/async-throttler/index.d.ts +4 -0
- package/dist/async-throttler/index.js +6 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs +50 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.cjs.map +1 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.d.cts +44 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.d.ts +44 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.js +50 -0
- package/dist/async-throttler/injectAsyncThrottledCallback.js.map +1 -0
- package/dist/async-throttler/injectAsyncThrottler.cjs +62 -0
- package/dist/async-throttler/injectAsyncThrottler.cjs.map +1 -0
- package/dist/async-throttler/injectAsyncThrottler.d.cts +65 -0
- package/dist/async-throttler/injectAsyncThrottler.d.ts +65 -0
- package/dist/async-throttler/injectAsyncThrottler.js +62 -0
- package/dist/async-throttler/injectAsyncThrottler.js.map +1 -0
- package/dist/batcher/index.cjs +13 -0
- package/dist/batcher/index.d.cts +4 -0
- package/dist/batcher/index.d.ts +4 -0
- package/dist/batcher/index.js +6 -0
- package/dist/batcher/injectBatchedCallback.cjs +47 -0
- package/dist/batcher/injectBatchedCallback.cjs.map +1 -0
- package/dist/batcher/injectBatchedCallback.d.cts +43 -0
- package/dist/batcher/injectBatchedCallback.d.ts +43 -0
- package/dist/batcher/injectBatchedCallback.js +47 -0
- package/dist/batcher/injectBatchedCallback.js.map +1 -0
- package/dist/batcher/injectBatcher.cjs +56 -0
- package/dist/batcher/injectBatcher.cjs.map +1 -0
- package/dist/batcher/injectBatcher.d.cts +58 -0
- package/dist/batcher/injectBatcher.d.ts +58 -0
- package/dist/batcher/injectBatcher.js +56 -0
- package/dist/batcher/injectBatcher.js.map +1 -0
- package/dist/debouncer/index.cjs +17 -0
- package/dist/debouncer/index.d.cts +6 -0
- package/dist/debouncer/index.d.ts +6 -0
- package/dist/debouncer/index.js +8 -0
- package/dist/debouncer/injectDebouncedCallback.cjs +46 -0
- package/dist/debouncer/injectDebouncedCallback.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedCallback.d.cts +43 -0
- package/dist/debouncer/injectDebouncedCallback.d.ts +43 -0
- package/dist/debouncer/injectDebouncedCallback.js +46 -0
- package/dist/debouncer/injectDebouncedCallback.js.map +1 -0
- package/dist/debouncer/injectDebouncedSignal.cjs +68 -0
- package/dist/debouncer/injectDebouncedSignal.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedSignal.d.cts +65 -0
- package/dist/debouncer/injectDebouncedSignal.d.ts +65 -0
- package/dist/debouncer/injectDebouncedSignal.js +68 -0
- package/dist/debouncer/injectDebouncedSignal.js.map +1 -0
- package/dist/debouncer/injectDebouncedValue.cjs +17 -0
- package/dist/debouncer/injectDebouncedValue.cjs.map +1 -0
- package/dist/debouncer/injectDebouncedValue.d.cts +76 -0
- package/dist/debouncer/injectDebouncedValue.d.ts +76 -0
- package/dist/debouncer/injectDebouncedValue.js +17 -0
- package/dist/debouncer/injectDebouncedValue.js.map +1 -0
- package/dist/debouncer/injectDebouncer.cjs +76 -0
- package/dist/debouncer/injectDebouncer.cjs.map +1 -0
- package/dist/debouncer/injectDebouncer.d.cts +79 -0
- package/dist/debouncer/injectDebouncer.d.ts +79 -0
- package/dist/debouncer/injectDebouncer.js +76 -0
- package/dist/debouncer/injectDebouncer.js.map +1 -0
- package/dist/index.cjs +65 -0
- package/dist/index.d.cts +30 -0
- package/dist/index.d.ts +30 -0
- package/dist/index.js +32 -0
- package/dist/provider/index.cjs +7 -0
- package/dist/provider/index.d.cts +3 -0
- package/dist/provider/index.d.ts +3 -0
- package/dist/provider/index.js +4 -0
- package/dist/provider/pacer-context.cjs +20 -0
- package/dist/provider/pacer-context.cjs.map +1 -0
- package/dist/provider/pacer-context.d.cts +21 -0
- package/dist/provider/pacer-context.d.ts +21 -0
- package/dist/provider/pacer-context.js +19 -0
- package/dist/provider/pacer-context.js.map +1 -0
- package/dist/provider/pacer-provider.cjs +41 -0
- package/dist/provider/pacer-provider.cjs.map +1 -0
- package/dist/provider/pacer-provider.d.cts +36 -0
- package/dist/provider/pacer-provider.d.ts +36 -0
- package/dist/provider/pacer-provider.js +41 -0
- package/dist/provider/pacer-provider.js.map +1 -0
- package/dist/queuer/index.cjs +15 -0
- package/dist/queuer/index.d.cts +5 -0
- package/dist/queuer/index.d.ts +5 -0
- package/dist/queuer/index.js +7 -0
- package/dist/queuer/injectQueuedSignal.cjs +47 -0
- package/dist/queuer/injectQueuedSignal.cjs.map +1 -0
- package/dist/queuer/injectQueuedSignal.d.cts +49 -0
- package/dist/queuer/injectQueuedSignal.d.ts +49 -0
- package/dist/queuer/injectQueuedSignal.js +47 -0
- package/dist/queuer/injectQueuedSignal.js.map +1 -0
- package/dist/queuer/injectQueuedValue.cjs +23 -0
- package/dist/queuer/injectQueuedValue.cjs.map +1 -0
- package/dist/queuer/injectQueuedValue.d.cts +34 -0
- package/dist/queuer/injectQueuedValue.d.ts +34 -0
- package/dist/queuer/injectQueuedValue.js +23 -0
- package/dist/queuer/injectQueuedValue.js.map +1 -0
- package/dist/queuer/injectQueuer.cjs +61 -0
- package/dist/queuer/injectQueuer.cjs.map +1 -0
- package/dist/queuer/injectQueuer.d.cts +63 -0
- package/dist/queuer/injectQueuer.d.ts +63 -0
- package/dist/queuer/injectQueuer.js +61 -0
- package/dist/queuer/injectQueuer.js.map +1 -0
- package/dist/rate-limiter/index.cjs +17 -0
- package/dist/rate-limiter/index.d.cts +6 -0
- package/dist/rate-limiter/index.d.ts +6 -0
- package/dist/rate-limiter/index.js +8 -0
- package/dist/rate-limiter/injectRateLimitedCallback.cjs +45 -0
- package/dist/rate-limiter/injectRateLimitedCallback.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedCallback.d.cts +42 -0
- package/dist/rate-limiter/injectRateLimitedCallback.d.ts +42 -0
- package/dist/rate-limiter/injectRateLimitedCallback.js +45 -0
- package/dist/rate-limiter/injectRateLimitedCallback.js.map +1 -0
- package/dist/rate-limiter/injectRateLimitedSignal.cjs +62 -0
- package/dist/rate-limiter/injectRateLimitedSignal.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedSignal.d.cts +59 -0
- package/dist/rate-limiter/injectRateLimitedSignal.d.ts +59 -0
- package/dist/rate-limiter/injectRateLimitedSignal.js +62 -0
- package/dist/rate-limiter/injectRateLimitedSignal.js.map +1 -0
- package/dist/rate-limiter/injectRateLimitedValue.cjs +17 -0
- package/dist/rate-limiter/injectRateLimitedValue.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimitedValue.d.cts +49 -0
- package/dist/rate-limiter/injectRateLimitedValue.d.ts +49 -0
- package/dist/rate-limiter/injectRateLimitedValue.js +17 -0
- package/dist/rate-limiter/injectRateLimitedValue.js.map +1 -0
- package/dist/rate-limiter/injectRateLimiter.cjs +90 -0
- package/dist/rate-limiter/injectRateLimiter.cjs.map +1 -0
- package/dist/rate-limiter/injectRateLimiter.d.cts +93 -0
- package/dist/rate-limiter/injectRateLimiter.d.ts +93 -0
- package/dist/rate-limiter/injectRateLimiter.js +90 -0
- package/dist/rate-limiter/injectRateLimiter.js.map +1 -0
- package/dist/throttler/index.cjs +17 -0
- package/dist/throttler/index.d.cts +6 -0
- package/dist/throttler/index.d.ts +6 -0
- package/dist/throttler/index.js +8 -0
- package/dist/throttler/injectThrottledCallback.cjs +45 -0
- package/dist/throttler/injectThrottledCallback.cjs.map +1 -0
- package/dist/throttler/injectThrottledCallback.d.cts +42 -0
- package/dist/throttler/injectThrottledCallback.d.ts +42 -0
- package/dist/throttler/injectThrottledCallback.js +45 -0
- package/dist/throttler/injectThrottledCallback.js.map +1 -0
- package/dist/throttler/injectThrottledSignal.cjs +69 -0
- package/dist/throttler/injectThrottledSignal.cjs.map +1 -0
- package/dist/throttler/injectThrottledSignal.d.cts +66 -0
- package/dist/throttler/injectThrottledSignal.d.ts +66 -0
- package/dist/throttler/injectThrottledSignal.js +69 -0
- package/dist/throttler/injectThrottledSignal.js.map +1 -0
- package/dist/throttler/injectThrottledValue.cjs +17 -0
- package/dist/throttler/injectThrottledValue.cjs.map +1 -0
- package/dist/throttler/injectThrottledValue.d.cts +74 -0
- package/dist/throttler/injectThrottledValue.d.ts +74 -0
- package/dist/throttler/injectThrottledValue.js +17 -0
- package/dist/throttler/injectThrottledValue.js.map +1 -0
- package/dist/throttler/injectThrottler.cjs +76 -0
- package/dist/throttler/injectThrottler.cjs.map +1 -0
- package/dist/throttler/injectThrottler.d.cts +79 -0
- package/dist/throttler/injectThrottler.d.ts +79 -0
- package/dist/throttler/injectThrottler.js +76 -0
- package/dist/throttler/injectThrottler.js.map +1 -0
- package/dist/types/index.cjs +9 -0
- package/dist/types/index.d.cts +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.js +3 -0
- package/dist/utils/index.cjs +9 -0
- package/dist/utils/index.d.cts +1 -0
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +3 -0
- package/package.json +121 -0
- package/src/async-batcher/index.ts +4 -0
- package/src/async-batcher/injectAsyncBatchedCallback.ts +48 -0
- package/src/async-batcher/injectAsyncBatcher.ts +87 -0
- package/src/async-debouncer/index.ts +4 -0
- package/src/async-debouncer/injectAsyncDebouncedCallback.ts +50 -0
- package/src/async-debouncer/injectAsyncDebouncer.ts +113 -0
- package/src/async-queuer/index.ts +4 -0
- package/src/async-queuer/injectAsyncQueuedSignal.ts +82 -0
- package/src/async-queuer/injectAsyncQueuer.ts +87 -0
- package/src/async-rate-limiter/index.ts +4 -0
- package/src/async-rate-limiter/injectAsyncRateLimitedCallback.ts +52 -0
- package/src/async-rate-limiter/injectAsyncRateLimiter.ts +87 -0
- package/src/async-retryer/index.ts +1 -0
- package/src/async-throttler/index.ts +4 -0
- package/src/async-throttler/injectAsyncThrottledCallback.ts +50 -0
- package/src/async-throttler/injectAsyncThrottler.ts +92 -0
- package/src/batcher/index.ts +4 -0
- package/src/batcher/injectBatchedCallback.ts +46 -0
- package/src/batcher/injectBatcher.ts +79 -0
- package/src/debouncer/index.ts +6 -0
- package/src/debouncer/injectDebouncedCallback.ts +46 -0
- package/src/debouncer/injectDebouncedSignal.ts +101 -0
- package/src/debouncer/injectDebouncedValue.ts +126 -0
- package/src/debouncer/injectDebouncer.ts +102 -0
- package/src/index.ts +40 -0
- package/src/provider/index.ts +2 -0
- package/src/provider/pacer-context.ts +46 -0
- package/src/provider/pacer-provider.ts +38 -0
- package/src/queuer/index.ts +5 -0
- package/src/queuer/injectQueuedSignal.ts +70 -0
- package/src/queuer/injectQueuedValue.ts +101 -0
- package/src/queuer/injectQueuer.ts +83 -0
- package/src/rate-limiter/index.ts +6 -0
- package/src/rate-limiter/injectRateLimitedCallback.ts +45 -0
- package/src/rate-limiter/injectRateLimitedSignal.ts +95 -0
- package/src/rate-limiter/injectRateLimitedValue.ts +99 -0
- package/src/rate-limiter/injectRateLimiter.ts +116 -0
- package/src/throttler/index.ts +6 -0
- package/src/throttler/injectThrottledCallback.ts +45 -0
- package/src/throttler/injectThrottledSignal.ts +102 -0
- package/src/throttler/injectThrottledValue.ts +124 -0
- package/src/throttler/injectThrottler.ts +102 -0
- package/src/types/index.ts +1 -0
- package/src/utils/index.ts +1 -0
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Signal } from "@angular/core";
|
|
2
|
+
import { Store } from "@tanstack/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 AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* Reactive state signal that will be updated when the throttler state changes
|
|
10
|
+
*
|
|
11
|
+
* Use this instead of `throttler.store.state`
|
|
12
|
+
*/
|
|
13
|
+
readonly state: Signal<Readonly<TSelected>>;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
16
|
+
* The state on the store object is not reactive in Angular signals.
|
|
17
|
+
*/
|
|
18
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* An Angular function that creates and manages a Throttler instance.
|
|
22
|
+
*
|
|
23
|
+
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
24
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
25
|
+
*
|
|
26
|
+
* This function provides throttling functionality to limit how often a function can be called,
|
|
27
|
+
* ensuring it executes at most once within a specified time window.
|
|
28
|
+
*
|
|
29
|
+
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
30
|
+
* prevent further executions until the wait period has elapsed.
|
|
31
|
+
*
|
|
32
|
+
* ## State Management and Selector
|
|
33
|
+
*
|
|
34
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
35
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
36
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
37
|
+
*
|
|
38
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
39
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
40
|
+
* full control over when your component tracks state changes.
|
|
41
|
+
*
|
|
42
|
+
* Available state properties:
|
|
43
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
44
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
45
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
46
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
47
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
48
|
+
* - `lastExecutionTime`: Timestamp of the last execution
|
|
49
|
+
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
50
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Default behavior - no reactive state subscriptions
|
|
55
|
+
* const throttler = injectThrottler(
|
|
56
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
57
|
+
* { wait: 100 }
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
61
|
+
* const throttler = injectThrottler(
|
|
62
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
63
|
+
* { wait: 100 },
|
|
64
|
+
* (state) => ({ isPending: state.isPending })
|
|
65
|
+
* );
|
|
66
|
+
*
|
|
67
|
+
* // In an event handler
|
|
68
|
+
* window.addEventListener('scroll', () => {
|
|
69
|
+
* throttler.maybeExecute(window.scrollY);
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
73
|
+
* const { isPending } = throttler.state();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): AngularThrottler<TFn, TSelected>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { AngularThrottler, injectThrottler };
|
|
79
|
+
//# sourceMappingURL=injectThrottler.d.cts.map
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Signal } from "@angular/core";
|
|
2
|
+
import { Throttler, ThrottlerOptions, ThrottlerState } from "@tanstack/pacer/throttler";
|
|
3
|
+
import { AnyFunction } from "@tanstack/pacer/types";
|
|
4
|
+
import { Store } from "@tanstack/store";
|
|
5
|
+
|
|
6
|
+
//#region src/throttler/injectThrottler.d.ts
|
|
7
|
+
interface AngularThrottler<TFn extends AnyFunction, TSelected = {}> extends Omit<Throttler<TFn>, 'store'> {
|
|
8
|
+
/**
|
|
9
|
+
* Reactive state signal that will be updated when the throttler state changes
|
|
10
|
+
*
|
|
11
|
+
* Use this instead of `throttler.store.state`
|
|
12
|
+
*/
|
|
13
|
+
readonly state: Signal<Readonly<TSelected>>;
|
|
14
|
+
/**
|
|
15
|
+
* @deprecated Use `throttler.state` instead of `throttler.store.state` if you want to read reactive state.
|
|
16
|
+
* The state on the store object is not reactive in Angular signals.
|
|
17
|
+
*/
|
|
18
|
+
readonly store: Store<Readonly<ThrottlerState<TFn>>>;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* An Angular function that creates and manages a Throttler instance.
|
|
22
|
+
*
|
|
23
|
+
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
24
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
25
|
+
*
|
|
26
|
+
* This function provides throttling functionality to limit how often a function can be called,
|
|
27
|
+
* ensuring it executes at most once within a specified time window.
|
|
28
|
+
*
|
|
29
|
+
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
30
|
+
* prevent further executions until the wait period has elapsed.
|
|
31
|
+
*
|
|
32
|
+
* ## State Management and Selector
|
|
33
|
+
*
|
|
34
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
35
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
36
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
37
|
+
*
|
|
38
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
39
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
40
|
+
* full control over when your component tracks state changes.
|
|
41
|
+
*
|
|
42
|
+
* Available state properties:
|
|
43
|
+
* - `canLeadingExecute`: Whether the throttler can execute on the leading edge
|
|
44
|
+
* - `canTrailingExecute`: Whether the throttler can execute on the trailing edge
|
|
45
|
+
* - `executionCount`: Number of function executions that have been completed
|
|
46
|
+
* - `isPending`: Whether the throttler is waiting for the timeout to trigger execution
|
|
47
|
+
* - `lastArgs`: The arguments from the most recent call to maybeExecute
|
|
48
|
+
* - `lastExecutionTime`: Timestamp of the last execution
|
|
49
|
+
* - `nextExecutionTime`: Timestamp of the next allowed execution
|
|
50
|
+
* - `status`: Current execution status ('disabled' | 'idle' | 'pending')
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* // Default behavior - no reactive state subscriptions
|
|
55
|
+
* const throttler = injectThrottler(
|
|
56
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
57
|
+
* { wait: 100 }
|
|
58
|
+
* );
|
|
59
|
+
*
|
|
60
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
61
|
+
* const throttler = injectThrottler(
|
|
62
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
63
|
+
* { wait: 100 },
|
|
64
|
+
* (state) => ({ isPending: state.isPending })
|
|
65
|
+
* );
|
|
66
|
+
*
|
|
67
|
+
* // In an event handler
|
|
68
|
+
* window.addEventListener('scroll', () => {
|
|
69
|
+
* throttler.maybeExecute(window.scrollY);
|
|
70
|
+
* });
|
|
71
|
+
*
|
|
72
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
73
|
+
* const { isPending } = throttler.state();
|
|
74
|
+
* ```
|
|
75
|
+
*/
|
|
76
|
+
declare function injectThrottler<TFn extends AnyFunction, TSelected = {}>(fn: TFn, options: ThrottlerOptions<TFn>, selector?: (state: ThrottlerState<TFn>) => TSelected): AngularThrottler<TFn, TSelected>;
|
|
77
|
+
//#endregion
|
|
78
|
+
export { AngularThrottler, injectThrottler };
|
|
79
|
+
//# sourceMappingURL=injectThrottler.d.ts.map
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { injectPacerOptions } from "../provider/pacer-context.js";
|
|
2
|
+
import { injectStore } from "@tanstack/angular-store";
|
|
3
|
+
import { Throttler } from "@tanstack/pacer/throttler";
|
|
4
|
+
|
|
5
|
+
//#region src/throttler/injectThrottler.ts
|
|
6
|
+
/**
|
|
7
|
+
* An Angular function that creates and manages a Throttler instance.
|
|
8
|
+
*
|
|
9
|
+
* This is a lower-level function that provides direct access to the Throttler's functionality.
|
|
10
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
11
|
+
*
|
|
12
|
+
* This function provides throttling functionality to limit how often a function can be called,
|
|
13
|
+
* ensuring it executes at most once within a specified time window.
|
|
14
|
+
*
|
|
15
|
+
* The throttler will execute the function immediately (if leading is enabled) and then
|
|
16
|
+
* prevent further executions until the wait period has elapsed.
|
|
17
|
+
*
|
|
18
|
+
* ## State Management and Selector
|
|
19
|
+
*
|
|
20
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
21
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
22
|
+
* optimizing performance by preventing unnecessary updates 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 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
|
+
* // Default behavior - no reactive state subscriptions
|
|
41
|
+
* const throttler = injectThrottler(
|
|
42
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
43
|
+
* { wait: 100 }
|
|
44
|
+
* );
|
|
45
|
+
*
|
|
46
|
+
* // Opt-in to track isPending changes (optimized for loading states)
|
|
47
|
+
* const throttler = injectThrottler(
|
|
48
|
+
* (scrollY: number) => updateScrollPosition(scrollY),
|
|
49
|
+
* { wait: 100 },
|
|
50
|
+
* (state) => ({ isPending: state.isPending })
|
|
51
|
+
* );
|
|
52
|
+
*
|
|
53
|
+
* // In an event handler
|
|
54
|
+
* window.addEventListener('scroll', () => {
|
|
55
|
+
* throttler.maybeExecute(window.scrollY);
|
|
56
|
+
* });
|
|
57
|
+
*
|
|
58
|
+
* // Access the selected state (will be empty object {} unless selector provided)
|
|
59
|
+
* const { isPending } = throttler.state();
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
function injectThrottler(fn, options, selector = () => ({})) {
|
|
63
|
+
const throttler = new Throttler(fn, {
|
|
64
|
+
...injectPacerOptions().throttler,
|
|
65
|
+
...options
|
|
66
|
+
});
|
|
67
|
+
const state = injectStore(throttler.store, selector);
|
|
68
|
+
return {
|
|
69
|
+
...throttler,
|
|
70
|
+
state
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
//#endregion
|
|
75
|
+
export { injectThrottler };
|
|
76
|
+
//# sourceMappingURL=injectThrottler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"injectThrottler.js","names":[],"sources":["../../src/throttler/injectThrottler.ts"],"sourcesContent":["import { injectStore } 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/store'\nimport type { AnyFunction } from '@tanstack/pacer/types'\nimport type {\n ThrottlerOptions,\n ThrottlerState,\n} from '@tanstack/pacer/throttler'\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 * @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: ThrottlerOptions<TFn>,\n selector: (state: ThrottlerState<TFn>) => TSelected = () => ({}) as TSelected,\n): AngularThrottler<TFn, TSelected> {\n const mergedOptions = {\n ...injectPacerOptions().throttler,\n ...options,\n } as ThrottlerOptions<TFn>\n\n const throttler = new Throttler<TFn>(fn, mergedOptions)\n const state = injectStore(throttler.store, selector)\n\n return {\n ...throttler,\n state,\n } as AngularThrottler<TFn, TSelected>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoFA,SAAgB,gBACd,IACA,SACA,kBAA6D,EAAE,GAC7B;CAMlC,MAAM,YAAY,IAAI,UAAe,IALf;EACpB,GAAG,oBAAoB,CAAC;EACxB,GAAG;EACJ,CAEsD;CACvD,MAAM,QAAQ,YAAY,UAAU,OAAO,SAAS;AAEpD,QAAO;EACL,GAAG;EACH;EACD"}
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/types";
|
|
@@ -0,0 +1,9 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "@tanstack/pacer/utils";
|
package/package.json
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tanstack/angular-pacer",
|
|
3
|
+
"version": "0.20.0",
|
|
4
|
+
"description": "Utilities for debouncing and throttling functions in Angular.",
|
|
5
|
+
"author": "Tanner Linsley",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/TanStack/pacer.git",
|
|
10
|
+
"directory": "packages/angular-pacer"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://tanstack.com/pacer",
|
|
13
|
+
"funding": {
|
|
14
|
+
"type": "github",
|
|
15
|
+
"url": "https://github.com/sponsors/tannerlinsley"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"angular",
|
|
19
|
+
"debounce",
|
|
20
|
+
"throttle",
|
|
21
|
+
"rate-limit",
|
|
22
|
+
"queue",
|
|
23
|
+
"async",
|
|
24
|
+
"signals"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "premove ./build ./dist",
|
|
28
|
+
"test:eslint": "eslint ./src",
|
|
29
|
+
"test:lib": "vitest --passWithNoTests",
|
|
30
|
+
"test:lib:dev": "pnpm test:lib --watch",
|
|
31
|
+
"test:types": "tsc",
|
|
32
|
+
"build": "tsdown"
|
|
33
|
+
},
|
|
34
|
+
"type": "module",
|
|
35
|
+
"main": "./dist/index.cjs",
|
|
36
|
+
"module": "./dist/index.js",
|
|
37
|
+
"types": "./dist/index.d.cts",
|
|
38
|
+
"exports": {
|
|
39
|
+
".": {
|
|
40
|
+
"import": "./dist/index.js",
|
|
41
|
+
"require": "./dist/index.cjs"
|
|
42
|
+
},
|
|
43
|
+
"./async-batcher": {
|
|
44
|
+
"import": "./dist/async-batcher/index.js",
|
|
45
|
+
"require": "./dist/async-batcher/index.cjs"
|
|
46
|
+
},
|
|
47
|
+
"./async-debouncer": {
|
|
48
|
+
"import": "./dist/async-debouncer/index.js",
|
|
49
|
+
"require": "./dist/async-debouncer/index.cjs"
|
|
50
|
+
},
|
|
51
|
+
"./async-queuer": {
|
|
52
|
+
"import": "./dist/async-queuer/index.js",
|
|
53
|
+
"require": "./dist/async-queuer/index.cjs"
|
|
54
|
+
},
|
|
55
|
+
"./async-rate-limiter": {
|
|
56
|
+
"import": "./dist/async-rate-limiter/index.js",
|
|
57
|
+
"require": "./dist/async-rate-limiter/index.cjs"
|
|
58
|
+
},
|
|
59
|
+
"./async-retryer": {
|
|
60
|
+
"import": "./dist/async-retryer/index.js",
|
|
61
|
+
"require": "./dist/async-retryer/index.cjs"
|
|
62
|
+
},
|
|
63
|
+
"./async-throttler": {
|
|
64
|
+
"import": "./dist/async-throttler/index.js",
|
|
65
|
+
"require": "./dist/async-throttler/index.cjs"
|
|
66
|
+
},
|
|
67
|
+
"./batcher": {
|
|
68
|
+
"import": "./dist/batcher/index.js",
|
|
69
|
+
"require": "./dist/batcher/index.cjs"
|
|
70
|
+
},
|
|
71
|
+
"./debouncer": {
|
|
72
|
+
"import": "./dist/debouncer/index.js",
|
|
73
|
+
"require": "./dist/debouncer/index.cjs"
|
|
74
|
+
},
|
|
75
|
+
"./provider": {
|
|
76
|
+
"import": "./dist/provider/index.js",
|
|
77
|
+
"require": "./dist/provider/index.cjs"
|
|
78
|
+
},
|
|
79
|
+
"./queuer": {
|
|
80
|
+
"import": "./dist/queuer/index.js",
|
|
81
|
+
"require": "./dist/queuer/index.cjs"
|
|
82
|
+
},
|
|
83
|
+
"./rate-limiter": {
|
|
84
|
+
"import": "./dist/rate-limiter/index.js",
|
|
85
|
+
"require": "./dist/rate-limiter/index.cjs"
|
|
86
|
+
},
|
|
87
|
+
"./throttler": {
|
|
88
|
+
"import": "./dist/throttler/index.js",
|
|
89
|
+
"require": "./dist/throttler/index.cjs"
|
|
90
|
+
},
|
|
91
|
+
"./types": {
|
|
92
|
+
"import": "./dist/types/index.js",
|
|
93
|
+
"require": "./dist/types/index.cjs"
|
|
94
|
+
},
|
|
95
|
+
"./utils": {
|
|
96
|
+
"import": "./dist/utils/index.js",
|
|
97
|
+
"require": "./dist/utils/index.cjs"
|
|
98
|
+
},
|
|
99
|
+
"./package.json": "./package.json"
|
|
100
|
+
},
|
|
101
|
+
"sideEffects": false,
|
|
102
|
+
"engines": {
|
|
103
|
+
"node": ">=18"
|
|
104
|
+
},
|
|
105
|
+
"files": [
|
|
106
|
+
"dist",
|
|
107
|
+
"src"
|
|
108
|
+
],
|
|
109
|
+
"dependencies": {
|
|
110
|
+
"@tanstack/angular-store": "^0.8.0",
|
|
111
|
+
"@tanstack/pacer": "workspace:*",
|
|
112
|
+
"@tanstack/store": "^0.8.0"
|
|
113
|
+
},
|
|
114
|
+
"devDependencies": {
|
|
115
|
+
"@angular/core": "^21.0.0",
|
|
116
|
+
"@types/node": "^25.0.10"
|
|
117
|
+
},
|
|
118
|
+
"peerDependencies": {
|
|
119
|
+
"@angular/core": ">=17.0.0"
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { injectStore } from '@tanstack/angular-store'
|
|
2
|
+
import { AsyncBatcher } from '@tanstack/pacer/async-batcher'
|
|
3
|
+
import { injectPacerOptions } from '../provider/pacer-context'
|
|
4
|
+
import type { Signal } from '@angular/core'
|
|
5
|
+
import type { Store } from '@tanstack/store'
|
|
6
|
+
import type {
|
|
7
|
+
AsyncBatcherOptions,
|
|
8
|
+
AsyncBatcherState,
|
|
9
|
+
} from '@tanstack/pacer/async-batcher'
|
|
10
|
+
|
|
11
|
+
export interface AngularAsyncBatcher<TValue, TSelected = {}> extends Omit<
|
|
12
|
+
AsyncBatcher<TValue>,
|
|
13
|
+
'store'
|
|
14
|
+
> {
|
|
15
|
+
/**
|
|
16
|
+
* Reactive state signal that will be updated when the async batcher state changes
|
|
17
|
+
*
|
|
18
|
+
* Use this instead of `batcher.store.state`
|
|
19
|
+
*/
|
|
20
|
+
readonly state: Signal<Readonly<TSelected>>
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Use `batcher.state` instead of `batcher.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<AsyncBatcherState<TValue>>>
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* An Angular function that creates and manages an AsyncBatcher instance.
|
|
30
|
+
*
|
|
31
|
+
* This is a lower-level function that provides direct access to the AsyncBatcher's functionality.
|
|
32
|
+
* This allows you to integrate it with any state management solution you prefer.
|
|
33
|
+
*
|
|
34
|
+
* The AsyncBatcher collects items and processes them in batches asynchronously with support for
|
|
35
|
+
* promise-based processing, error handling, retry capabilities, and abort support.
|
|
36
|
+
*
|
|
37
|
+
* ## State Management and Selector
|
|
38
|
+
*
|
|
39
|
+
* The function uses TanStack Store for state management and wraps it with Angular signals.
|
|
40
|
+
* The `selector` parameter allows you to specify which state changes will trigger signal updates,
|
|
41
|
+
* optimizing performance by preventing unnecessary updates when irrelevant state changes occur.
|
|
42
|
+
*
|
|
43
|
+
* **By default, there will be no reactive state subscriptions** and you must opt-in to state
|
|
44
|
+
* tracking by providing a selector function. This prevents unnecessary updates and gives you
|
|
45
|
+
* full control over when your component tracks state changes.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* // Default behavior - no reactive state subscriptions
|
|
50
|
+
* const batcher = injectAsyncBatcher(
|
|
51
|
+
* async (items: Array<Data>) => {
|
|
52
|
+
* const response = await fetch('/api/batch', {
|
|
53
|
+
* method: 'POST',
|
|
54
|
+
* body: JSON.stringify(items)
|
|
55
|
+
* });
|
|
56
|
+
* return response.json();
|
|
57
|
+
* },
|
|
58
|
+
* { maxSize: 10, wait: 2000 }
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Add items
|
|
62
|
+
* batcher.addItem(data1);
|
|
63
|
+
* batcher.addItem(data2);
|
|
64
|
+
*
|
|
65
|
+
* // Access the selected state
|
|
66
|
+
* const { items, isExecuting } = batcher.state();
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export function injectAsyncBatcher<TValue, TSelected = {}>(
|
|
70
|
+
fn: (items: Array<TValue>) => Promise<any>,
|
|
71
|
+
options: AsyncBatcherOptions<TValue> = {},
|
|
72
|
+
selector: (state: AsyncBatcherState<TValue>) => TSelected = () =>
|
|
73
|
+
({}) as TSelected,
|
|
74
|
+
): AngularAsyncBatcher<TValue, TSelected> {
|
|
75
|
+
const mergedOptions = {
|
|
76
|
+
...injectPacerOptions().asyncBatcher,
|
|
77
|
+
...options,
|
|
78
|
+
} as AsyncBatcherOptions<TValue>
|
|
79
|
+
|
|
80
|
+
const batcher = new AsyncBatcher<TValue>(fn, mergedOptions)
|
|
81
|
+
const state = injectStore(batcher.store, selector)
|
|
82
|
+
|
|
83
|
+
return {
|
|
84
|
+
...batcher,
|
|
85
|
+
state,
|
|
86
|
+
} as AngularAsyncBatcher<TValue, TSelected>
|
|
87
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
}
|