]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
wip: refactor
authordaiwei <daiwei521@126.com>
Thu, 27 Mar 2025 05:43:08 +0000 (13:43 +0800)
committerdaiwei <daiwei521@126.com>
Thu, 27 Mar 2025 05:43:08 +0000 (13:43 +0800)
packages/runtime-vapor/src/component.ts
packages/runtime-vapor/src/components/Teleport.ts

index 209bf135045c0ad8e81ce739aade74466a4975bf..0b051420a54d1b7130ddbf5689c052fb52bc2b2a 100644 (file)
@@ -60,11 +60,7 @@ import {
 import { hmrReload, hmrRerender } from './hmr'
 import { isHydrating, locateHydrationNode } from './dom/hydration'
 import { insertionAnchor, insertionParent } from './insertionState'
-import {
-  type VaporTeleportImpl,
-  instanceToTeleportMap,
-  teleportStack,
-} from './components/Teleport'
+import type { VaporTeleportImpl } from './components/Teleport'
 
 export { currentInstance } from '@vue/runtime-dom'
 
@@ -205,11 +201,6 @@ export function createComponent(
   )
 
   if (__DEV__) {
-    let teleport = teleportStack[teleportStack.length - 1]
-    if (teleport) {
-      instanceToTeleportMap.set(instance, teleport)
-    }
-
     pushWarningContext(instance)
     startMeasure(instance, `init`)
 
index 1af56e65b55ec0435807d456cfcdefb129d6d2fa..c16d0b063422135d0e444cabefa618e235103fb4 100644 (file)
@@ -10,44 +10,19 @@ import {
 } from '@vue/runtime-dom'
 import { type Block, type BlockFn, insert, remove } from '../block'
 import { createComment, createTextNode, querySelector } from '../dom/node'
-import type {
-  LooseRawProps,
-  LooseRawSlots,
-  VaporComponentInstance,
+import {
+  type LooseRawProps,
+  type LooseRawSlots,
+  type VaporComponentInstance,
+  isVaporComponent,
 } from '../component'
 import { rawPropsProxyHandlers } from '../componentProps'
 import { renderEffect } from '../renderEffect'
 import { extend, isArray } from '@vue/shared'
 import { VaporFragment } from '../fragment'
 
-export const teleportStack: TeleportFragment[] = __DEV__
-  ? ([] as TeleportFragment[])
-  : (undefined as any)
-export const instanceToTeleportMap: WeakMap<
-  VaporComponentInstance,
-  TeleportFragment
-> = __DEV__ ? new WeakMap() : (undefined as any)
-
-/**
- * dev only
- * when the root child component updates, synchronously update
- * the TeleportFragment's nodes.
- */
-export function handleTeleportRootComponentHmrReload(
-  instance: VaporComponentInstance,
-  newInstance: VaporComponentInstance,
-): void {
-  const teleport = instanceToTeleportMap.get(instance)
-  if (teleport) {
-    instanceToTeleportMap.set(newInstance, teleport)
-    if (teleport.nodes === instance) {
-      teleport.nodes = newInstance
-    } else if (isArray(teleport.nodes)) {
-      const i = teleport.nodes.indexOf(instance)
-      if (i !== -1) teleport.nodes[i] = newInstance
-    }
-  }
-}
+const instanceToTeleportMap: WeakMap<VaporComponentInstance, TeleportFragment> =
+  __DEV__ ? new WeakMap() : (undefined as any)
 
 export const VaporTeleportImpl = {
   name: 'VaporTeleport',
@@ -59,11 +34,9 @@ export const VaporTeleportImpl = {
       ? new TeleportFragment('teleport')
       : new TeleportFragment()
 
-    const updateChildrenEffect = renderEffect(() => {
-      __DEV__ && teleportStack.push(frag)
-      frag.updateChildren(slots.default && (slots.default as BlockFn)())
-      __DEV__ && teleportStack.pop()
-    })
+    const updateChildrenEffect = renderEffect(() =>
+      frag.updateChildren(slots.default && (slots.default as BlockFn)()),
+    )
 
     const updateEffect = renderEffect(() => {
       frag.update(
@@ -138,6 +111,18 @@ class TeleportFragment extends VaporFragment {
       // mount new nodes
       insert((this.nodes = children), this.currentParent, this.currentAnchor)
     }
+
+    if (__DEV__) {
+      if (isVaporComponent(children)) {
+        instanceToTeleportMap.set(children, this)
+      } else if (isArray(children)) {
+        children.forEach(node => {
+          if (isVaporComponent(node)) {
+            instanceToTeleportMap.set(node, this)
+          }
+        })
+      }
+    }
   }
 
   update(props: TeleportProps): void {
@@ -256,3 +241,24 @@ export const VaporTeleport = VaporTeleportImpl as unknown as {
     }
   }
 }
+
+/**
+ * dev only
+ * when the root child component updates, synchronously update
+ * the TeleportFragment's nodes.
+ */
+export function handleTeleportRootComponentHmrReload(
+  instance: VaporComponentInstance,
+  newInstance: VaporComponentInstance,
+): void {
+  const teleport = instanceToTeleportMap.get(instance)
+  if (teleport) {
+    instanceToTeleportMap.set(newInstance, teleport)
+    if (teleport.nodes === instance) {
+      teleport.nodes = newInstance
+    } else if (isArray(teleport.nodes)) {
+      const i = teleport.nodes.indexOf(instance)
+      if (i !== -1) teleport.nodes[i] = newInstance
+    }
+  }
+}