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