@@ -12,13 +12,7 @@ import {
12
12
warn ,
13
13
watch ,
14
14
} 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'
22
16
import {
23
17
type ObjectVaporComponent ,
24
18
type VaporComponent ,
@@ -37,7 +31,9 @@ export interface KeepAliveInstance extends VaporComponentInstance {
37
31
) => void
38
32
deactivate : ( instance : VaporComponentInstance ) => void
39
33
process : ( instance : VaporComponentInstance ) => void
40
- getCache : ( comp : VaporComponent ) => VaporComponentInstance | undefined
34
+ getCachedInstance : (
35
+ comp : VaporComponent ,
36
+ ) => VaporComponentInstance | undefined
41
37
}
42
38
43
39
type CacheKey = PropertyKey | VaporComponent
@@ -80,13 +76,10 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
80
76
function cacheBlock ( ) {
81
77
const { max } = props
82
78
// 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
88
81
89
- const key = block . type
82
+ const key = innerBlock . type
90
83
if ( cache . has ( key ) ) {
91
84
// make this key the freshest
92
85
keys . delete ( key )
@@ -98,7 +91,7 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
98
91
pruneCacheEntry ( keys . values ( ) . next ( ) . value ! )
99
92
}
100
93
}
101
- cache . set ( key , ( current = block ) )
94
+ cache . set ( key , ( current = innerBlock ) )
102
95
}
103
96
104
97
onMounted ( cacheBlock )
@@ -118,25 +111,28 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
118
111
} )
119
112
} )
120
113
121
- keepAliveInstance . getCache = ( comp : VaporComponent ) => cache . get ( comp )
114
+ keepAliveInstance . getCachedInstance = ( comp : VaporComponent ) =>
115
+ cache . get ( comp )
122
116
123
- keepAliveInstance . process = ( instance : VaporComponentInstance ) => {
117
+ const process = ( keepAliveInstance . process = (
118
+ instance : VaporComponentInstance ,
119
+ ) => {
124
120
if ( cache . has ( instance . type ) ) {
125
121
instance . shapeFlag ! |= ShapeFlags . COMPONENT_KEPT_ALIVE
126
122
}
127
123
128
124
if ( shouldCache ( instance ) ) {
129
125
instance . shapeFlag ! |= ShapeFlags . COMPONENT_SHOULD_KEEP_ALIVE
130
126
}
131
- }
127
+ } )
132
128
133
129
keepAliveInstance . activate = (
134
130
instance : VaporComponentInstance ,
135
131
parentNode : ParentNode ,
136
132
anchor : Node ,
137
133
) => {
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 )
140
136
queuePostFlushCb ( ( ) => {
141
137
instance . isDeactivated = false
142
138
if ( instance . a ) invokeArrayFns ( instance . a )
@@ -167,12 +163,11 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
167
163
return children
168
164
}
169
165
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 )
176
171
177
172
function pruneCache ( filter : ( name : string ) => boolean ) {
178
173
cache . forEach ( ( instance , key ) => {
@@ -187,6 +182,7 @@ export const VaporKeepAliveImpl: ObjectVaporComponent = defineVaporComponent({
187
182
const cached = cache . get ( key )
188
183
if ( cached ) {
189
184
resetShapeFlag ( cached )
185
+ // don't unmount if the instance is the current one
190
186
if ( cached !== current ) {
191
187
unmountComponent ( cached )
192
188
}
0 commit comments