@sv-use/core
Version:
A collection of Svelte 5 utilities.
17 lines (16 loc) • 513 B
JavaScript
import { normalizeValue, toArray } from '../__internal__/utils.svelte.js';
export function whenever(deps, fn, options = {}) {
const { runOnMount = true } = options;
let active = runOnMount;
$effect(() => {
// Allows to run even with deeply nested object changes
const values = $state.snapshot(toArray(deps).map(normalizeValue));
if (!active) {
active = true;
return;
}
if (values.every((v) => v)) {
fn();
}
});
}