Skip to content

Commit 3c2eb4c

Browse files
committed
fix
1 parent e3b1510 commit 3c2eb4c

File tree

4 files changed

+20
-71
lines changed

4 files changed

+20
-71
lines changed

packages/svelte/src/internal/client/dom/blocks/branches.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@ export class BranchManager {
5757
} else {
5858
// effect is currently offscreen. put it in the DOM
5959
var offscreen = this.#offscreen.get(key);
60-
if (!offscreen) throw new Error('This should never happen!');
6160

62-
this.#onscreen.set(key, offscreen.effect);
63-
this.#offscreen.delete(key);
61+
if (offscreen) {
62+
this.#onscreen.set(key, offscreen.effect);
63+
this.#offscreen.delete(key);
6464

65-
// remove the anchor...
66-
/** @type {TemplateNode} */ (offscreen.fragment.lastChild).remove();
65+
// remove the anchor...
66+
/** @type {TemplateNode} */ (offscreen.fragment.lastChild).remove();
6767

68-
// ...and append the fragment
69-
this.anchor.before(offscreen.fragment);
68+
// ...and append the fragment
69+
this.anchor.before(offscreen.fragment);
70+
}
7071
}
7172

7273
this.#batches.delete(batch);
@@ -115,7 +116,7 @@ export class BranchManager {
115116
/**
116117
*
117118
* @param {any} key
118-
* @param {(target: TemplateNode) => void} fn
119+
* @param {null | ((target: TemplateNode) => void)} fn
119120
*/
120121
ensure(key, fn) {
121122
var batch = /** @type {Batch} */ (current_batch);
@@ -125,7 +126,7 @@ export class BranchManager {
125126
key = {};
126127
}
127128

128-
if (!this.#onscreen.has(key) && !this.#offscreen.has(key)) {
129+
if (fn && !this.#onscreen.has(key) && !this.#offscreen.has(key)) {
129130
var fragment = document.createDocumentFragment();
130131
var target = create_text();
131132

packages/svelte/src/internal/client/dom/blocks/if.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export function if_block(node, fn, elseif = false) {
3131

3232
/**
3333
* @param {boolean} condition,
34-
* @param {((anchor: Node) => void)} fn
34+
* @param {null | ((anchor: Node) => void)} fn
3535
*/
3636
function update_branch(condition, fn) {
3737
if (hydrating) {
@@ -65,7 +65,7 @@ export function if_block(node, fn, elseif = false) {
6565
});
6666

6767
if (!has_branch) {
68-
update_branch(false, noop);
68+
update_branch(false, null);
6969
}
7070
}, flags);
7171
}

packages/svelte/src/internal/client/dom/blocks/snippet.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ export function snippet(node, get_snippet, ...args) {
2828
var branches = new BranchManager(node);
2929

3030
block(() => {
31-
const snippet = get_snippet();
31+
const snippet = get_snippet() ?? null;
3232

33-
if (snippet == null) {
33+
if (DEV && snippet == null) {
3434
e.invalid_snippet();
3535
}
3636

37-
branches.ensure(snippet, (anchor) => snippet(anchor, ...args));
37+
branches.ensure(snippet, snippet && ((anchor) => snippet(anchor, ...args)));
3838
}, EFFECT_TRANSPARENT);
3939
}
4040

packages/svelte/src/internal/client/dom/blocks/svelte-component.js

Lines changed: 5 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { block, branch, pause_effect } from '../../reactivity/effects.js';
55
import { current_batch } from '../../reactivity/batch.js';
66
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
77
import { create_text, should_defer_append } from '../operations.js';
8+
import { BranchManager } from './branches.js';
9+
import { noop } from '../../../shared/utils.js';
810

911
/**
1012
* @template P
@@ -19,64 +21,10 @@ export function component(node, get_component, render_fn) {
1921
hydrate_next();
2022
}
2123

22-
var anchor = node;
23-
24-
/** @type {C} */
25-
var component;
26-
27-
/** @type {Effect | null} */
28-
var effect;
29-
30-
/** @type {DocumentFragment | null} */
31-
var offscreen_fragment = null;
32-
33-
/** @type {Effect | null} */
34-
var pending_effect = null;
35-
36-
function commit() {
37-
if (effect) {
38-
pause_effect(effect);
39-
effect = null;
40-
}
41-
42-
if (offscreen_fragment) {
43-
// remove the anchor
44-
/** @type {Text} */ (offscreen_fragment.lastChild).remove();
45-
46-
anchor.before(offscreen_fragment);
47-
offscreen_fragment = null;
48-
}
49-
50-
effect = pending_effect;
51-
pending_effect = null;
52-
}
24+
var branches = new BranchManager(node);
5325

5426
block(() => {
55-
if (component === (component = get_component())) return;
56-
57-
var defer = should_defer_append();
58-
59-
if (component) {
60-
var target = anchor;
61-
62-
if (defer) {
63-
offscreen_fragment = document.createDocumentFragment();
64-
offscreen_fragment.append((target = create_text()));
65-
if (effect) {
66-
/** @type {Batch} */ (current_batch).skipped_effects.add(effect);
67-
}
68-
}
69-
pending_effect = branch(() => render_fn(target, component));
70-
}
71-
72-
if (defer) {
73-
/** @type {Batch} */ (current_batch).add_callback(commit);
74-
} else {
75-
commit();
76-
}
27+
var component = get_component() ?? null;
28+
branches.ensure(component, component && ((target) => render_fn(target, component)));
7729
}, EFFECT_TRANSPARENT);
78-
79-
if (hydrating) {
80-
anchor = hydrate_node;
81-
}
8230
}

0 commit comments

Comments
 (0)