From: daiwei Date: Fri, 25 Jul 2025 02:16:29 +0000 (+0800) Subject: chore: tweaks X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=b8ceb89a4634b46e619354610dc6df395dff0259;p=thirdparty%2Fvuejs%2Fcore.git chore: tweaks --- diff --git a/packages/runtime-vapor/src/apiCreateFor.ts b/packages/runtime-vapor/src/apiCreateFor.ts index ac2a94f092..0f032e685e 100644 --- a/packages/runtime-vapor/src/apiCreateFor.ts +++ b/packages/runtime-vapor/src/apiCreateFor.ts @@ -152,7 +152,6 @@ export const createFor = ( // render fallback nodes if (frag.fallback) { insert((frag.nodes[0] = frag.fallback()), parent!, parentAnchor) - oldBlocks = [] isFallback = true } } else if (!getKey) { @@ -341,9 +340,9 @@ export const createFor = ( if (!isFallback) { frag.nodes = [(oldBlocks = newBlocks)] - if (parentAnchor) { - frag.nodes.push(parentAnchor) - } + if (parentAnchor) frag.nodes.push(parentAnchor) + } else { + oldBlocks = [] } setActiveSub(prevSub) } diff --git a/packages/runtime-vapor/src/block.ts b/packages/runtime-vapor/src/block.ts index 0271c1c008..7c178e894b 100644 --- a/packages/runtime-vapor/src/block.ts +++ b/packages/runtime-vapor/src/block.ts @@ -73,38 +73,53 @@ export class DynamicFragment extends VaporFragment { } if (this.fallback) { - parent && remove(this.nodes, parent) - const scope = this.scope || (this.scope = new EffectScope()) - scope.run(() => { - // handle nested fragment - if (isFragment(this.nodes)) { - ensureFallback(this.nodes, this.fallback!) - } else if (!isValidBlock(this.nodes)) { - this.nodes = this.fallback!() || [] - } - }) - - parent && insert(this.nodes, parent, this.anchor) + // set fallback for nested fragments + const isFrag = isFragment(this.nodes) + if (isFrag) { + setFragmentFallback(this.nodes as VaporFragment, this.fallback) + } + + 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) } } -function ensureFallback(fragment: VaporFragment, fallback: BlockFn): void { - if (!fragment.fallback) fragment.fallback = fallback +function setFragmentFallback( + fragment: VaporFragment, + fallback: BlockFn | undefined, +): void { + if (!fragment.fallback) { + fragment.fallback = fallback + } + if (isFragment(fragment.nodes)) { + setFragmentFallback(fragment.nodes, fallback) + } +} - if (fragment instanceof DynamicFragment) { +function renderFragmentFallback(fragment: VaporFragment): void { + if (fragment instanceof ForFragment) { + fragment.nodes[0] = [fragment.fallback!() || []] as Block[] + } else if (fragment instanceof DynamicFragment) { const nodes = fragment.nodes if (isFragment(nodes)) { - ensureFallback(nodes, fallback) - } else if (!isValidBlock(nodes)) { + renderFragmentFallback(nodes) + } else { fragment.update(fragment.fallback) } - } else if (fragment instanceof ForFragment) { - if (!isValidBlock(fragment.nodes[0])) { - fragment.nodes[0] = [fallback() || []] as Block[] - } } else { // vdom slots } diff --git a/packages/runtime-vapor/src/componentSlots.ts b/packages/runtime-vapor/src/componentSlots.ts index 4215548f2e..036cf670e4 100644 --- a/packages/runtime-vapor/src/componentSlots.ts +++ b/packages/runtime-vapor/src/componentSlots.ts @@ -129,16 +129,7 @@ export function createSlot( fragment.fallback = fallback // 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 - fragment.update( - slot._bound || - (slot._bound = () => { - const slotContent = slot(slotProps) - if (slotContent instanceof DynamicFragment) { - slotContent.fallback = fallback - } - return slotContent - }), - ) + fragment.update(slot._bound || (slot._bound = () => slot(slotProps))) } else { fragment.update(fallback) }