chore: tweaks

This commit is contained in:
daiwei 2025-07-25 10:16:29 +08:00
parent a65da3aee9
commit 1ab9cc10f1
2 changed files with 25 additions and 34 deletions

View File

@ -73,38 +73,38 @@ export class DynamicFragment extends VaporFragment {
} }
if (this.fallback) { if (this.fallback) {
parent && remove(this.nodes, parent) // set fallback for nested fragments
const scope = this.scope || (this.scope = new EffectScope()) const isFrag = isFragment(this.nodes)
scope.run(() => { if (isFrag) {
// handle nested fragment if (!(this.nodes as VaporFragment).fallback) {
if (isFragment(this.nodes)) { ;(this.nodes as VaporFragment).fallback = this.fallback
ensureFallback(this.nodes, this.fallback!)
} else if (!isValidBlock(this.nodes)) {
this.nodes = this.fallback!() || []
} }
}) }
parent && insert(this.nodes, parent, this.anchor) if (!isValidBlock(this.nodes)) {
parent && remove(this.nodes, parent)
const scope = this.scope || (this.scope = new EffectScope())
scope.run(() => {
if (isFrag) {
// render fragment's fallback
renderFragmentFallback(this.nodes as VaporFragment)
} else {
this.nodes = this.fallback!() || []
}
})
parent && insert(this.nodes, parent, this.anchor)
}
} }
setActiveSub(prevSub) setActiveSub(prevSub)
} }
} }
function ensureFallback(fragment: VaporFragment, fallback: BlockFn): void { function renderFragmentFallback(fragment: VaporFragment): void {
if (!fragment.fallback) fragment.fallback = fallback if (fragment instanceof ForFragment) {
fragment.nodes[0] = [fragment.fallback!() || []] as Block[]
if (fragment instanceof DynamicFragment) { } else if (fragment instanceof DynamicFragment) {
const nodes = fragment.nodes fragment.update(fragment.fallback)
if (isFragment(nodes)) {
ensureFallback(nodes, fallback)
} else if (!isValidBlock(nodes)) {
fragment.update(fragment.fallback)
}
} else if (fragment instanceof ForFragment) {
if (!isValidBlock(fragment.nodes[0])) {
fragment.nodes[0] = [fallback() || []] as Block[]
}
} else { } else {
// vdom slots // vdom slots
} }

View File

@ -129,16 +129,7 @@ export function createSlot(
fragment.fallback = fallback fragment.fallback = fallback
// create and cache bound version of the slot to make it stable // create and cache bound version of the slot to make it stable
// so that we avoid unnecessary updates if it resolves to the same slot // so that we avoid unnecessary updates if it resolves to the same slot
fragment.update( fragment.update(slot._bound || (slot._bound = () => slot(slotProps)))
slot._bound ||
(slot._bound = () => {
const slotContent = slot(slotProps)
if (slotContent instanceof DynamicFragment) {
slotContent.fallback = fallback
}
return slotContent
}),
)
} else { } else {
fragment.update(fallback) fragment.update(fallback)
} }