Vize

watch-throttled

Watch sources like Vue's watch, but run the callback at most once per cooldown window.

Package @vizejs/composable/watch-throttled
Own the source vize lib pull composable:watch-throttled
Runtime exports watchThrottled
Gzip budget 1536 B

Usage

import { watchThrottled } from "@vizejs/composable/watch-throttled";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
watchThrottled timing experimental deterministic-fallback not-applicable caller, reactive-scope web, server, worker, native, desktop, terminal globalThis, window tryOnScopeDispose

API

watchThrottled

Watch sources like Vue's watch, but run the callback at most once per cooldown window. With the defaults (leading and trailing both on) the first change runs immediately and the newest change inside the window runs when it closes, which opens a new window. Every call receives, as its old value, the value of the previously delivered call, so consecutive calls chain without gaps. Source typing mirrors watch. Without a browser window (and without runOnServer) the callback runs synchronously. Timers are cleared when the watcher stops or the owning scope is disposed.

function watchThrottled< const Sources extends WatchSources, Immediate extends Readonly<boolean> = false, >( source: Sources, callback: WatchHelperCallback<Sources, Immediate>, options?: WatchThrottledOptions<Immediate>, ): TimedWatchHandle
watchThrottled(scrollY, (y) => report(y), { throttle: 100 });

watchThrottled

function watchThrottled( source: WatchSources, callback: WatchCallback<unknown, unknown>, options: WatchThrottledOptions<boolean> = {}, ): TimedWatchHandle

Types

WatchThrottledOptions

Options for watchThrottled.

Member Type Description
throttle? MaybeRefOrGetter<number> Cooldown window in milliseconds. Reactive; read whenever a window opens.
leading? boolean Run the first change of a window immediately.
trailing? boolean Run the newest change collected during a window when it closes.
runOnServer? boolean Applies the timing policy when no browser window is available. When disabled, the callback runs synchronously on the server.
scheduler? TimeoutScheduler Owns the cooldown timer.