Skip to content

Commit 050ca87

Browse files
committed
wip: save
1 parent 02b4629 commit 050ca87

File tree

3 files changed

+28
-62
lines changed

3 files changed

+28
-62
lines changed

packages/runtime-vapor/__tests__/components/KeepAlive.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ describe('VaporKeepAlive', () => {
11161116
// toggle in, state should be maintained
11171117
toggle.value = true
11181118
await nextTick()
1119-
expect(html()).toBe('<p>1</p><!--if-->')
1119+
expect(html()).toBe('<p>1</p><!--async component--><!--if-->')
11201120

11211121
toggle.value = false
11221122
await nextTick()

packages/runtime-vapor/src/apiDefineAsyncComponent.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ function createInnerComp(
193193
// If there is a parent KeepAlive, let it handle the resolved async component
194194
// This will process shapeFlag and cache the component
195195
;(parent.parent as KeepAliveInstance).cacheComponent(instance)
196+
// cache the wrapper instance as well
197+
;(parent.parent as KeepAliveInstance).cacheComponent(parent)
196198
}
197199

198200
// set ref

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

Lines changed: 25 additions & 61 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
@@ -166,29 +168,12 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
166168
keepAliveInstance.getStorageContainer = () => storageContainer
167169

168170
keepAliveInstance.getCachedComponent = comp => {
169-
// For async components, use the resolved component type as the cache key
170-
return cache.get(comp.__asyncResolved || comp)
171-
}
172-
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-
}
171+
return cache.get(comp)
187172
}
188173

189174
keepAliveInstance.cacheComponent = (instance: VaporComponentInstance) => {
190175
if (!shouldCache(instance)) return
191-
setShapeFlags(instance)
176+
instance.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
192177
innerCacheBlock(instance.type, instance)
193178
}
194179

@@ -198,14 +183,23 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
198183

199184
const fragment = findInteropFragment(frag.nodes)
200185
if (fragment) {
201-
setVdomShapeFlags(fragment)
186+
if (cache.has(fragment.vnode!.type)) {
187+
fragment.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
188+
}
189+
if (shouldCache(innerBlock)) {
190+
fragment.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
191+
}
202192
} else {
203-
setShapeFlags(innerBlock)
193+
if (cache.has(innerBlock.type)) {
194+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
195+
}
196+
if (shouldCache(innerBlock)) {
197+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
198+
}
204199
}
205200
}
206201

207202
keepAliveInstance.cacheFragment = (fragment: DynamicFragment) => {
208-
// Find the component within the fragment
209203
const innerBlock = getInnerBlock(fragment.nodes)
210204
if (!innerBlock || !shouldCache(innerBlock)) return
211205

@@ -216,12 +210,11 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
216210
// find vdom interop fragment
217211
const frag = findInteropFragment(fragment)
218212
if (frag) {
219-
// For vdom components, set shapeFlag
220-
setVdomShapeFlags(frag)
213+
frag.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
221214
toCache = frag
222-
key = getCacheKey(frag)!
215+
key = frag.vnode!.type
223216
} else {
224-
setShapeFlags(innerBlock)
217+
innerBlock.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
225218
toCache = innerBlock
226219
key = innerBlock.type
227220
}
@@ -238,24 +231,6 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
238231
deactivate(instance, storageContainer)
239232
}
240233

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-
259234
function resetCachedShapeFlag(
260235
cached: VaporComponentInstance | VaporFragment,
261236
) {
@@ -277,9 +252,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
277252
// Process shapeFlag for vapor and vdom components
278253
// DynamicFragment (v-if, <component is/>) is processed in DynamicFragment.update
279254
if (isVaporComponent(children)) {
280-
setShapeFlags(children)
255+
children.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
281256
} else if (isInteropFragment(children)) {
282-
setVdomShapeFlags(children, true)
257+
children.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
283258
}
284259

285260
function pruneCache(filter: (name: string) => boolean) {
@@ -324,11 +299,9 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
324299
function getInnerBlock(block: Block): VaporComponentInstance | undefined {
325300
if (isVaporComponent(block)) {
326301
return block
327-
}
328-
if (isInteropFragment(block)) {
302+
} else if (isInteropFragment(block)) {
329303
return block.vnode as any
330-
}
331-
if (isFragment(block)) {
304+
} else if (isFragment(block)) {
332305
return getInnerBlock(block.nodes)
333306
}
334307
}
@@ -356,15 +329,6 @@ function getInstanceFromCache(
356329
return cached.vnode!.component as GenericComponentInstance
357330
}
358331

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-
368332
export function activate(
369333
instance: VaporComponentInstance,
370334
parentNode: ParentNode,

0 commit comments

Comments
 (0)