R
fn

whenever

v0.0.15testeddemo

Shorthand for watching a source to be truthy. Behaves like `watch`, but the callback only fires when the resolved value is truthy.

Examples

ts
const ready = ref(false);
whenever(ready, () => console.log('ready!'));
ts
whenever(() => count.value > 5, () => console.log('over five'), { once: true });

Demo

Loading demo…

Signatures

ts
export function whenever<T>(source: WatchSource<T>, cb: WatchCallback<Truthy<T>, T | undefined>, options?: WheneverOptions<true>): WatchHandle;
ts
export function whenever<T>(source: WatchSource<T>, cb: WatchCallback<Truthy<T>, T>, options?: WheneverOptions<false>): WatchHandle;

Type Parameters

T

Parameters

ParameterTypeDescription
sourceWatchSource<T>The reactive source to watch
cbWatchCallback<Truthy<T>, T | undefined>Invoked with the truthy value, previous value, and `onCleanup`
options?WheneverOptions<boolean> | undefinedWatch options (`immediate`, `deep`, `flush`, `once`)

Returns

WatchHandleA handle to stop watching