Home / File/ lifecycle.js — svelte Source File

lifecycle.js — svelte Source File

Architecture documentation for lifecycle.js, a javascript file in the svelte codebase. 13 imports, 0 dependents.

File javascript ClientRuntime LegacyBridge 13 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  309b6da7_c79b_4906_049d_a61ee844e1d7["lifecycle.js"]
  cb946435_ce66_d1e8_6bee_287bdb07e7c5["utils.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  3b9fcc16_eee3_208a_bc65_45c294d8565a["run"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 3b9fcc16_eee3_208a_bc65_45c294d8565a
  ff67bcc0_629f_eab4_221d_71e6bc15e31e["run_all"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> ff67bcc0_629f_eab4_221d_71e6bc15e31e
  48cf26f8_bf34_fd7a_3d52_cc963051e167["context.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 48cf26f8_bf34_fd7a_3d52_cc963051e167
  2fb8b3eb_7c25_3930_a184_09fab29d537f["deriveds.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 2fb8b3eb_7c25_3930_a184_09fab29d537f
  9b434868_abac_094f_b009_bb7564b0d40d["derived"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 9b434868_abac_094f_b009_bb7564b0d40d
  1ae6fa4e_16ee_acdf_5e28_17eb0819fddb["effects.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 1ae6fa4e_16ee_acdf_5e28_17eb0819fddb
  b4142a92_fb70_a0da_ca48_719b2f2398ad["user_pre_effect"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> b4142a92_fb70_a0da_ca48_719b2f2398ad
  61387353_b966_b4f7_d65e_52c75ac7ff61["user_effect"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 61387353_b966_b4f7_d65e_52c75ac7ff61
  bde4209f_8ffc_1594_4024_b1835a44bcf6["runtime.js"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> bde4209f_8ffc_1594_4024_b1835a44bcf6
  14da5ffa_bc19_aee6_36b3_bbba24db7f02["deep_read_state"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> 14da5ffa_bc19_aee6_36b3_bbba24db7f02
  a08b6cc5_af73_1be4_d02f_3113cf8a8305["get"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> a08b6cc5_af73_1be4_d02f_3113cf8a8305
  a814b193_e12a_4037_c3c8_dfd45f3bd0bb["untrack"]
  309b6da7_c79b_4906_049d_a61ee844e1d7 --> a814b193_e12a_4037_c3c8_dfd45f3bd0bb
  style 309b6da7_c79b_4906_049d_a61ee844e1d7 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @import { ComponentContextLegacy } from '#client' */
import { run, run_all } from '../../../shared/utils.js';
import { component_context } from '../../context.js';
import { derived } from '../../reactivity/deriveds.js';
import { user_pre_effect, user_effect } from '../../reactivity/effects.js';
import { deep_read_state, get, untrack } from '../../runtime.js';

/**
 * Legacy-mode only: Call `onMount` callbacks and set up `beforeUpdate`/`afterUpdate` effects
 * @param {boolean} [immutable]
 */
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);
		});
	}
}

/**
 * Invoke the getter of all signals associated with a component
 * so they can be registered to the effect this function is called in.
 * @param {ComponentContextLegacy} context
 * @param {(() => void)} props
 */
function observe_all(context, props) {
	if (context.l.s) {
		for (const signal of context.l.s) get(signal);
	}

	props();
}

Domain

Subdomains

Frequently Asked Questions

What does lifecycle.js do?
lifecycle.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, LegacyBridge subdomain.
What functions are defined in lifecycle.js?
lifecycle.js defines 2 function(s): init, observe_all.
What does lifecycle.js depend on?
lifecycle.js imports 13 module(s): context.js, deep_read_state, derived, deriveds.js, effects.js, get, run, run_all, and 5 more.
Where is lifecycle.js in the architecture?
lifecycle.js is located at packages/svelte/src/internal/client/dom/legacy/lifecycle.js (domain: ClientRuntime, subdomain: LegacyBridge, directory: packages/svelte/src/internal/client/dom/legacy).

Analyze Your Own Codebase

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

Try Supermodel Free