Skip to content

Commit 8a1ec85

Browse files
committed
wip: simplify hydration boundary init
1 parent 78ac66e commit 8a1ec85

File tree

4 files changed

+16
-50
lines changed

4 files changed

+16
-50
lines changed

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,7 @@ export const createFor = (
124124
isMounted = true
125125
if (isHydrating) {
126126
const hydrationStart = currentHydrationNode!
127-
const restoreBoundary = pushHydrationBoundary({
128-
close: null,
129-
preserve: null,
130-
})
127+
const restoreBoundary = pushHydrationBoundary({})
131128
let nextNode
132129
const emptyLocalRange =
133130
isComment(hydrationStart, ']') &&

packages/runtime-vapor/src/components/Teleport.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -442,11 +442,7 @@ export class TeleportFragment extends VaporFragment {
442442

443443
hydrate = (): void => {
444444
if (!isHydrating) return
445-
const restoreBoundary = pushHydrationBoundary({
446-
close: null,
447-
preserve: null,
448-
cleanupOnPop: false,
449-
})
445+
const restoreBoundary = pushHydrationBoundary({})
450446
try {
451447
const target = (this.target = resolveTeleportTarget(
452448
this.resolvedProps!,

packages/runtime-vapor/src/dom/hydration.ts

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,15 @@ export let currentHydrationNode: Node | null = null
3030

3131
export interface HydrationBoundary {
3232
// Structural close marker the current owner must not cross during cleanup.
33-
close: Node | null
33+
close?: Node | null
3434
// Marker mismatch recovery must insert before instead of replacing.
35-
preserve: Node | null
35+
preserve?: Node | null
3636
// Whether restore should trim unclaimed SSR nodes up to `close`.
3737
cleanupOnPop?: boolean
3838
}
3939

4040
export let currentHydrationBoundary: HydrationBoundary | null = null
4141

42-
function canReachBoundaryClose(node: Node, close: Node): boolean {
43-
let cur: Node | null = node
44-
while (cur) {
45-
if (cur === close) return true
46-
cur = locateNextNode(cur)
47-
}
48-
return false
49-
}
50-
5142
function finalizeHydrationBoundary(boundary: HydrationBoundary): void {
5243
const close = boundary.close
5344
let node = currentHydrationNode
@@ -56,9 +47,14 @@ function finalizeHydrationBoundary(boundary: HydrationBoundary): void {
5647
return
5748
}
5849

59-
if (!canReachBoundaryClose(node, close)) {
60-
return
50+
// This boundary only owns cleanup while the current cursor is still inside
51+
// its SSR range. If nested hydration has already advanced past `close`, stop
52+
// here so we don't delete sibling or parent-owned SSR nodes by mistake.
53+
let cur: Node | null = node
54+
while (cur && cur !== close) {
55+
cur = locateNextNode(cur)
6156
}
57+
if (!cur) return
6258

6359
warnHydrationChildrenMismatch((close as Node).parentElement)
6460

@@ -157,11 +153,7 @@ function performHydration<T>(
157153
export function withHydration(container: ParentNode, fn: () => void): void {
158154
const setup = () => {
159155
setInsertionState(container)
160-
currentHydrationBoundary = {
161-
close: null,
162-
preserve: null,
163-
cleanupOnPop: false,
164-
}
156+
currentHydrationBoundary = {}
165157
}
166158
const cleanup = () => resetInsertionState()
167159
return performHydration(fn, setup, cleanup)
@@ -170,11 +162,7 @@ export function withHydration(container: ParentNode, fn: () => void): void {
170162
export function hydrateNode(node: Node, fn: () => void): void {
171163
const setup = () => {
172164
currentHydrationNode = node
173-
currentHydrationBoundary = {
174-
close: null,
175-
preserve: null,
176-
cleanupOnPop: false,
177-
}
165+
currentHydrationBoundary = {}
178166
}
179167
const cleanup = () => {}
180168
return performHydration(fn, setup, cleanup)
@@ -190,11 +178,7 @@ export function enterHydration(node: Node): () => void {
190178
const prevHydrationNode = currentHydrationNode
191179
const prevHydrationBoundary = currentHydrationBoundary
192180
currentHydrationNode = node
193-
currentHydrationBoundary = {
194-
close: null,
195-
preserve: null,
196-
cleanupOnPop: false,
197-
}
181+
currentHydrationBoundary = {}
198182

199183
return () => {
200184
currentHydrationNode = prevHydrationNode

packages/runtime-vapor/src/fragment.ts

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,11 +374,7 @@ export class DynamicFragment extends VaporFragment {
374374
if (this.isAnchorPending) return
375375

376376
let advanceAfterRestore: Node | null = null
377-
const restoreBoundary = pushHydrationBoundary({
378-
close: null,
379-
preserve: null,
380-
cleanupOnPop: false,
381-
})
377+
const restoreBoundary = pushHydrationBoundary({})
382378

383379
try {
384380
// reuse `<!---->` as anchor
@@ -389,7 +385,6 @@ export class DynamicFragment extends VaporFragment {
389385
patchCurrentHydrationBoundary({
390386
close: currentHydrationNode,
391387
preserve: this.anchor,
392-
cleanupOnPop: false,
393388
})
394389
advanceHydrationNode(currentHydrationNode)
395390
return
@@ -442,7 +437,6 @@ export class DynamicFragment extends VaporFragment {
442437
if (nextNode) {
443438
patchCurrentHydrationBoundary({
444439
close: nextNode,
445-
preserve: null,
446440
cleanupOnPop: true,
447441
})
448442
} else {
@@ -611,15 +605,10 @@ export class SlotFragment extends DynamicFragment {
611605
pushedEndAnchor = true
612606
restoreBoundary = pushHydrationBoundary({
613607
close: endAnchor,
614-
preserve: null,
615608
cleanupOnPop: true,
616609
})
617610
} else {
618-
restoreBoundary = pushHydrationBoundary({
619-
close: null,
620-
preserve: null,
621-
cleanupOnPop: true,
622-
})
611+
restoreBoundary = pushHydrationBoundary({ cleanupOnPop: true })
623612
}
624613
}
625614

0 commit comments

Comments
 (0)