Debounce
Accumulate and delay state updates.
Debounce
The debounce middleware queues rapid updates and flushes them after a configured wait time. In the current implementation, the wait window starts on the first update and additional updates are batched into that same window.
import { debounce } from '@ilokesto/state/middleware';
const store = debounce(200)({ text: '' });How It Works
- Queueing: Updates are collected during the wait period.
- Replaying: Function updaters are replayed in the exact order they were received against the current accumulated state.
- Flushing: When the current wait window expires, the final accumulated state is committed to the store.
Call Shape
- Curried form:
debounce(wait)(store) - Direct form:
debounce(store, wait)
If you omit the wait value, the current implementation falls back to 300ms.
Considerations
- Windowed Batching: This is not a classic “wait until inactivity” debounce. The timer is not reset on every update.
- Latency:
debouncedelays writes by design. Avoid using it where immediate synchronous state access is required (e.g., controlled inputs that need instant feedback). - Sequentiality: Replays function updaters to ensure consistent state transitions even when debounced.