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

createAsyncDebouncer.md

108 lines3.7 KB

1---
2id: createAsyncDebouncer
3title: createAsyncDebouncer
4---
5​
6```ts
7function createAsyncDebouncer<TFn, TSelected>(
8 scope,
9 fn,
10 options,
11selector?): AlpineAsyncDebouncer<TFn, TSelected>;
12```
13​
14Defined in: [async-debouncer/createAsyncDebouncer.ts:94](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/async-debouncer/createAsyncDebouncer.ts#L94)
15​
16Creates and retains the AsyncDebouncer for its Alpine owner.
17​
18Waits for a quiet period, then executes the latest call. Each new call restarts the trailing timer. Configure leading and trailing edges for search, autosave, or resize handlers.
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- `canLeadingExecute`: Whether the debouncer can execute on the leading edge of the timeout
34- `errorCount`: Number of function executions that have resulted in errors
35- `isExecuting`: Whether the debounced function is currently executing asynchronously
36- `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
37- `lastArgs`: The arguments from the most recent call to maybeExecute
38- `lastResult`: The result from the most recent successful function execution
39- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
40- `settleCount`: Number of function executions that have completed (either successfully or with errors)
41- `status`: Current execution status - 'idle' when not active, 'pending' when waiting, 'executing' when running, 'settled' when completed
42- `successCount`: Number of function executions that have completed successfully
43​
44## Options and ownership
45​
46Pass an options object with property getters or a factory. Top-level properties are read
47reactively; function-valued core options remain callbacks. Local options override provider
48defaults. Updates retain the utility, its store, counters, and pending work.
49Destroying the owning scope calls cancel() and abort().
50onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
51must perform all required cleanup. Use flush() where supported to finish pending work.
52​
53## Type Parameters
54​
55### TFn
56​
57`TFn` *extends* `AnyAsyncFunction`
58​
59### TSelected
60​
61`TSelected` = \{
62\}
63​
64## Parameters
65​
66### scope
67​
68[`PacerScope`](../interfaces/PacerScope.md)
69​
70Owner of option updates, subscriptions, and cleanup.
71​
72### fn
73​
74`TFn`
75​
76Function executed by the utility.
77​
78### options
79​
80[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineAsyncDebouncerOptions`](../interfaces/AlpineAsyncDebouncerOptions.md)\<`TFn`, `TSelected`\>\>
81​
82Core options or a reactive factory, plus an optional onUnmount callback.
83​
84### selector?
85​
86(`state`) => `TSelected`
87​
88Selects state that updates the owner. Omit to leave selected state empty.
89​
90## Returns
91​
92[`AlpineAsyncDebouncer`](../interfaces/AlpineAsyncDebouncer.md)\<`TFn`, `TSelected`\>
93​
94The retained utility instance with selected state and child subscriptions.
95​
96## Example
97​
98```ts
99import { createAsyncDebouncer } from '@tanstack/alpine-pacer'
100​
101const utility = createAsyncDebouncer(
102 scope, async (value: string) => { console.log(value) },
103 { wait: 500 },
104 (state) => ({ isPending: state.isPending }),
106utility.maybeExecute('item')
107// Selected state: utility.state.isPending
108```