debounce
Delays emission until a specified duration has passed without new signals. Emits only the last signal after activity stops.
Signature
function debounce<T extends Signal>(ms: number): Operator<T, T>Example
import { keyboard } from "cereb";import { debounce } from "cereb/operators";
keyboard(window) .pipe(debounce(300)) .on((signal) => { // Fires 300ms after last keypress search(signal.value.key); });Comparison with Throttle
| Operator | Behavior | Use Case |
|---|---|---|
debounce | Wait for silence | After activity stops |
throttle | Emit first, ignore rest | Real-time feedback |
throttleLast | Collect, emit last | Final state matters |
Use Cases
Search Input
// Wait for user to stop typingkeyboard(searchInput) .pipe(debounce(300)) .on(() => performSearch());Resize Handler
// Handle resize only after user stops resizingdomEvent(window, "resize") .pipe(debounce(200)) .on(recalculateLayout);Save Draft
// Auto-save 1 second after last editkeyboard(editor) .pipe(debounce(1000)) .on(saveDraft);