Open History to see the last commit
1---
2id: createDebouncer
3title: createDebouncer
4---
5
6```ts
7function createDebouncer<TFn, TSelected>(
8 scope,
9 fn,
10 options,
11selector?): AlpineDebouncer<TFn, TSelected>;
12```
13
14Defined in: [debouncer/createDebouncer.ts:84](https://github.com/TanStack/pacer/blob/main/packages/alpine-pacer/src/debouncer/createDebouncer.ts#L84)
15
16Creates and retains the Debouncer 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
20## State and subscriptions
21
22Pass a selector to track only the state consumed by the owner. The default selection is {},
23so utility state changes do not update the owner unless it opts in. Selection uses shallow
24comparison. The raw store remains available for additional subscriptions.
25Use utility.subscribe(childScope, selector) for a child subscription. It returns a getter
26and cleans up with the child without canceling the parent utility.
27
28Available state fields:
29
30- `canLeadingExecute`: Whether the debouncer can execute on the leading edge of the timeout
31- `executionCount`: Number of function executions that have been completed
32- `isPending`: Whether the debouncer is waiting for the timeout to trigger execution
33- `lastArgs`: The arguments from the most recent call to maybeExecute
34- `maybeExecuteCount`: Number of times maybeExecute has been called (for reduction calculations)
35- `status`: Current execution status - 'idle' when not active, 'pending' when waiting for timeout
36
37## Options and ownership
38
39Pass an options object with property getters or a factory. Top-level properties are read
40reactively; function-valued core options remain callbacks. Local options override provider
41defaults. Updates retain the utility, its store, counters, and pending work.
42Destroying the owning scope calls cancel().
43onUnmount replaces default cleanup and receives the same adapter instance. A custom callback
44must perform all required cleanup. Use flush() where supported to finish pending work.
45
46## Type Parameters
47
48### TFn
49
50`TFn` *extends* `AnyFunction`
51
52### TSelected
53
54`TSelected` = \{
55\}
56
57## Parameters
58
59### scope
60
61[`PacerScope`](../interfaces/PacerScope.md)
62
63Owner of option updates, subscriptions, and cleanup.
64
65### fn
66
67`TFn`
68
69Function executed by the utility.
70
71### options
72
73[`AlpinePacerOptions`](../type-aliases/AlpinePacerOptions.md)\<[`AlpineDebouncerOptions`](../interfaces/AlpineDebouncerOptions.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[`AlpineDebouncer`](../interfaces/AlpineDebouncer.md)\<`TFn`, `TSelected`\>
86
87The retained utility instance with selected state and child subscriptions.
88
89## Example
90
91```ts
92import { createDebouncer } from '@tanstack/alpine-pacer'
93
94const utility = createDebouncer(
95 scope, (value: string) => { console.log(value) },
96 { wait: 500 },
97 (state) => ({ isPending: state.isPending }),
98)
99utility.maybeExecute('item')
100// Selected state: utility.state.isPending
101```