Skip to content

Commit 8459098

Browse files
authored
chore: simplify effect.dom stuff (#11752)
1 parent 5765752 commit 8459098

File tree

3 files changed

+25
-84
lines changed

3 files changed

+25
-84
lines changed

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

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { current_effect, get } from '../../runtime.js';
44
import { is_array } from '../../utils.js';
55
import { hydrate_nodes, hydrating } from '../hydration.js';
66
import { create_fragment_from_html, remove } from '../reconciler.js';
7-
import { push_template_node } from '../template.js';
87

98
/**
109
* @param {import('#client').Effect} effect
@@ -38,7 +37,7 @@ export function html(anchor, get_value, svg, mathml) {
3837
let value = derived(get_value);
3938

4039
render_effect(() => {
41-
var dom = html_to_dom(anchor, parent_effect, get(value), svg, mathml);
40+
var dom = html_to_dom(anchor, get(value), svg, mathml);
4241

4342
if (dom) {
4443
return () => {
@@ -56,13 +55,12 @@ export function html(anchor, get_value, svg, mathml) {
5655
* inserts it before the target anchor and returns the new nodes.
5756
* @template V
5857
* @param {Element | Text | Comment} target
59-
* @param {import('#client').Effect | null} effect
6058
* @param {V} value
6159
* @param {boolean} svg
6260
* @param {boolean} mathml
6361
* @returns {Element | Comment | (Element | Comment | Text)[]}
6462
*/
65-
function html_to_dom(target, effect, value, svg, mathml) {
63+
function html_to_dom(target, value, svg, mathml) {
6664
if (hydrating) return hydrate_nodes;
6765

6866
var html = value + '';
@@ -81,9 +79,6 @@ function html_to_dom(target, effect, value, svg, mathml) {
8179
if (node.childNodes.length === 1) {
8280
var child = /** @type {Text | Element | Comment} */ (node.firstChild);
8381
target.before(child);
84-
if (effect !== null) {
85-
push_template_node(child, effect);
86-
}
8782
return child;
8883
}
8984

@@ -97,9 +92,5 @@ function html_to_dom(target, effect, value, svg, mathml) {
9792
target.before(node);
9893
}
9994

100-
if (effect !== null) {
101-
push_template_node(nodes, effect);
102-
}
103-
10495
return nodes;
10596
}

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

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,13 @@ import {
66
branch,
77
destroy_effect,
88
pause_effect,
9-
render_effect,
109
resume_effect
1110
} from '../../reactivity/effects.js';
12-
import { is_array } from '../../utils.js';
1311
import { set_should_intro } from '../../render.js';
1412
import { current_each_item, set_current_each_item } from './each.js';
15-
import { current_component_context, current_effect } from '../../runtime.js';
16-
import { push_template_node } from '../template.js';
13+
import { current_component_context } from '../../runtime.js';
1714
import { DEV } from 'esm-env';
1815

19-
/**
20-
* @param {import('#client').Effect} effect
21-
* @param {Element} from
22-
* @param {Element} to
23-
* @returns {void}
24-
*/
25-
function swap_block_dom(effect, from, to) {
26-
const dom = effect.dom;
27-
28-
if (is_array(dom)) {
29-
for (let i = 0; i < dom.length; i++) {
30-
if (dom[i] === from) {
31-
dom[i] = to;
32-
break;
33-
}
34-
}
35-
} else if (dom === from) {
36-
effect.dom = to;
37-
}
38-
}
39-
4016
/**
4117
* @param {Comment} anchor
4218
* @param {() => string} get_tag
@@ -47,8 +23,6 @@ function swap_block_dom(effect, from, to) {
4723
* @returns {void}
4824
*/
4925
export function element(anchor, get_tag, is_svg, render_fn, get_namespace, location) {
50-
const parent_effect = /** @type {import('#client').Effect} */ (current_effect);
51-
5226
const filename = DEV && location && current_component_context?.function.filename;
5327

5428
/** @type {string | null} */
@@ -104,7 +78,6 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
10478

10579
if (next_tag && next_tag !== current_tag) {
10680
effect = branch(() => {
107-
const prev_element = element;
10881
element = hydrating
10982
? /** @type {Element} */ (hydrate_nodes[0])
11083
: ns
@@ -144,12 +117,9 @@ export function element(anchor, get_tag, is_svg, render_fn, get_namespace, locat
144117

145118
anchor.before(element);
146119

147-
if (prev_element) {
148-
swap_block_dom(parent_effect, prev_element, element);
149-
prev_element.remove();
150-
} else if (!hydrating) {
151-
push_template_node(element, parent_effect);
152-
}
120+
return () => {
121+
element?.remove();
122+
};
153123
});
154124
}
155125

packages/svelte/src/internal/client/dom/template.js

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,15 @@ import { is_array } from '../utils.js';
99
/**
1010
* @template {import("#client").TemplateNode | import("#client").TemplateNode[]} T
1111
* @param {T} dom
12-
* @param {import("#client").Effect} effect
1312
* @returns {T}
1413
*/
15-
export function push_template_node(
16-
dom,
17-
effect = /** @type {import('#client').Effect} */ (current_effect)
18-
) {
19-
var current_dom = effect.dom;
20-
if (current_dom === null) {
21-
effect.dom = dom;
22-
} else {
23-
if (!is_array(current_dom)) {
24-
current_dom = effect.dom = [current_dom];
25-
}
14+
function push_template_node(dom) {
15+
var effect = /** @type {import('#client').Effect} */ (current_effect);
2616

27-
if (is_array(dom)) {
28-
current_dom.push(...dom);
29-
} else {
30-
current_dom.push(dom);
31-
}
17+
if (effect.dom === null) {
18+
effect.dom = dom;
3219
}
20+
3321
return dom;
3422
}
3523

@@ -55,15 +43,8 @@ export function template(content, flags) {
5543
node = create_fragment_from_html(content);
5644
if (!is_fragment) node = /** @type {Node} */ (node.firstChild);
5745
}
58-
var clone = use_import_node ? document.importNode(node, true) : node.cloneNode(true);
5946

60-
push_template_node(
61-
is_fragment
62-
? /** @type {import('#client').TemplateNode[]} */ ([...clone.childNodes])
63-
: /** @type {import('#client').TemplateNode} */ (clone)
64-
);
65-
66-
return clone;
47+
return use_import_node ? document.importNode(node, true) : node.cloneNode(true);
6748
};
6849
}
6950

@@ -123,15 +104,7 @@ export function ns_template(content, flags, ns = 'svg') {
123104
}
124105
}
125106

126-
var clone = node.cloneNode(true);
127-
128-
push_template_node(
129-
is_fragment
130-
? /** @type {import('#client').TemplateNode[]} */ ([...clone.childNodes])
131-
: /** @type {import('#client').TemplateNode} */ (clone)
132-
);
133-
134-
return clone;
107+
return node.cloneNode(true);
135108
};
136109
}
137110

@@ -206,7 +179,7 @@ function run_scripts(node) {
206179
*/
207180
/*#__NO_SIDE_EFFECTS__*/
208181
export function text(anchor) {
209-
if (!hydrating) return push_template_node(empty());
182+
if (!hydrating) return empty();
210183

211184
var node = hydrate_nodes[0];
212185

@@ -227,7 +200,6 @@ export function comment() {
227200
var frag = document.createDocumentFragment();
228201
var anchor = empty();
229202
frag.append(anchor);
230-
push_template_node([anchor]);
231203

232204
return frag;
233205
}
@@ -236,10 +208,18 @@ export function comment() {
236208
* Assign the created (or in hydration mode, traversed) dom elements to the current block
237209
* and insert the elements into the dom (in client mode).
238210
* @param {Text | Comment | Element} anchor
239-
* @param {import('#client').Dom} dom
211+
* @param {DocumentFragment | Element | Comment} node
240212
*/
241-
export function append(anchor, dom) {
213+
export function append(anchor, node) {
242214
if (!hydrating) {
243-
anchor.before(/** @type {Node} */ (dom));
215+
/** @type {import('#client').Dom} */
216+
const dom =
217+
node.nodeType === 11
218+
? /** @type {import('#client').TemplateNode[]} */ ([...node.childNodes])
219+
: /** @type {Element | Comment} */ (node);
220+
221+
/** @type {import('#client').Effect} */ (current_effect).dom = dom;
222+
223+
anchor.before(/** @type {Node} */ (node));
244224
}
245225
}

0 commit comments

Comments
 (0)