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

Function if_block

packages/svelte/src/internal/client/dom/blocks/if.js:21–72  ·  view source on GitHub ↗
(node, fn, elseif = false)

Source from the content-addressed store, hash-verified

19 * @returns {void}
20 */
21export function if_block(node, fn, elseif = false) {
22 /** @type {TemplateNode | undefined} */
23 var marker;
24 if (hydrating) {
25 marker = hydrate_node;
26 hydrate_next();
27 }
28
29 var branches = new BranchManager(node);
30 var flags = elseif ? EFFECT_TRANSPARENT : 0;
31
32 /**
33 * @param {number | false} key
34 * @param {null | ((anchor: Node) => void)} fn
35 */
36 function update_branch(key, fn) {
37 if (hydrating) {
38 var data = read_hydration_instruction(/** @type {TemplateNode} */ (marker));
39
40 // "[n" = branch n, "[-1" = else
41 if (key !== parseInt(data.substring(1))) {
42 // Hydration mismatch: remove everything inside the anchor and start fresh.
43 // This could happen with `{#if browser}...{/if}`, for example
44 var anchor = skip_nodes();
45
46 set_hydrate_node(anchor);
47 branches.anchor = anchor;
48
49 set_hydrating(false);
50 branches.ensure(key, fn);
51 set_hydrating(true);
52
53 return;
54 }
55 }
56
57 branches.ensure(key, fn);
58 }
59
60 block(() => {
61 var has_branch = false;
62
63 fn((fn, key = 0) => {
64 has_branch = true;
65 update_branch(key, fn);
66 });
67
68 if (!has_branch) {
69 update_branch(-1, null);
70 }
71 }, flags);
72}

Callers

nothing calls this directly

Calls 4

hydrate_nextFunction · 0.90
blockFunction · 0.90
update_branchFunction · 0.85
fnFunction · 0.50

Tested by

no test coverage detected