All files / src/internal/client/dom/legacy lifecycle.js

100% Statements 75/75
100% Branches 19/19
100% Functions 2/2
100% Lines 71/71

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 722x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2736x 2736x 2736x 2736x 2736x 2736x 2129x 2129x 2734x 2022x 2046x 2046x 2046x 2046x 2046x 2046x 2046x 2006x 2006x 2022x 2022x 2129x 2129x 2129x 2129x 2129x 2127x 115x 42x 42x 115x 2129x 2129x 2129x 2129x 2736x 18x 36x 36x 18x 18x 2736x 2x 2x 2x 2x 2x 2x 2082x 2082x 2080x 2080x 2082x 2082x 2082x  
import { CLEAN } from '../../constants.js';
import { run, run_all } from '../../../shared/utils.js';
import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
import {
	current_component_context,
	current_effect,
	deep_read_state,
	flush_local_render_effects,
	get,
	untrack
} from '../../runtime.js';
 
/**
 * Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects
 */
export function init() {
	const context = /** @type {import('#client').ComponentContextLegacy} */ (
		current_component_context
	);
 
	const callbacks = context.l.u;
	if (!callbacks) return;
 
	// beforeUpdate
	if (callbacks.b.length) {
		user_pre_effect(() => {
			observe_all(context);
			run_all(callbacks.b);
			// beforeUpdate might change state that affects rendering, ensure the render effects following from it
			// are batched up with the current run. Avoids for example child components rerunning when they're
			// now hidden because beforeUpdate did set an if block to false.
			const parent = current_effect?.parent;
			if (parent != null && (parent.f & CLEAN) === 0) {
				flush_local_render_effects(parent);
			}
		});
	}
 
	// onMount (must run before afterUpdate)
	user_effect(() => {
		const fns = untrack(() => callbacks.m.map(run));
		return () => {
			for (const fn of fns) {
				if (typeof fn === 'function') {
					fn();
				}
			}
		};
	});
 
	// afterUpdate
	if (callbacks.a.length) {
		user_effect(() => {
			observe_all(context);
			run_all(callbacks.a);
		});
	}
}
 
/**
 * Invoke the getter of all signals associated with a component
 * so they can be registered to the effect this function is called in.
 * @param {import('#client').ComponentContextLegacy} context
 */
function observe_all(context) {
	if (context.l.s) {
		for (const signal of context.l.s) get(signal);
	}
 
	deep_read_state(context.s);
}