watchThrottled#

watch with a throttled callback — it runs at most once per throttle ms, always with the latest value. Cross-platform — import from @sigx/use.

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

const scrollY = signal(0);
watchThrottled(scrollY, y => updateMinimap(y), { throttle: 100 });

It takes the same source/cb as core's watch, plus throttle options that extend WatchOptions, and returns core's WatchHandle shape (a callable stop, plus .stop(), .pause(), .resume()). leading runs on the leading edge of the window; trailing runs the latest pending change on the trailing edge (both default true).

Signature#

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

interface WatchThrottledOptions extends WatchOptions {
    throttle?: MaybeSignal<number>;
    leading?: boolean;
    trailing?: boolean;
}

Options#

OptionTypeDefaultDescription
throttleMaybeSignal<number>200Throttle window in ms.
leadingbooleantrueRun on the leading edge of the window.
trailingbooleantrueRun the latest pending change on the trailing edge.

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

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

See also#