Skip to content

Commit b8ceb89

Browse files
committed
chore: tweaks
1 parent a65da3a commit b8ceb89

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

packages/runtime-vapor/src/apiCreateFor.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,6 @@ export const createFor = (
152152
// render fallback nodes
153153
if (frag.fallback) {
154154
insert((frag.nodes[0] = frag.fallback()), parent!, parentAnchor)
155-
oldBlocks = []
156155
isFallback = true
157156
}
158157
} else if (!getKey) {
@@ -341,9 +340,9 @@ export const createFor = (
341340

342341
if (!isFallback) {
343342
frag.nodes = [(oldBlocks = newBlocks)]
344-
if (parentAnchor) {
345-
frag.nodes.push(parentAnchor)
346-
}
343+
if (parentAnchor) frag.nodes.push(parentAnchor)
344+
} else {
345+
oldBlocks = []
347346
}
348347
setActiveSub(prevSub)
349348
}

packages/runtime-vapor/src/block.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,53 @@ export class DynamicFragment extends VaporFragment {
7373
}
7474

7575
if (this.fallback) {
76-
parent && remove(this.nodes, parent)
77-
const scope = this.scope || (this.scope = new EffectScope())
78-
scope.run(() => {
79-
// handle nested fragment
80-
if (isFragment(this.nodes)) {
81-
ensureFallback(this.nodes, this.fallback!)
82-
} else if (!isValidBlock(this.nodes)) {
83-
this.nodes = this.fallback!() || []
84-
}
85-
})
86-
87-
parent && insert(this.nodes, parent, this.anchor)
76+
// set fallback for nested fragments
77+
const isFrag = isFragment(this.nodes)
78+
if (isFrag) {
79+
setFragmentFallback(this.nodes as VaporFragment, this.fallback)
80+
}
81+
82+
if (!isValidBlock(this.nodes)) {
83+
parent && remove(this.nodes, parent)
84+
const scope = this.scope || (this.scope = new EffectScope())
85+
scope.run(() => {
86+
if (isFrag) {
87+
// render fragment's fallback
88+
renderFragmentFallback(this.nodes as VaporFragment)
89+
} else {
90+
this.nodes = this.fallback!() || []
91+
}
92+
})
93+
parent && insert(this.nodes, parent, this.anchor)
94+
}
8895
}
8996

9097
setActiveSub(prevSub)
9198
}
9299
}
93100

94-
function ensureFallback(fragment: VaporFragment, fallback: BlockFn): void {
95-
if (!fragment.fallback) fragment.fallback = fallback
101+
function setFragmentFallback(
102+
fragment: VaporFragment,
103+
fallback: BlockFn | undefined,
104+
): void {
105+
if (!fragment.fallback) {
106+
fragment.fallback = fallback
107+
}
108+
if (isFragment(fragment.nodes)) {
109+
setFragmentFallback(fragment.nodes, fallback)
110+
}
111+
}
96112

97-
if (fragment instanceof DynamicFragment) {
113+
function renderFragmentFallback(fragment: VaporFragment): void {
114+
if (fragment instanceof ForFragment) {
115+
fragment.nodes[0] = [fragment.fallback!() || []] as Block[]
116+
} else if (fragment instanceof DynamicFragment) {
98117
const nodes = fragment.nodes
99118
if (isFragment(nodes)) {
100-
ensureFallback(nodes, fallback)
101-
} else if (!isValidBlock(nodes)) {
119+
renderFragmentFallback(nodes)
120+
} else {
102121
fragment.update(fragment.fallback)
103122
}
104-
} else if (fragment instanceof ForFragment) {
105-
if (!isValidBlock(fragment.nodes[0])) {
106-
fragment.nodes[0] = [fallback() || []] as Block[]
107-
}
108123
} else {
109124
// vdom slots
110125
}

packages/runtime-vapor/src/componentSlots.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,16 +129,7 @@ export function createSlot(
129129
fragment.fallback = fallback
130130
// create and cache bound version of the slot to make it stable
131131
// so that we avoid unnecessary updates if it resolves to the same slot
132-
fragment.update(
133-
slot._bound ||
134-
(slot._bound = () => {
135-
const slotContent = slot(slotProps)
136-
if (slotContent instanceof DynamicFragment) {
137-
slotContent.fallback = fallback
138-
}
139-
return slotContent
140-
}),
141-
)
132+
fragment.update(slot._bound || (slot._bound = () => slot(slotProps)))
142133
} else {
143134
fragment.update(fallback)
144135
}

0 commit comments

Comments
 (0)