Skip to content

Commit 74ba484

Browse files
committed
feat(runtime-vapor): add reset callback for setInsertionState
1 parent 1ef6e6e commit 74ba484

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { warn } from '@vue/runtime-dom'
22
import {
33
insertionAnchor,
44
insertionParent,
5-
resetInsertionState,
65
setInsertionState,
76
} from '../insertionState'
87
import { child, next } from './node'
@@ -27,7 +26,7 @@ export function withHydration(container: ParentNode, fn: () => void): void {
2726
isHydrating = true
2827
setInsertionState(container, 0)
2928
const res = fn()
30-
resetInsertionState()
29+
setInsertionState()
3130
currentHydrationNode = null
3231
isHydrating = false
3332
return res
@@ -124,6 +123,6 @@ function locateHydrationNodeImpl() {
124123
warn('Hydration mismatch in ', insertionParent)
125124
}
126125

127-
resetInsertionState()
126+
setInsertionState()
128127
currentHydrationNode = node
129128
}

packages/runtime-vapor/src/insertionState.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@ export let insertionAnchor: Node | 0 | undefined
55
* This function is called before a block type that requires insertion
66
* (component, slot outlet, if, for) is created. The state is used for actual
77
* insertion on client-side render, and used for node adoption during hydration.
8+
*
9+
* @returns A function that restores the previous insertion state when called.
810
*/
9-
export function setInsertionState(parent: ParentNode, anchor?: Node | 0): void {
11+
export function setInsertionState(parent?: ParentNode, anchor?: Node | 0) {
12+
const prevParent = insertionParent
13+
const prevAnchor = insertionAnchor
1014
insertionParent = parent
1115
insertionAnchor = anchor
12-
}
13-
14-
export function resetInsertionState(): void {
15-
insertionParent = insertionAnchor = undefined
16+
return (): void => {
17+
insertionParent = prevParent
18+
insertionAnchor = prevAnchor
19+
}
1620
}

0 commit comments

Comments
 (0)