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

createAsyncDebouncer.md

101 lines3.6 KB

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