Skip to content

Commit 95aa627

Browse files
committed
wip: save
1 parent c87af7c commit 95aa627

File tree

4 files changed

+57
-20
lines changed

4 files changed

+57
-20
lines changed

packages/runtime-vapor/src/apiCreateIf.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type Block, type BlockFn, insert } from './block'
1+
import { type Block, type BlockFn, getComponentsInBlock, insert } from './block'
22
import { advanceHydrationNode, isHydrating } from './dom/hydration'
33
import {
44
insertionAnchor,
@@ -7,6 +7,7 @@ import {
77
} from './insertionState'
88
import { renderEffect } from './renderEffect'
99
import { DynamicFragment } from './fragment'
10+
import { mountComponent } from './component'
1011

1112
export function createIf(
1213
condition: () => any,
@@ -30,6 +31,15 @@ export function createIf(
3031
if (!isHydrating) {
3132
if (_insertionParent) insert(frag, _insertionParent, _insertionAnchor)
3233
} else {
34+
if (_insertionParent) {
35+
const components = getComponentsInBlock(frag)
36+
if (components.length) {
37+
components.forEach(i =>
38+
mountComponent(i, _insertionParent, _insertionAnchor, false),
39+
)
40+
}
41+
}
42+
3343
// After block node hydration is completed, advance currentHydrationNode to
3444
// _insertionParent's next sibling if _insertionAnchor has a value
3545
// _insertionAnchor values:

packages/runtime-vapor/src/block.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,17 @@ export function setComponentScopeId(instance: VaporComponentInstance): void {
230230
}
231231
}
232232
}
233+
234+
export function getComponentsInBlock(block: Block): VaporComponentInstance[] {
235+
const components: VaporComponentInstance[] = []
236+
if (isArray(block)) {
237+
for (const item of block) {
238+
components.push(...getComponentsInBlock(item))
239+
}
240+
} else if (isVaporComponent(block)) {
241+
components.push(block)
242+
} else if (isFragment(block)) {
243+
components.push(...getComponentsInBlock(block.nodes))
244+
}
245+
return components
246+
}

packages/runtime-vapor/src/component.ts

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from '@vue/runtime-dom'
3030
import {
3131
type Block,
32+
getComponentsInBlock,
3233
insert,
3334
isBlock,
3435
remove,
@@ -43,13 +44,7 @@ import {
4344
setActiveSub,
4445
unref,
4546
} from '@vue/reactivity'
46-
import {
47-
EMPTY_OBJ,
48-
invokeArrayFns,
49-
isArray,
50-
isFunction,
51-
isString,
52-
} from '@vue/shared'
47+
import { EMPTY_OBJ, invokeArrayFns, isFunction, isString } from '@vue/shared'
5348
import {
5449
type DynamicPropsSource,
5550
type RawProps,
@@ -653,24 +648,25 @@ export function mountComponent(
653648
instance: VaporComponentInstance,
654649
parent: ParentNode,
655650
anchor?: Node | null | 0,
651+
needInsert: boolean = true,
656652
): void {
657653
if (__DEV__) {
658654
startMeasure(instance, `mount`)
659655
}
660656
if (instance.bm) invokeArrayFns(instance.bm)
661-
const block = instance.block
662-
if (isHydrating) {
663-
if (
664-
!(block instanceof Node) ||
665-
(isArray(block) && block.some(b => !(b instanceof Node)))
666-
) {
657+
if (needInsert) {
658+
const block = instance.block
659+
if (isHydrating) {
660+
// ensure components inside the block get mounted
661+
const components = getComponentsInBlock(block)
662+
if (components.length) {
663+
components.forEach(i => mountComponent(i, parent, anchor, false))
664+
}
665+
} else {
667666
insert(block, parent, anchor)
667+
setComponentScopeId(instance)
668668
}
669-
} else {
670-
insert(block, parent, anchor)
671-
setComponentScopeId(instance)
672669
}
673-
674670
if (instance.m) queuePostFlushCb(() => invokeArrayFns(instance.m!))
675671
instance.isMounted = true
676672
if (__DEV__) {

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
import { EMPTY_OBJ, NO, hasOwn, isArray, isFunction } from '@vue/shared'
2-
import { type Block, type BlockFn, insert, setScopeId } from './block'
2+
import {
3+
type Block,
4+
type BlockFn,
5+
getComponentsInBlock,
6+
insert,
7+
setScopeId,
8+
} from './block'
39
import { rawPropsProxyHandlers } from './componentProps'
410
import { currentInstance, isRef } from '@vue/runtime-dom'
5-
import type { LooseRawProps, VaporComponentInstance } from './component'
11+
import {
12+
type LooseRawProps,
13+
type VaporComponentInstance,
14+
mountComponent,
15+
} from './component'
616
import { renderEffect } from './renderEffect'
717
import {
818
insertionAnchor,
@@ -169,6 +179,13 @@ export function createSlot(
169179
} else {
170180
if (fragment.insert) {
171181
;(fragment as VaporFragment).hydrate!()
182+
} else if (_insertionParent) {
183+
const components = getComponentsInBlock(fragment)
184+
if (components.length) {
185+
components.forEach(i =>
186+
mountComponent(i, _insertionParent, _insertionAnchor, false),
187+
)
188+
}
172189
}
173190
if (_insertionAnchor !== undefined) {
174191
advanceHydrationNode(_insertionParent!)

0 commit comments

Comments
 (0)