Jelajahi Sumber

refactor: rename `getSlotInstance` to `getScopeOwner` and centralize scope owner retrieval

daiwei 4 bulan lalu
induk
melakukan
603dc8b102

+ 3 - 6
packages/runtime-vapor/src/component.ts

@@ -77,8 +77,8 @@ import {
   type RawSlots,
   type StaticSlots,
   type VaporSlot,
-  currentSlotOwner,
   dynamicSlotsProxyHandlers,
+  getScopeOwner,
   getSlot,
   setCurrentSlotOwner,
 } from './componentSlots'
@@ -654,9 +654,7 @@ export class VaporComponentInstance implements GenericComponentInstance {
         : rawSlots
       : EMPTY_OBJ
 
-    // Use currentSlotOwner for scopeId inheritance when inside a slot
-    // This ensures components created in slots inherit the slot owner's scopeId
-    const scopeOwner = currentSlotOwner || currentInstance
+    const scopeOwner = getScopeOwner()
     this.scopeId = scopeOwner && scopeOwner.type.__scopeId
 
     // apply custom element special handling
@@ -747,8 +745,7 @@ export function createPlainElement(
   ;(el as any).$root = isSingleRoot
 
   if (!isHydrating) {
-    // Use currentSlotOwner for scopeId when inside a slot
-    const scopeOwner = currentSlotOwner || currentInstance
+    const scopeOwner = getScopeOwner()
     const scopeId = scopeOwner!.type.__scopeId
     if (scopeId) setScopeId(el, [scopeId])
   }

+ 2 - 3
packages/runtime-vapor/src/componentSlots.ts

@@ -145,7 +145,7 @@ export function setCurrentSlotOwner(
  * Get the effective slot instance for accessing rawSlots and scopeId.
  * Prefers currentSlotOwner (if inside a slot), falls back to currentInstance.
  */
-export function getSlotInstance(): VaporComponentInstance {
+export function getScopeOwner(): VaporComponentInstance {
   return (currentSlotOwner || currentInstance) as VaporComponentInstance
 }
 
@@ -180,8 +180,7 @@ export function createSlot(
   const _isLastInsertion = isLastInsertion
   if (!isHydrating) resetInsertionState()
 
-  // Use slot owner if inside a slot (forwarded slots), otherwise use currentInstance
-  const instance = getSlotInstance()
+  const instance = getScopeOwner()
   const rawSlots = instance.rawSlots
   const slotProps = rawProps
     ? new Proxy(rawProps, rawPropsProxyHandlers)

+ 2 - 3
packages/runtime-vapor/src/vdomInterop.ts

@@ -60,7 +60,7 @@ import {
 } from '@vue/shared'
 import { type RawProps, rawPropsProxyHandlers } from './componentProps'
 import type { RawSlots, VaporSlot } from './componentSlots'
-import { currentSlotScopeIds, getSlotInstance } from './componentSlots'
+import { currentSlotScopeIds, getScopeOwner } from './componentSlots'
 import { renderEffect } from './renderEffect'
 import { _next, createTextNode } from './dom/node'
 import { optimizePropertyLookup } from './dom/prop'
@@ -350,8 +350,7 @@ function createVDOMComponent(
     frag.nodes = vnode.el as any
   }
 
-  // Use currentSlotOwner for scopeId when inside a slot
-  const scopeOwner = getSlotInstance()
+  const scopeOwner = getScopeOwner()
   vnode.scopeId = (scopeOwner && scopeOwner.type.__scopeId) || null
   vnode.slotScopeIds = currentSlotScopeIds