Browse Source

fix(runtime-vapor): update setCurrentBranchKey to return previous key and handle context correctly

daiwei 2 months ago
parent
commit
c9e52bc0cc

+ 7 - 6
packages/runtime-vapor/src/components/KeepAlive.ts

@@ -43,7 +43,7 @@ export interface KeepAliveContext {
   cacheBlock(): void
   cacheBlock(): void
   cacheScope(key: any, scope: EffectScope): void
   cacheScope(key: any, scope: EffectScope): void
   getScope(key: any): EffectScope | undefined
   getScope(key: any): EffectScope | undefined
-  setCurrentBranchKey(key: any): void
+  setCurrentBranchKey(key: any): any
 }
 }
 
 
 export let currentKeepAliveCtx: KeepAliveContext | null = null
 export let currentKeepAliveCtx: KeepAliveContext | null = null
@@ -314,7 +314,11 @@ const KeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
         }
         }
       },
       },
       setCurrentBranchKey(key) {
       setCurrentBranchKey(key) {
-        currentBranchKey = key
+        try {
+          return currentBranchKey
+        } finally {
+          currentBranchKey = key
+        }
       },
       },
     }
     }
 
 
@@ -386,10 +390,7 @@ function resolveKey(
   branchKey?: any,
   branchKey?: any,
 ): CacheKey {
 ): CacheKey {
   if (key != null) {
   if (key != null) {
-    if (branchKey !== undefined && key === branchKey) {
-      return getCompositeKey(type, branchKey)
-    }
-    return key as CacheKey
+    return getCompositeKey(type, key)
   }
   }
   if (branchKey !== undefined) {
   if (branchKey !== undefined) {
     return getCompositeKey(type, branchKey)
     return getCompositeKey(type, branchKey)

+ 5 - 1
packages/runtime-vapor/src/fragment.ts

@@ -199,14 +199,18 @@ export class DynamicFragment extends VaporFragment {
       const prevOwner = setCurrentSlotOwner(this.slotOwner)
       const prevOwner = setCurrentSlotOwner(this.slotOwner)
       // set currentKeepAliveCtx so nested DynamicFragments and components can capture it
       // set currentKeepAliveCtx so nested DynamicFragments and components can capture it
       const prevCtx = setCurrentKeepAliveCtx(keepAliveCtx)
       const prevCtx = setCurrentKeepAliveCtx(keepAliveCtx)
+      let prevBranchKey: any
       if (keepAliveCtx && this.keyed) {
       if (keepAliveCtx && this.keyed) {
-        keepAliveCtx.setCurrentBranchKey(this.current)
+        prevBranchKey = keepAliveCtx.setCurrentBranchKey(this.current)
       }
       }
       // switch current instance to parent instance during update
       // switch current instance to parent instance during update
       // ensure that the parent instance is correct for nested components
       // ensure that the parent instance is correct for nested components
       const prev = parent && instance ? setCurrentInstance(instance) : undefined
       const prev = parent && instance ? setCurrentInstance(instance) : undefined
       this.nodes = this.scope.run(render) || []
       this.nodes = this.scope.run(render) || []
       if (prev !== undefined) setCurrentInstance(...prev)
       if (prev !== undefined) setCurrentInstance(...prev)
+      if (keepAliveCtx && this.keyed) {
+        keepAliveCtx.setCurrentBranchKey(prevBranchKey)
+      }
       setCurrentKeepAliveCtx(prevCtx)
       setCurrentKeepAliveCtx(prevCtx)
       setCurrentSlotOwner(prevOwner)
       setCurrentSlotOwner(prevOwner)