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

createDebouncedState.md

80 lines2.6 KB

1---
2id: createDebouncedState
3title: createDebouncedState
4---
5​
6```ts
7function createDebouncedState<TValue, TSelected>(
8 host,
9 initialValue,
10 options,
11 selector?): [CellValue<TValue>, SetValue<TValue>, LitDebouncer<SetValue<TValue>, TSelected>];
12```
13​
14Defined in: [debouncer/createDebouncedState.ts:39](https://github.com/TanStack/pacer/blob/main/packages/lit-pacer/src/debouncer/createDebouncedState.ts#L39)
15​
16Creates debounced state with a scheduled setter.
17​
18With the default trailing behavior, each call restarts the wait timer and only the latest pending update executes. Leading and trailing options control the edges.
19​
20## Return value
21​
22Returns [value, setValue, utility]. The value is an accessor; call value() from render(). Setters accept a value or a functional updater. Updaters run when the utility executes, using the last committed value. Pending updates may be replaced or rejected according to the utility's scheduling rules. To store a function itself, pass an updater that returns that function.
23​
24## State and ownership
25​
26The value updates independently of the utility selector. The default utility selection is {}. Pass a selector to subscribe to fields such as executionCount, isPending, or status where the underlying utility exposes them.
27​
28Pass the owning ReactiveControllerHost first. Host updates refresh options. Disconnecting runs cleanup; reconnecting restores subscriptions to the same utility.
29Options accept an object with top-level getters or an options factory. Function-valued core options remain callbacks. Local options override provider defaults without replacing the utility or its pending work.
30onUnmount replaces default cleanup and receives the utility instance. A custom callback must perform every needed cancel, stop, or abort action.
31​
32## Type Parameters
33​
34### TValue
35​
36`TValue`
37​
38### TSelected
39​
40`TSelected` = \{
41\}
42​
43## Parameters
44​
45### host
46​
47`ReactiveControllerHost`
48​
49### initialValue
50​
51`TValue`
52​
53### options
54​
55[`LitPacerOptions`](../type-aliases/LitPacerOptions.md)\<[`LitDebouncerOptions`](../interfaces/LitDebouncerOptions.md)\<`SetValue`\<`TValue`\>, `TSelected`\>\>
56​
57### selector?
58​
59(`state`) => `TSelected`
60​
61## Returns
62​
63\[`CellValue`\<`TValue`\>, `SetValue`\<`TValue`\>, [`LitDebouncer`](../interfaces/LitDebouncer.md)\<`SetValue`\<`TValue`\>, `TSelected`\>\]
64​
65## Example
66​
67```ts
68import { createDebouncedState } from '@tanstack/lit-pacer'
69​
70// In a LitElement constructor:
71const [count, setCount, utility] = createDebouncedState(this, 0, { wait: 500 },
72 (state) => ({ executionCount: state.executionCount }),
74setCount((previous) => previous + 1)
75// Bind count and utility.state in the component. Read count() for the committed value.
76```
77​
78## See
79​
80createDebouncer