1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
| const cache: Cache = new Map() const keys: Keys = new Set()
const KeepAliveImpl: ComponentOptions = { name: `KeepAlive`,
__isKeepAlive: true,
props: { include: [String, RegExp, Array], exclude: [String, RegExp, Array], max: [String, Number] },
setup(props: KeepAliveProps, { slots }: SetupContext) { const instance = getCurrentInstance()! const sharedContext = instance.ctx as KeepAliveContext
if (__SSR__ && !sharedContext.renderer) { return () => { const children = slots.default && slots.default() return children && children.length === 1 ? children[0] : children } }
const cache: Cache = new Map() const keys: Keys = new Set() let current: VNode | null = null
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { ;(instance as any).__v_cache = cache }
const parentSuspense = instance.suspense
const { renderer: { p: patch, m: move, um: _unmount, o: { createElement } } } = sharedContext const storageContainer = createElement('div')
sharedContext.activate = (vnode, container, anchor, isSVG, optimized) => { const instance = vnode.component! move(vnode, container, anchor, MoveType.ENTER, parentSuspense) patch( instance.vnode, vnode, container, anchor, instance, parentSuspense, isSVG, vnode.slotScopeIds, optimized ) queuePostRenderEffect(() => { instance.isDeactivated = false if (instance.a) { invokeArrayFns(instance.a) } const vnodeHook = vnode.props && vnode.props.onVnodeMounted if (vnodeHook) { invokeVNodeHook(vnodeHook, instance.parent, vnode) } }, parentSuspense)
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { devtoolsComponentAdded(instance) } }
sharedContext.deactivate = (vnode: VNode) => { const instance = vnode.component! move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense) queuePostRenderEffect(() => { if (instance.da) { invokeArrayFns(instance.da) } const vnodeHook = vnode.props && vnode.props.onVnodeUnmounted if (vnodeHook) { invokeVNodeHook(vnodeHook, instance.parent, vnode) } instance.isDeactivated = true }, parentSuspense)
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { devtoolsComponentAdded(instance) } }
function unmount(vnode: VNode) { resetShapeFlag(vnode) _unmount(vnode, instance, parentSuspense, true) }
function pruneCache(filter?: (name: string) => boolean) { cache.forEach((vnode, key) => { const name = getComponentName(vnode.type as ConcreteComponent) if (name && (!filter || !filter(name))) { pruneCacheEntry(key) } }) }
function pruneCacheEntry(key: CacheKey) { const cached = cache.get(key) as VNode if (!current || !isSameVNodeType(cached, current)) { unmount(cached) } else if (current) { resetShapeFlag(current) } cache.delete(key) keys.delete(key) }
watch( () => [props.include, props.exclude], ([include, exclude]) => { include && pruneCache(name => matches(include, name)) exclude && pruneCache(name => !matches(exclude, name)) }, { flush: 'post', deep: true } )
let pendingCacheKey: CacheKey | null = null const cacheSubtree = () => { if (pendingCacheKey != null) { cache.set(pendingCacheKey, getInnerChild(instance.subTree)) } } onMounted(cacheSubtree) onUpdated(cacheSubtree)
onBeforeUnmount(() => { cache.forEach(cached => { const { subTree, suspense } = instance const vnode = getInnerChild(subTree) if (cached.type === vnode.type && cached.key === vnode.key) { resetShapeFlag(vnode) const da = vnode.component!.da da && queuePostRenderEffect(da, suspense) return } unmount(cached) }) })
return () => { pendingCacheKey = null
if (!slots.default) { return null }
const children = slots.default() const rawVNode = children[0] if (children.length > 1) { if (__DEV__) { warn(`KeepAlive should contain exactly one component child.`) } current = null return children } else if ( !isVNode(rawVNode) || (!(rawVNode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) && !(rawVNode.shapeFlag & ShapeFlags.SUSPENSE)) ) { current = null return rawVNode }
let vnode = getInnerChild(rawVNode) const comp = vnode.type as ConcreteComponent
const name = getComponentName( isAsyncWrapper(vnode) ? (vnode.type as ComponentOptions).__asyncResolved || {} : comp )
const { include, exclude, max } = props
if ( (include && (!name || !matches(include, name))) || (exclude && name && matches(exclude, name)) ) { current = vnode return rawVNode }
const key = vnode.key == null ? comp : vnode.key const cachedVNode = cache.get(key)
if (vnode.el) { vnode = cloneVNode(vnode) if (rawVNode.shapeFlag & ShapeFlags.SUSPENSE) { rawVNode.ssContent = vnode } } pendingCacheKey = key
if (cachedVNode) { vnode.el = cachedVNode.el vnode.component = cachedVNode.component if (vnode.transition) { setTransitionHooks(vnode, vnode.transition!) } vnode.shapeFlag |= ShapeFlags.COMPONENT_KEPT_ALIVE keys.delete(key) keys.add(key) } else { keys.add(key) if (max && keys.size > parseInt(max as string, 10)) { pruneCacheEntry(keys.values().next().value) } } vnode.shapeFlag |= ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
current = vnode return isSuspense(rawVNode.type) ? rawVNode : vnode } } }
|