MCPcopy
hub / github.com/sveltejs/svelte / init

Function init

packages/svelte/src/internal/client/dom/legacy/lifecycle.js:12–68  ·  view source on GitHub ↗
(immutable = false)

Source from the content-addressed store, hash-verified

10 * @param {boolean} [immutable]
11 */
12export function init(immutable = false) {
13 const context = /** @type {ComponentContextLegacy} */ (component_context);
14
15 const callbacks = context.l.u;
16 if (!callbacks) return;
17
18 let props = () => deep_read_state(context.s);
19
20 if (immutable) {
21 let version = 0;
22 let prev = /** @type {Record<string, any>} */ ({});
23
24 // In legacy immutable mode, before/afterUpdate only fire if the object identity of a prop changes
25 const d = derived(() => {
26 let changed = false;
27 const props = context.s;
28 for (const key in props) {
29 if (props[key] !== prev[key]) {
30 prev[key] = props[key];
31 changed = true;
32 }
33 }
34 if (changed) version++;
35 return version;
36 });
37
38 props = () => get(d);
39 }
40
41 // beforeUpdate
42 if (callbacks.b.length) {
43 user_pre_effect(() => {
44 observe_all(context, props);
45 run_all(callbacks.b);
46 });
47 }
48
49 // onMount (must run before afterUpdate)
50 user_effect(() => {
51 const fns = untrack(() => callbacks.m.map(run));
52 return () => {
53 for (const fn of fns) {
54 if (typeof fn === 'function') {
55 fn();
56 }
57 }
58 };
59 });
60
61 // afterUpdate
62 if (callbacks.a.length) {
63 user_effect(() => {
64 observe_all(context, props);
65 run_all(callbacks.a);
66 });
67 }
68}
69

Callers

nothing calls this directly

Calls 8

derivedFunction · 0.90
getFunction · 0.90
user_pre_effectFunction · 0.90
run_allFunction · 0.90
user_effectFunction · 0.90
untrackFunction · 0.90
observe_allFunction · 0.85
fnFunction · 0.50

Tested by

no test coverage detected