Skip to content

Commit c7152bf

Browse files
committed
chore: refactor
1 parent 032f46f commit c7152bf

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

packages/runtime-vapor/src/apiTemplateRef.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ export function setRef(
5050
if (!instance || instance.isUnmounted) return
5151

5252
const setupState: any = __DEV__ ? instance.setupState || {} : null
53-
const refValue = isVaporComponent(el)
54-
? getExposed(el) || el
55-
: el instanceof DynamicFragment
56-
? getExposed(el.nodes as VaporComponentInstance) || el.nodes
57-
: el
53+
const refValue = getRefValue(el)
5854
const refs =
5955
instance.refs === EMPTY_OBJ ? (instance.refs = {}) : instance.refs
6056

@@ -147,3 +143,12 @@ export function setRef(
147143
}
148144
return ref
149145
}
146+
147+
const getRefValue = (el: RefEl) => {
148+
if (isVaporComponent(el)) {
149+
return getExposed(el) || el
150+
} else if (el instanceof DynamicFragment) {
151+
return getRefValue(el.nodes as RefEl)
152+
}
153+
return el
154+
}

packages/runtime-vapor/src/component.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,12 @@ export function createComponent(
152152
locateHydrationNode()
153153
}
154154

155+
// try to get the cached instance if inside keep-alive
155156
if (currentInstance && isKeepAlive(currentInstance)) {
156-
const cache = (currentInstance as KeepAliveInstance).getCache(component)
157-
if (cache) return cache
157+
const cached = (currentInstance as KeepAliveInstance).getCachedInstance(
158+
component,
159+
)
160+
if (cached) return cached
158161
}
159162

160163
// vdom interop enabled and component is not an explicit vapor component
@@ -525,7 +528,7 @@ export function mountComponent(
525528
}
526529
if (instance.bm) invokeArrayFns(instance.bm)
527530
insert(instance.block, parentNode, anchor)
528-
if (instance.m) queuePostFlushCb(() => invokeArrayFns(instance.m!))
531+
if (instance.m) queuePostFlushCb(instance.m!)
529532
if (
530533
instance.shapeFlag! & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE &&
531534
instance.a
@@ -558,7 +561,7 @@ export function unmountComponent(
558561
instance.scope.stop()
559562

560563
if (instance.um) {
561-
queuePostFlushCb(() => invokeArrayFns(instance.um!))
564+
queuePostFlushCb(instance.um!)
562565
}
563566
instance.isUnmounted = true
564567
}

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

+22-26
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,7 @@ import {
1212
warn,
1313
watch,
1414
} from '@vue/runtime-dom'
15-
import {
16-
type Block,
17-
DynamicFragment,
18-
insert,
19-
isFragment,
20-
isValidBlock,
21-
} from '../block'
15+
import { type Block, insert, isFragment } from '../block'
2216
import {
2317
type ObjectVaporComponent,
2418
type VaporComponent,
@@ -37,7 +31,9 @@ export interface KeepAliveInstance extends VaporComponentInstance {
3731
) => void
3832
deactivate: (instance: VaporComponentInstance) => void
3933
process: (instance: VaporComponentInstance) => void
40-
getCache: (comp: VaporComponent) => VaporComponentInstance | undefined
34+
getCachedInstance: (
35+
comp: VaporComponent,
36+
) => VaporComponentInstance | undefined
4137
}
4238

4339
type CacheKey = PropertyKey | VaporComponent
@@ -80,13 +76,10 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
8076
function cacheBlock() {
8177
const { max } = props
8278
// TODO suspense
83-
const currentBlock = keepAliveInstance.block!
84-
if (!isValidBlock(currentBlock)) return
85-
86-
const block = getInnerBlock(currentBlock)!
87-
if (!block || !shouldCache(block)) return
79+
const innerBlock = getInnerBlock(keepAliveInstance.block!)!
80+
if (!innerBlock || !shouldCache(innerBlock)) return
8881

89-
const key = block.type
82+
const key = innerBlock.type
9083
if (cache.has(key)) {
9184
// make this key the freshest
9285
keys.delete(key)
@@ -98,7 +91,7 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
9891
pruneCacheEntry(keys.values().next().value!)
9992
}
10093
}
101-
cache.set(key, (current = block))
94+
cache.set(key, (current = innerBlock))
10295
}
10396

10497
onMounted(cacheBlock)
@@ -118,25 +111,28 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
118111
})
119112
})
120113

121-
keepAliveInstance.getCache = (comp: VaporComponent) => cache.get(comp)
114+
keepAliveInstance.getCachedInstance = (comp: VaporComponent) =>
115+
cache.get(comp)
122116

123-
keepAliveInstance.process = (instance: VaporComponentInstance) => {
117+
const process = (keepAliveInstance.process = (
118+
instance: VaporComponentInstance,
119+
) => {
124120
if (cache.has(instance.type)) {
125121
instance.shapeFlag! |= ShapeFlags.COMPONENT_KEPT_ALIVE
126122
}
127123

128124
if (shouldCache(instance)) {
129125
instance.shapeFlag! |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
130126
}
131-
}
127+
})
132128

133129
keepAliveInstance.activate = (
134130
instance: VaporComponentInstance,
135131
parentNode: ParentNode,
136132
anchor: Node,
137133
) => {
138-
const cachedBlock = (current = cache.get(instance.type)!)
139-
insert((instance.block = cachedBlock.block), parentNode, anchor)
134+
current = instance
135+
insert(instance.block, parentNode, anchor)
140136
queuePostFlushCb(() => {
141137
instance.isDeactivated = false
142138
if (instance.a) invokeArrayFns(instance.a)
@@ -167,12 +163,11 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
167163
return children
168164
}
169165

170-
// wrap children in dynamic fragment
171-
if (!isFragment(children)) {
172-
const frag = new DynamicFragment()
173-
frag.update(() => children)
174-
children = frag
175-
}
166+
// `children` could be either a `VaporComponent` or a `DynamicFragment`
167+
// (when using `v-if` or `<component is/>`). For `DynamicFragment` children,
168+
// the `shapeFlag` is processed in `DynamicFragment.update`. Here only need
169+
// to process the `VaporComponent` type
170+
if (isVaporComponent(children)) process(children)
176171

177172
function pruneCache(filter: (name: string) => boolean) {
178173
cache.forEach((instance, key) => {
@@ -187,6 +182,7 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
187182
const cached = cache.get(key)
188183
if (cached) {
189184
resetShapeFlag(cached)
185+
// don't unmount if the instance is the current one
190186
if (cached !== current) {
191187
unmountComponent(cached)
192188
}

0 commit comments

Comments
 (0)