Skip to content

Commit fd6f163

Browse files
committed
wip: save
1 parent 7ab1a30 commit fd6f163

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,29 @@ export const VaporTeleportImpl = {
5656
return frag.parent !== frag.currentParent ? [] : frag.nodes
5757
}
5858

59-
// for HMR re-render
59+
// for HMR rerender
6060
const instance = currentInstance as VaporComponentInstance
6161
;(
6262
instance!.hmrRerenderEffects || (instance!.hmrRerenderEffects = [])
6363
).push(() => {
6464
// remove the teleport content
65-
frag.remove(frag.anchor.parentNode!)
65+
frag.remove()
6666

6767
// stop effects
6868
updateChildrenEffect.stop()
6969
updateEffect.stop()
7070
})
71+
72+
// for HMR reload
73+
const nodes = frag.nodes
74+
if (isVaporComponent(nodes)) {
75+
instanceToTeleportMap.set(nodes, frag)
76+
} else if (isArray(nodes)) {
77+
nodes.forEach(
78+
node =>
79+
isVaporComponent(node) && instanceToTeleportMap.set(node, frag),
80+
)
81+
}
7182
}
7283

7384
return frag
@@ -105,24 +116,13 @@ class TeleportFragment extends VaporFragment {
105116
// not mounted yet
106117
if (!this.parent) {
107118
this.nodes = children
108-
} else {
109-
// teardown previous nodes
110-
remove(this.nodes, this.currentParent)
111-
// mount new nodes
112-
insert((this.nodes = children), this.currentParent, this.currentAnchor)
119+
return
113120
}
114121

115-
if (__DEV__) {
116-
if (isVaporComponent(children)) {
117-
instanceToTeleportMap.set(children, this)
118-
} else if (isArray(children)) {
119-
children.forEach(node => {
120-
if (isVaporComponent(node)) {
121-
instanceToTeleportMap.set(node, this)
122-
}
123-
})
124-
}
125-
}
122+
// teardown previous nodes
123+
remove(this.nodes, this.currentParent)
124+
// mount new nodes
125+
insert((this.nodes = children), this.currentParent, this.currentAnchor)
126126
}
127127

128128
update(props: TeleportProps): void {
@@ -187,7 +187,7 @@ class TeleportFragment extends VaporFragment {
187187
}
188188
}
189189

190-
remove = (parent: ParentNode | undefined): void => {
190+
remove = (parent: ParentNode | undefined = this.parent!): void => {
191191
// remove nodes
192192
if (this.nodes) {
193193
remove(this.nodes, this.currentParent)
@@ -244,8 +244,9 @@ export const VaporTeleport = VaporTeleportImpl as unknown as {
244244

245245
/**
246246
* dev only
247-
* when the root child component updates, synchronously update
248-
* the TeleportFragment's nodes.
247+
* during root component HMR reload, since the old component will be unmounted
248+
* and a new one will be mounted, we need to update the teleport's nodes
249+
* to ensure they are up to date.
249250
*/
250251
export function handleTeleportRootComponentHmrReload(
251252
instance: VaporComponentInstance,

0 commit comments

Comments
 (0)