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

createAsyncThrottler.md

110 lines3.8 KB

1---
2id: createAsyncThrottler
3title: createAsyncThrottler
4---
5​
6```ts
7function createAsyncThrottler<TFn, TSelected>(
8 host,
9 fn,
10 options,
11selector?): LitAsyncThrottler<TFn, TSelected>;
12```
13​
14Defined in: [async-throttler/createAsyncThrottler.ts:97](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/async-throttler/createAsyncThrottler.ts#L97)
15​
16Creates and retains the AsyncThrottler for its Lit 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(childHost, 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.
50Disconnecting the host 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.
53Reconnecting the host refreshes options and restores subscriptions to the same instance.
54​
55## Type Parameters
56​
57### TFn
58​
59`TFn` *extends* `AnyAsyncFunction`
60​
61### TSelected
62​
63`TSelected` = \{
64\}
65​
66## Parameters
67​
68### host
69​
70`ReactiveControllerHost`
71​
72Owner of option updates, subscriptions, and cleanup.
73​
74### fn
75​
76`TFn`
77​
78Function executed by the utility.
79​
80### options
81​
82[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitAsyncThrottlerOptions`](../interfaces/LitAsyncThrottlerOptions.md)\<`TFn`, `TSelected`\>\>
83​
84Core options or a reactive factory, plus an optional onUnmount callback.
85​
86### selector?
87​
88(`state`) => `TSelected`
89​
90Selects state that updates the owner. Omit to leave selected state empty.
91​
92## Returns
93​
94[`LitAsyncThrottler`](../interfaces/LitAsyncThrottler.md)\<`TFn`, `TSelected`\>
95​
96The retained utility instance with selected state and child subscriptions.
97​
98## Example
99​
100```ts
101import { createAsyncThrottler } from '@tanstack/lit-pacer'
102​
103const utility = createAsyncThrottler(
104 this, async (value: string) => { console.log(value) },
105 { wait: 500 },
106 (state) => ({ isPending: state.isPending }),
108utility.maybeExecute('item')
109// Selected state: utility.state.isPending
110```