UNPKG

svelte

Version:

Cybernetically enhanced web apps

34 lines (28 loc) 806 B
/** @import { Effect } from '#client' */ import { block, branch, effect, destroy_effect } from '../../reactivity/effects.js'; // TODO in 6.0 or 7.0, when we remove legacy mode, we can simplify this by // getting rid of the block/branch stuff and just letting the effect rip. // see https://github.com/sveltejs/svelte/pull/15962 /** * @param {Element} node * @param {() => (node: Element) => void} get_fn */ export function attach(node, get_fn) { /** @type {false | undefined | ((node: Element) => void)} */ var fn = undefined; /** @type {Effect | null} */ var e; block(() => { if (fn !== (fn = get_fn())) { if (e) { destroy_effect(e); e = null; } if (fn) { e = branch(() => { effect(() => /** @type {(node: Element) => void} */ (fn)(node)); }); } } }); }