All files / src/internal/client/dom/blocks svelte-component.js

100% Statements 31/31
100% Branches 8/8
100% Functions 1/1
100% Lines 30/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 312x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 78x 78x 78x 78x 78x 78x 78x 150x 146x 150x 66x 66x 66x 146x 150x 132x 132x 78x 78x  
import { block, branch, pause_effect } from '../../reactivity/effects.js';
 
/**
 * @template P
 * @template {(props: P) => void} C
 * @param {import('#client').TemplateNode} anchor
 * @param {() => C} get_component
 * @param {(component: C) => import('#client').Dom | void} render_fn
 * @returns {void}
 */
export function component(anchor, get_component, render_fn) {
	/** @type {C} */
	let component;
 
	/** @type {import('#client').Effect | null} */
	let effect;
 
	block(anchor, 0, () => {
		if (component === (component = get_component())) return;
 
		if (effect) {
			pause_effect(effect);
			effect = null;
		}
 
		if (component) {
			effect = branch(() => render_fn(component));
		}
	});
}