Skip to content

Commit da393d9

Browse files
committed
wip: save
1 parent 02b4629 commit da393d9

File tree

1 file changed

+22
-59
lines changed

1 file changed

+22
-59
lines changed

packages/runtime-vapor/src/components/KeepAlive.ts

Lines changed: 22 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
128128
if (isFragment(block) && (frag = findInteropFragment(block))) {
129129
// vdom component: cache the fragment
130130
toCache = frag
131-
key = getCacheKey(frag)
131+
key = frag.vnode!.type
132132
} else {
133133
// vapor component: cache the instance
134134
toCache = innerBlock
@@ -150,7 +150,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
150150

151151
// current instance will be unmounted as part of keep-alive's unmount
152152
if (current) {
153-
const currentKey = getCacheKey(current)
153+
const currentKey = isVaporComponent(current)
154+
? current.type
155+
: current.vnode!.type
154156
if (currentKey === key) {
155157
// call deactivated hook
156158
const da = instance.da
@@ -170,25 +172,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
170172
return cache.get(comp.__asyncResolved || comp)
171173
}
172174

173-
const setShapeFlags = (instance: VaporComponentInstance) => {
174-
// For unresolved async wrappers, skip processing
175-
// Wait for resolution and re-process via createInnerComp
176-
if (isAsyncWrapper(instance) && !instance.type.__asyncResolved) {
177-
return
178-
}
179-
180-
if (cache.has(instance.type)) {
181-
instance.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
182-
}
183-
184-
if (shouldCache(instance)) {
185-
instance.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
186-
}
187-
}
188-
189175
keepAliveInstance.cacheComponent = (instance: VaporComponentInstance) => {
190176
if (!shouldCache(instance)) return
191-
setShapeFlags(instance)
177+
instance.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
192178
innerCacheBlock(instance.type, instance)
193179
}
194180

@@ -198,14 +184,21 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
198184

199185
const fragment = findInteropFragment(frag.nodes)
200186
if (fragment) {
201-
setVdomShapeFlags(fragment)
187+
if (cache.has(fragment.vnode!.type)) {
188+
fragment.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
189+
}
202190
} else {
203-
setShapeFlags(innerBlock)
191+
if (cache.has(innerBlock.type)) {
192+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
193+
}
194+
195+
if (shouldCache(innerBlock)) {
196+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
197+
}
204198
}
205199
}
206200

207201
keepAliveInstance.cacheFragment = (fragment: DynamicFragment) => {
208-
// Find the component within the fragment
209202
const innerBlock = getInnerBlock(fragment.nodes)
210203
if (!innerBlock || !shouldCache(innerBlock)) return
211204

@@ -216,12 +209,11 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
216209
// find vdom interop fragment
217210
const frag = findInteropFragment(fragment)
218211
if (frag) {
219-
// For vdom components, set shapeFlag
220-
setVdomShapeFlags(frag)
212+
frag.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
221213
toCache = frag
222-
key = getCacheKey(frag)!
214+
key = frag.vnode!.type
223215
} else {
224-
setShapeFlags(innerBlock)
216+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
225217
toCache = innerBlock
226218
key = innerBlock.type
227219
}
@@ -238,24 +230,6 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
238230
deactivate(instance, storageContainer)
239231
}
240232

241-
function setVdomShapeFlags(
242-
fragment: VaporFragment,
243-
shouldKeepAlive: boolean = true,
244-
) {
245-
if (shouldKeepAlive) {
246-
fragment.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
247-
}
248-
const fragKey = getCacheKey(fragment)
249-
if (fragKey && cache.has(fragKey)) {
250-
fragment.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
251-
}
252-
// Also set shapeFlag on the component instance if it exists
253-
const vnode = fragment.vnode as any
254-
if (vnode && vnode.component) {
255-
vnode.component.shapeFlag = fragment.vnode!.shapeFlag
256-
}
257-
}
258-
259233
function resetCachedShapeFlag(
260234
cached: VaporComponentInstance | VaporFragment,
261235
) {
@@ -277,9 +251,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
277251
// Process shapeFlag for vapor and vdom components
278252
// DynamicFragment (v-if, <component is/>) is processed in DynamicFragment.update
279253
if (isVaporComponent(children)) {
280-
setShapeFlags(children)
254+
children.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
281255
} else if (isInteropFragment(children)) {
282-
setVdomShapeFlags(children, true)
256+
children.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
283257
}
284258

285259
function pruneCache(filter: (name: string) => boolean) {
@@ -324,11 +298,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
324298
function getInnerBlock(block: Block): VaporComponentInstance | undefined {
325299
if (isVaporComponent(block)) {
326300
return block
327-
}
328-
if (isInteropFragment(block)) {
301+
} else if (isInteropFragment(block)) {
329302
return block.vnode as any
330-
}
331-
if (isFragment(block)) {
303+
} else if (isFragment(block)) {
332304
return getInnerBlock(block.nodes)
333305
}
334306
}
@@ -356,15 +328,6 @@ function getInstanceFromCache(
356328
return cached.vnode!.component as GenericComponentInstance
357329
}
358330

359-
function getCacheKey(block: VaporComponentInstance | VaporFragment): CacheKey {
360-
if (isVaporComponent(block)) {
361-
return block.type
362-
}
363-
364-
// vdom interop
365-
return block.vnode!.type
366-
}
367-
368331
export function activate(
369332
instance: VaporComponentInstance,
370333
parentNode: ParentNode,

0 commit comments

Comments
 (0)