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.
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
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
| Option | Type | Default | Description |
|---|---|---|---|
throttle | MaybeSignal<number> | 200 | Throttle window in ms. |
leading | boolean | true | Run on the leading edge of the window. |
trailing | boolean | true | Run 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
watchDebounced— collapse bursts into one run after quiet instead.useThrottledSignal— derive a throttled signal rather than run a callback.- Conventions — the shared input/output patterns.
