Skip to content

Commit d168bb1

Browse files
committed
key blocks
1 parent ff48b23 commit d168bb1

File tree

2 files changed

+14
-62
lines changed

2 files changed

+14
-62
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** @import { Effect, TemplateNode } from '#client' */
2+
import { is_runes } from '../../context.js';
23
import { Batch, current_batch } from '../../reactivity/batch.js';
34
import {
45
branch,
@@ -30,6 +31,8 @@ export class BranchManager {
3031
/** @type {Map<Key, Branch>} */
3132
#offscreen = new Map();
3233

34+
#legacy = !is_runes();
35+
3336
/**
3437
*
3538
* @param {TemplateNode} anchor
@@ -117,6 +120,11 @@ export class BranchManager {
117120
ensure(key, fn) {
118121
var batch = /** @type {Batch} */ (current_batch);
119122

123+
// key blocks in Svelte <5 had stupid semantics
124+
if (this.#legacy && typeof key === 'object') {
125+
key = {};
126+
}
127+
120128
if (!this.#onscreen.has(key) && !this.#offscreen.has(key)) {
121129
var fragment = document.createDocumentFragment();
122130
var target = create_text();
Lines changed: 6 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
/** @import { Effect, TemplateNode } from '#client' */
2-
/** @import { Batch } from '../../reactivity/batch.js'; */
3-
import { UNINITIALIZED } from '../../../../constants.js';
4-
import { block, branch, pause_effect } from '../../reactivity/effects.js';
5-
import { not_equal, safe_not_equal } from '../../reactivity/equality.js';
6-
import { is_runes } from '../../context.js';
7-
import { hydrate_next, hydrate_node, hydrating } from '../hydration.js';
8-
import { create_text, should_defer_append } from '../operations.js';
9-
import { current_batch } from '../../reactivity/batch.js';
1+
/** @import { TemplateNode } from '#client' */
2+
import { block } from '../../reactivity/effects.js';
3+
import { hydrate_next, hydrating } from '../hydration.js';
4+
import { BranchManager } from './branches.js';
105

116
/**
127
* @template V
@@ -20,60 +15,9 @@ export function key(node, get_key, render_fn) {
2015
hydrate_next();
2116
}
2217

23-
var anchor = node;
24-
25-
/** @type {V | typeof UNINITIALIZED} */
26-
var key = UNINITIALIZED;
27-
28-
/** @type {Effect} */
29-
var effect;
30-
31-
/** @type {Effect} */
32-
var pending_effect;
33-
34-
/** @type {DocumentFragment | null} */
35-
var offscreen_fragment = null;
36-
37-
var changed = is_runes() ? not_equal : safe_not_equal;
38-
39-
function commit() {
40-
if (effect) {
41-
pause_effect(effect);
42-
}
43-
44-
if (offscreen_fragment !== null) {
45-
// remove the anchor
46-
/** @type {Text} */ (offscreen_fragment.lastChild).remove();
47-
48-
anchor.before(offscreen_fragment);
49-
offscreen_fragment = null;
50-
}
51-
52-
effect = pending_effect;
53-
}
18+
var branches = new BranchManager(node);
5419

5520
block(() => {
56-
if (changed(key, (key = get_key()))) {
57-
var target = anchor;
58-
59-
var defer = should_defer_append();
60-
61-
if (defer) {
62-
offscreen_fragment = document.createDocumentFragment();
63-
offscreen_fragment.append((target = create_text()));
64-
}
65-
66-
pending_effect = branch(() => render_fn(target));
67-
68-
if (defer) {
69-
/** @type {Batch} */ (current_batch).add_callback(commit);
70-
} else {
71-
commit();
72-
}
73-
}
21+
branches.ensure(get_key(), render_fn);
7422
});
75-
76-
if (hydrating) {
77-
anchor = hydrate_node;
78-
}
7923
}

0 commit comments

Comments
 (0)