Skip to content

Commit 599a60c

Browse files
committed
wip: refactor async component handling
1 parent f0c9dff commit 599a60c

File tree

3 files changed

+28
-40
lines changed

3 files changed

+28
-40
lines changed

packages/runtime-vapor/__tests__/apiDefineAsyncComponent.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
defineVaporComponent,
1010
renderEffect,
1111
template,
12+
withVaporCtx,
1213
} from '@vue/runtime-vapor'
1314
import { setElementText } from '../src/dom/prop'
1415

@@ -785,12 +786,13 @@ describe('api: defineAsyncComponent', () => {
785786
const { html } = define({
786787
setup() {
787788
return createComponent(VaporKeepAlive, null, {
788-
default: () =>
789+
default: withVaporCtx(() =>
789790
createIf(
790791
() => toggle.value,
791792
() => createComponent(Foo),
792793
() => createComponent(Bar),
793794
),
795+
),
794796
})
795797
},
796798
}).render()
@@ -834,7 +836,7 @@ describe('api: defineAsyncComponent', () => {
834836
VaporKeepAlive,
835837
{ include: () => 'Foo' },
836838
{
837-
default: () => createComponent(Foo),
839+
default: withVaporCtx(() => createComponent(Foo)),
838840
},
839841
)
840842
},

packages/runtime-vapor/src/apiDefineAsyncComponent.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
createAsyncComponentContext,
66
currentInstance,
77
handleError,
8-
isKeepAlive,
98
markAsyncBoundary,
109
performAsyncHydrate,
1110
useAsyncComponentState,
@@ -29,7 +28,6 @@ import {
2928
import { invokeArrayFns } from '@vue/shared'
3029
import { type TransitionOptions, insert, remove } from './block'
3130
import { parentNode } from './dom/node'
32-
import type { KeepAliveInstance } from './components/KeepAlive'
3331
import { setTransitionHooks } from './components/Transition'
3432

3533
/*@ __NO_SIDE_EFFECTS__ */
@@ -193,14 +191,6 @@ function createInnerComp(
193191
appContext,
194192
)
195193

196-
if (parent.parent && isKeepAlive(parent.parent)) {
197-
// If there is a parent KeepAlive, let it handle the resolved async component
198-
// This will process shapeFlag and cache the component
199-
;(parent.parent as KeepAliveInstance).cacheComponent(instance)
200-
// cache the wrapper instance as well
201-
;(parent.parent as KeepAliveInstance).cacheComponent(parent)
202-
}
203-
204194
// set transition hooks
205195
if ($transition) setTransitionHooks(instance, $transition)
206196

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ export interface KeepAliveInstance extends VaporComponentInstance {
4343
anchor?: Node | null | 0,
4444
) => void
4545
deactivate: (instance: VaporComponentInstance) => void
46-
cacheComponent: (instance: VaporComponentInstance) => void
4746
getCachedComponent: (
4847
comp: VaporComponent,
4948
) => VaporComponentInstance | VaporFragment | undefined
@@ -82,14 +81,6 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
8281

8382
keepAliveInstance.getCachedComponent = comp => cache.get(comp)
8483

85-
keepAliveInstance.cacheComponent = (instance: VaporComponentInstance) => {
86-
if (!shouldCache(instance as GenericComponentInstance, props)) {
87-
return
88-
}
89-
instance.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
90-
innerCacheBlock(instance.type, instance)
91-
}
92-
9384
keepAliveInstance.activate = (instance, parentNode, anchor) => {
9485
current = instance
9586
activate(instance, parentNode, anchor)
@@ -241,28 +232,18 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
241232
return children
242233
}
243234

244-
// process shapeFlag
245-
if (isVaporComponent(children)) {
246-
children.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
247-
} else if (isInteropFragment(children)) {
248-
children.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
249-
} else if (isDynamicFragment(children)) {
250-
processFragment(children)
251-
252-
// re-process fragment when fragment updates
253-
;(children.beforeTeardown || (children.beforeTeardown = [])).push(
235+
const injectKeepAliveHooks = (frag: DynamicFragment) => {
236+
;(frag.beforeTeardown || (frag.beforeTeardown = [])).push(
254237
(oldKey, nodes, scope) => {
255-
processFragment(children)
238+
processFragment(frag)
256239
keptAliveScopes.set(oldKey, scope)
257240
return true
258241
},
259242
)
260-
;(children.beforeMount || (children.beforeMount = [])).push(() =>
261-
cacheFragment(children),
243+
;(frag.beforeMount || (frag.beforeMount = [])).push(() =>
244+
cacheFragment(frag),
262245
)
263-
264-
// get scope for fragment
265-
children.getScope = key => {
246+
frag.getScope = key => {
266247
const scope = keptAliveScopes.get(key)
267248
if (scope) {
268249
keptAliveScopes.delete(key)
@@ -271,6 +252,22 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
271252
}
272253
}
273254

255+
// process shapeFlag
256+
if (isVaporComponent(children)) {
257+
children.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
258+
if (isAsyncWrapper(children)) {
259+
injectKeepAliveHooks(children.block as DynamicFragment)
260+
}
261+
} else if (isInteropFragment(children)) {
262+
children.vnode!.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
263+
} else if (isDynamicFragment(children)) {
264+
processFragment(children)
265+
injectKeepAliveHooks(children)
266+
if (isVaporComponent(children.nodes) && isAsyncWrapper(children.nodes)) {
267+
injectKeepAliveHooks(children.nodes.block as DynamicFragment)
268+
}
269+
}
270+
274271
return children
275272
},
276273
})
@@ -287,10 +284,9 @@ const shouldCache = (
287284
: (block as GenericComponentInstance).type
288285
) as GenericComponent & AsyncComponentInternalOptions
289286

290-
// For unresolved async wrappers, skip caching
291-
// Wait for resolution and re-process in createInnerComp
287+
// always cache unresolved async components
292288
if (isAsync && !type.__asyncResolved) {
293-
return false
289+
return true
294290
}
295291

296292
const { include, exclude } = props

0 commit comments

Comments
 (0)