Skip to content
Source
Sign inSign up
TanStack/pacer
Open History to see the last commit

createAsyncThrottler.md

109 lines3.8 KB

1---
2id: createAsyncThrottler
3title: createAsyncThrottler
4---
5​
6```ts
7function createAsyncThrottler<TFn, TSelected>(
8 scope,
9 fn,
10 options,
11selector?): AlpineAsyncThrottler<TFn, TSelected>;
12```
13​
14Defined in: [async-throttler/createAsyncThrottler.ts:95](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/async-throttler/createAsyncThrottler.ts#L95)
15​
16Creates and retains the AsyncThrottler for its Alpine owner.
17​
18Limits execution to at most one call per wait interval. Leading and trailing options control immediate and deferred execution; the trailing call uses the latest arguments.
19​
20The callback may return a Promise. Core result, error, retry, and abort behavior is preserved.
21Use onSuccess, onError, and onSettled for execution outcomes.
22​
23## State and subscriptions
24​
25Pass a selector to track only the state consumed by the owner. The default selection is {},
26so utility state changes do not update the owner unless it opts in. Selection uses shallow
27comparison. The raw store remains available for additional subscriptions.
28Use utility.subscribe(childScope, selector) for a child subscription. It returns a getter
29and cleans up with the child without canceling the parent utility.
30​
31Available state fields:
32​
33- `errorCount`: Number of function executions that have resulted in errors
34- `isExecuting`: Whether the throttled function is currently executing asynchronously
35- `isPending`: Whether the throttler is waiting for the timeout to trigger execution
36- `lastArgs`: The arguments from the most recent call to maybeExecute
37- `lastExecutionTime`: Timestamp of the last function execution in milliseconds
38- `lastResult`: The result from the most recent successful function execution
39- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
40- `nextExecutionTime`: Timestamp when the next execution can occur in milliseconds
41- `settleCount`: Number of function executions that have completed (either successfully or with errors)
42- `status`: Current execution status - 'idle' when not active, 'pending' when waiting, 'executing' when running, 'settled' when completed
43- `successCount`: Number of function executions that have completed successfully
44​
45## Options and ownership
46​
47Pass an options object with property getters or a factory. Top-level properties are read
48reactively; function-valued core options remain callbacks. Local options override provider
49defaults. Updates retain the utility, its store, counters, and pending work.
50Destroying the owning scope calls cancel() and abort().
51onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
52must perform all required cleanup. Use flush() where supported to finish pending work.
53​
54## Type Parameters
55​
56### TFn
57​
58`TFn` *extends* `AnyAsyncFunction`
59​
60### TSelected
61​
62`TSelected` = \{
63\}
64​
65## Parameters
66​
67### scope
68​
69[`PacerScope`](../interfaces/PacerScope.md)
70​
71Owner of option updates, subscriptions, and cleanup.
72​
73### fn
74​
75`TFn`
76​
77Function executed by the utility.
78​
79### options
80​
81[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineAsyncThrottlerOptions`](../interfaces/AlpineAsyncThrottlerOptions.md)\<`TFn`, `TSelected`\>\>
82​
83Core options or a reactive factory, plus an optional onUnmount callback.
84​
85### selector?
86​
87(`state`) => `TSelected`
88​
89Selects state that updates the owner. Omit to leave selected state empty.
90​
91## Returns
92​
93[`AlpineAsyncThrottler`](../interfaces/AlpineAsyncThrottler.md)\<`TFn`, `TSelected`\>
94​
95The retained utility instance with selected state and child subscriptions.
96​
97## Example
98​
99```ts
100import { createAsyncThrottler } from '@tanstack/alpine-pacer'
101​
102const utility = createAsyncThrottler(
103 scope, async (value: string) => { console.log(value) },
104 { wait: 500 },
105 (state) => ({ isPending: state.isPending }),
107utility.maybeExecute('item')
108// Selected state: utility.state.isPending
109```