watchDebounced#

watch with a debounced callback — a burst of source changes collapses into a single callback run carrying the latest value. Cross-platform — import from @sigx/use.

TypeScript
import { signal } from 'sigx';
import { watchDebounced } from '@sigx/use';

const query = signal('');
watchDebounced(query, q => api.search(q), { debounce: 300, maxWait: 2000 });

It takes the same source/cb as core's watch, plus debounce options that extend WatchOptions. The callback's onCleanup behaves as in watch: the registered cleanup runs before the next (debounced) invocation and on stop.

watchDebounced returns core's WatchHandle shape — a callable stop, plus .stop(), .pause(), and .resume():

TypeScript
const handle = watchDebounced(query, run, { debounce: 300 });
handle.pause();
handle.resume();
handle();        // stop — cancels any pending run

Signature#

TypeScript
function watchDebounced<T>(
    source: WatchSource<T> | ReadSignal<T>,
    cb: WatchCallback<T, T | undefined>,
    options?: WatchDebouncedOptions,
): WatchHandle;

interface WatchDebouncedOptions extends WatchOptions {
    debounce?: MaybeSignal<number>;
    maxWait?: number;
}

Options#

OptionTypeDefaultDescription
debounceMaybeSignal<number>200Debounce window in ms.
maxWaitnumberUpper bound (ms) between callback runs while the source keeps changing.

Plus every WatchOptions field (immediate, deep, …).

Platform: everywhere (setTimeout). Stopping — or scope disposal — cancels any pending run.

See also#