Skip to content

Commit 1ae7da0

Browse files
committed
refactor: simplify TeleportFragment nodes to directly hold content instead of always an array.
revert: c9f9b85
1 parent 654687b commit 1ae7da0

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

packages/runtime-vapor/__tests__/hydration.spec.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,9 +3034,12 @@ describe('Vapor Mode hydration', () => {
30343034
expect(teleport.anchor).toBe(container.lastChild)
30353035
expect(teleport.target).toBe(teleportContainer)
30363036
expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
3037-
const children = teleport.nodes[0] as Node[]
3038-
expect(children[0]).toBe(teleportContainer.childNodes[1])
3039-
expect(children[1]).toBe(teleportContainer.childNodes[2])
3037+
expect((teleport.nodes as Node[])[0]).toBe(
3038+
teleportContainer.childNodes[1],
3039+
)
3040+
expect((teleport.nodes as Node[])[1]).toBe(
3041+
teleportContainer.childNodes[2],
3042+
)
30403043
expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
30413044

30423045
expect(container.innerHTML).toMatchInlineSnapshot(
@@ -3149,14 +3152,16 @@ describe('Vapor Mode hydration', () => {
31493152

31503153
expect(teleport1.target).toBe(teleportContainer)
31513154
expect(teleport1.targetStart).toBe(teleportContainer.childNodes[0])
3152-
const children1 = teleport1.nodes[0] as Node[]
3153-
expect(children1[0]).toBe(teleportContainer.childNodes[1])
3155+
expect((teleport1.nodes as Node[])[0]).toBe(
3156+
teleportContainer.childNodes[1],
3157+
)
31543158
expect(teleport1.targetAnchor).toBe(teleportContainer.childNodes[3])
31553159

31563160
expect(teleport2.target).toBe(teleportContainer)
31573161
expect(teleport2.targetStart).toBe(teleportContainer.childNodes[4])
3158-
const children2 = teleport2.nodes[0] as Node[]
3159-
expect(children2[0]).toBe(teleportContainer.childNodes[5])
3162+
expect((teleport2.nodes as Node[])[0]).toBe(
3163+
teleportContainer.childNodes[5],
3164+
)
31603165
expect(teleport2.targetAnchor).toBe(teleportContainer.childNodes[7])
31613166

31623167
expect(container.innerHTML).toBe(
@@ -3240,9 +3245,8 @@ describe('Vapor Mode hydration', () => {
32403245
expect(blocks[0]).toBe(container.childNodes[1])
32413246

32423247
const teleport = blocks[1] as TeleportFragment
3243-
const children = teleport.nodes[0] as Node[]
3244-
expect(children[0]).toBe(container.childNodes[3])
3245-
expect(children[1]).toBe(container.childNodes[4])
3248+
expect((teleport.nodes as Node[])[0]).toBe(container.childNodes[3])
3249+
expect((teleport.nodes as Node[])[1]).toBe(container.childNodes[4])
32463250
expect(teleport.anchor).toBe(container.childNodes[5])
32473251
expect(teleport.target).toBe(teleportContainer)
32483252
expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
@@ -3327,7 +3331,7 @@ describe('Vapor Mode hydration', () => {
33273331
expect(teleport.anchor).toBe(container.childNodes[1])
33283332
expect(teleport.target).toBe(teleportContainer)
33293333
expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
3330-
expect(teleport.nodes[0]).toBe(teleportContainer.childNodes[1])
3334+
expect(teleport.nodes).toBe(teleportContainer.childNodes[1])
33313335
expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[2])
33323336
})
33333337

@@ -3355,11 +3359,11 @@ describe('Vapor Mode hydration', () => {
33553359
expect(teleport.targetStart).toBe(teleportContainer.childNodes[0])
33563360
expect(teleport.targetAnchor).toBe(teleportContainer.childNodes[3])
33573361

3358-
const childTeleport = teleport.nodes[0] as TeleportFragment
3362+
const childTeleport = teleport.nodes as TeleportFragment
33593363
expect(childTeleport.anchor).toBe(teleportContainer.childNodes[2])
33603364
expect(childTeleport.targetStart).toBe(teleportContainer.childNodes[4])
33613365
expect(childTeleport.targetAnchor).toBe(teleportContainer.childNodes[6])
3362-
expect(childTeleport.nodes[0]).toBe(teleportContainer.childNodes[5])
3366+
expect(childTeleport.nodes).toBe(teleportContainer.childNodes[5])
33633367
})
33643368

33653369
test('unmount (full integration)', async () => {

packages/runtime-vapor/src/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -875,7 +875,7 @@ export function getRootElement(
875875
return getRootElement(block.block, recurse)
876876
}
877877

878-
if (isFragment(block)) {
878+
if (isFragment(block) && !(block instanceof TeleportFragment)) {
879879
if (block instanceof DynamicFragment) block.root = true
880880
const { nodes } = block
881881
if (nodes instanceof Element && (nodes as any).$root) {

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const VaporTeleportImpl = {
4343
},
4444
}
4545

46-
export class TeleportFragment extends VaporFragment<Block[]> {
46+
export class TeleportFragment extends VaporFragment {
4747
anchor?: Node
4848
private rawProps?: LooseRawProps
4949
private resolvedProps?: TeleportProps
@@ -98,7 +98,7 @@ export class TeleportFragment extends VaporFragment<Block[]> {
9898
)
9999
})
100100

101-
const nodes = this.nodes[0]
101+
const nodes = this.nodes
102102
// register updateCssVars to root fragments's update hooks so that
103103
// it will be called when root fragment changed
104104
if (this.parentComponent && this.parentComponent.ut) {
@@ -129,16 +129,14 @@ export class TeleportFragment extends VaporFragment<Block[]> {
129129
private handleChildrenUpdate(children: Block): void {
130130
// not mounted yet
131131
if (!this.parent || isHydrating) {
132-
// Teleport nodes are always an array, preventing attrs fallthrough
133-
// consistent with VDOM Teleport behavior.
134-
this.nodes = [children]
132+
this.nodes = children
135133
return
136134
}
137135

138136
// teardown previous nodes
139137
remove(this.nodes, this.mountContainer!)
140138
// mount new nodes
141-
insert((this.nodes = [children]), this.mountContainer!, this.mountAnchor!)
139+
insert((this.nodes = children), this.mountContainer!, this.mountAnchor!)
142140
}
143141

144142
private mount(parent: ParentNode, anchor: Node | null): void {

packages/runtime-vapor/src/hmr.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,12 @@ export function updateParentTeleportOnHmrReload(
9999
const teleport = instance.parentTeleport
100100
if (teleport) {
101101
newInstance.parentTeleport = teleport
102-
const block = teleport.nodes[0]
103-
if (block === instance) {
104-
teleport.nodes[0] = newInstance
105-
} else if (isArray(block)) {
106-
for (let i = 0; i < block.length; i++) {
107-
if (block[i] === instance) {
108-
block[i] = newInstance
102+
if (teleport.nodes === instance) {
103+
teleport.nodes = newInstance
104+
} else if (isArray(teleport.nodes)) {
105+
for (let i = 0; i < teleport.nodes.length; i++) {
106+
if (teleport.nodes[i] === instance) {
107+
teleport.nodes[i] = newInstance
109108
break
110109
}
111110
}

0 commit comments

Comments
 (0)