Skip to content

feat(runtime-vapor): add reset callback for setInsertionState #13424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vapor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/runtime-vapor/src/dom/hydration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { warn } from '@vue/runtime-dom'
import {
insertionAnchor,
insertionParent,
resetInsertionState,
setInsertionState,
} from '../insertionState'
import { child, next } from './node'
Expand All @@ -27,7 +26,7 @@ export function withHydration(container: ParentNode, fn: () => void): void {
isHydrating = true
setInsertionState(container, 0)
const res = fn()
resetInsertionState()
setInsertionState()
currentHydrationNode = null
isHydrating = false
return res
Expand Down Expand Up @@ -124,6 +123,6 @@ function locateHydrationNodeImpl() {
warn('Hydration mismatch in ', insertionParent)
}

resetInsertionState()
setInsertionState()
currentHydrationNode = node
}
14 changes: 9 additions & 5 deletions packages/runtime-vapor/src/insertionState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ export let insertionAnchor: Node | 0 | undefined
* This function is called before a block type that requires insertion
* (component, slot outlet, if, for) is created. The state is used for actual
* insertion on client-side render, and used for node adoption during hydration.
*
* @returns A function that restores the previous insertion state when called.
*/
export function setInsertionState(parent: ParentNode, anchor?: Node | 0): void {
export function setInsertionState(parent?: ParentNode, anchor?: Node | 0) {
const prevParent = insertionParent
const prevAnchor = insertionAnchor
insertionParent = parent
insertionAnchor = anchor
}

export function resetInsertionState(): void {
insertionParent = insertionAnchor = undefined
return (): void => {
insertionParent = prevParent
insertionAnchor = prevAnchor
}
}