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

100% Statements 59/59
100% Branches 18/18
100% Functions 2/2
100% Lines 56/56

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 572x 2x 2x 2x 2x 2x 2x 2x 2740x 2740x 2740x 2740x 2740x 2740x 2129x 2129x 2738x 2022x 2046x 2046x 2022x 2022x 2129x 2129x 2129x 2129x 2129x 2127x 115x 42x 42x 115x 2129x 2129x 2129x 2129x 2740x 18x 36x 36x 18x 18x 2740x 2x 2x 2x 2x 2x 2x 2082x 2082x 2080x 2080x 2082x 2082x 2082x  
import { run, run_all } from '../../../shared/utils.js';
import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
import { current_component_context, deep_read_state, 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);
		});
	}
 
	// 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);
}