Home / Function/ init() — svelte Function Reference

init() — svelte Function Reference

Architecture documentation for the init() function in lifecycle.js from the svelte codebase.

Entity Profile

Dependency Diagram

graph TD
  2bef4c90_800a_61a0_724e_32f34a823482["init()"]
  309b6da7_c79b_4906_049d_a61ee844e1d7["lifecycle.js"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|defined in| 309b6da7_c79b_4906_049d_a61ee844e1d7
  14da5ffa_bc19_aee6_36b3_bbba24db7f02["deep_read_state()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| 14da5ffa_bc19_aee6_36b3_bbba24db7f02
  9b434868_abac_094f_b009_bb7564b0d40d["derived()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| 9b434868_abac_094f_b009_bb7564b0d40d
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| a08b6cc5_af73_1be4_d02f_3113cf8a8305
  b4142a92_fb70_a0da_ca48_719b2f2398ad["user_pre_effect()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| b4142a92_fb70_a0da_ca48_719b2f2398ad
  1f8c1534_1166_5d5a_e3b0_cb3660ea11bf["observe_all()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| 1f8c1534_1166_5d5a_e3b0_cb3660ea11bf
  ff67bcc0_629f_eab4_221d_71e6bc15e31e["run_all()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| ff67bcc0_629f_eab4_221d_71e6bc15e31e
  61387353_b966_b4f7_d65e_52c75ac7ff61["user_effect()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| 61387353_b966_b4f7_d65e_52c75ac7ff61
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack()"]
  2bef4c90_800a_61a0_724e_32f34a823482 -->|calls| a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  style 2bef4c90_800a_61a0_724e_32f34a823482 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/svelte/src/internal/client/dom/legacy/lifecycle.js lines 12–68

export function init(immutable = false) {
	const context = /** @type {ComponentContextLegacy} */ (component_context);

	const callbacks = context.l.u;
	if (!callbacks) return;

	let props = () => deep_read_state(context.s);

	if (immutable) {
		let version = 0;
		let prev = /** @type {Record<string, any>} */ ({});

		// In legacy immutable mode, before/afterUpdate only fire if the object identity of a prop changes
		const d = derived(() => {
			let changed = false;
			const props = context.s;
			for (const key in props) {
				if (props[key] !== prev[key]) {
					prev[key] = props[key];
					changed = true;
				}
			}
			if (changed) version++;
			return version;
		});

		props = () => get(d);
	}

	// beforeUpdate
	if (callbacks.b.length) {
		user_pre_effect(() => {
			observe_all(context, props);
			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, props);
			run_all(callbacks.a);
		});
	}
}

Domain

Subdomains

Frequently Asked Questions

What does init() do?
init() is a function in the svelte codebase, defined in packages/svelte/src/internal/client/dom/legacy/lifecycle.js.
Where is init() defined?
init() is defined in packages/svelte/src/internal/client/dom/legacy/lifecycle.js at line 12.
What does init() call?
init() calls 8 function(s): deep_read_state, derived, get, observe_all, run_all, untrack, user_effect, user_pre_effect.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free