Vize

whenever

Run a callback whenever a source becomes (or changes while) truthy.

Package @vizejs/composable/whenever
Own the source vize lib pull composable:whenever
Runtime exports whenever
Gzip budget 1024 B

Usage

import { whenever } from "@vizejs/composable/whenever";

Runtime contract

Utility Category Stability SSR Hydration Cleanup Targets Host globals Uses
whenever reactivity experimental safe not-applicable caller, reactive-scope web, server, worker, native, desktop, terminal — —

API

whenever

Run a callback whenever a source becomes (or changes while) truthy. The callback's value is narrowed with Exclude<Value, Falsy>, so a Ref<User | null> delivers User. With once, the watcher stops after the first truthy delivery, not after the first change (unlike Vue's once). With immediate, an already-truthy source is delivered right away. Safe during server rendering and bound to the owning scope.

function whenever<Value>( source: WatchSource<Value>, callback: ( value: Exclude<Value, Falsy>, oldValue: Value | undefined, onCleanup: WatchCleanupRegistrar, ) => void, options: WheneverOptions = {}, ): WatchHandle
whenever(currentUser, (user) => greet(user.name), { immediate: true, once: true });