whenever

Category Lifecycle

Shorthand for running a callback when a dependency is truthy.

It can also take an array of dependencies, in which case it will run if all dependencies are truthy.

Usage

<script>
    import { whenever } from '@sv-use/core';
 
    let isActive = $state(false);
 
    whenever(() => isActive, () => {
        console.log('Active now !');
    });
</script>
Type definitions
import type { Getter } from '../../__internal__/types.js';
type WheneverOptions = {
    /**
     * Whether to run the effect on mount or not.
     * @default true
     */
    runOnMount?: boolean;
};
/**
 * Triggers a callback when the dependency is `true`.
 * @param dep The dependency to watch.
 * @param fn The callback to trigger when the dependency is `true`.
 * @param options Additional options to customize the behavior.
 * @see https://svelte-librarian.github.io/sv-use/docs/core/lifecycle/whenever
 */
export declare function whenever(dep: Getter<boolean>, fn: () => void, options?: WheneverOptions): void;
/**
 * Triggers a callback when the dependencies are `true`.
 * @param deps The dependencies to watch.
 * @param fn The callback to trigger when the dependencies are `true`.
 * @param options Additional options to customize the behavior.
 * @see https://svelte-librarian.github.io/sv-use/docs/core/lifecycle/whenever
 */
export declare function whenever(deps: Array<Getter<boolean>>, fn: () => void, options?: WheneverOptions): void;
export {};

Sources