Skip to content

Commit 79e5e1d

Browse files
committed
Simplifyinit_hydrate sorting logic
1 parent 4f161fb commit 79e5e1d

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

src/runtime/internal/dom.ts

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export function end_hydrating() {
1414
type NodeEx = Node & {
1515
claim_order?: number,
1616
hydrate_init? : true,
17-
is_in_lis?: true,
1817
actual_end_child?: Node,
1918
childNodes: NodeListOf<NodeEx>,
2019
};
@@ -85,34 +84,32 @@ function init_hydrate(target: NodeEx) {
8584

8685
// The longest increasing subsequence of nodes (initially reversed)
8786
const lis: NodeEx2[] = [];
87+
// The rest of the nodes, nodes that will be moved
88+
const toMove: NodeEx2[] = [];
89+
let last = children.length - 1;
8890
for (let cur = m[longest] + 1; cur != 0; cur = p[cur - 1]) {
89-
const node = children[cur - 1];
90-
lis.push(node);
91-
node.is_in_lis = true;
91+
lis.push(children[cur - 1]);
92+
for (; last >= cur; last--) {
93+
toMove.push(children[last]);
94+
}
95+
last--;
96+
}
97+
for (; last >= 0; last--) {
98+
toMove.push(children[last]);
9299
}
93-
lis.reverse();
100+
lis.reverse();
94101

95-
// Move all nodes that aren't in the longest increasing subsequence
96-
const toMove = lis.map(() => [] as NodeEx2[]);
97-
// For the nodes at the end
98-
toMove.push([]);
99-
for (let i = 0; i < children.length; i++) {
100-
const node = children[i];
101-
if (!node.is_in_lis) {
102-
const idx = upper_bound(0, lis.length, idx => lis[idx].claim_order, node.claim_order);
103-
toMove[idx].push(node);
102+
// We sort the nodes being moved to guarantee that their insertion order matches the claim order
103+
toMove.sort((a, b) => a.claim_order - b.claim_order);
104+
105+
// Finally, we move the nodes
106+
for (let i = 0, j = 0; i < toMove.length; i++) {
107+
while (j < lis.length && toMove[i].claim_order >= lis[j].claim_order) {
108+
j++;
104109
}
110+
const anchor = j < lis.length ? lis[j] : null;
111+
target.insertBefore(toMove[i], anchor);
105112
}
106-
107-
toMove.forEach((lst, idx) => {
108-
// We sort the nodes being moved to guarantee that their insertion order matches the claim order
109-
lst.sort((a, b) => a.claim_order - b.claim_order);
110-
111-
const anchor = idx < lis.length ? lis[idx] : null;
112-
lst.forEach(n => {
113-
target.insertBefore(n, anchor);
114-
});
115-
});
116113
}
117114

118115
export function append(target: NodeEx, node: NodeEx) {

0 commit comments

Comments
 (0)