vue2/src/core/components/keep-alive.ts

134 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-04-03 22:06:56 +08:00
// @ts-nocheck
2016-11-21 10:35:55 +08:00
2017-10-06 10:01:50 +08:00
import { isRegExp, remove } from 'shared/util'
import { getFirstComponentChild } from 'core/vdom/helpers/index'
2021-04-03 22:06:56 +08:00
import VNode from 'core/vdom/vnode'
2021-04-04 00:29:42 +08:00
import type { VNodeComponentOptions, VNodeData } from 'typescript/vnode'
2016-06-06 13:20:27 +08:00
2021-04-03 22:06:56 +08:00
type VNodeCache = { [key: string]: VNode | null }
2017-02-17 04:57:59 +08:00
2021-04-03 22:06:56 +08:00
function getComponentName(opts?: VNodeComponentOptions): string | null {
return opts && (opts.Ctor.options.name || opts.tag)
}
2021-04-03 22:06:56 +08:00
function matches(
pattern: string | RegExp | Array<string>,
name: string
): boolean {
if (Array.isArray(pattern)) {
return pattern.indexOf(name) > -1
} else if (typeof pattern === 'string') {
2016-11-21 10:35:55 +08:00
return pattern.split(',').indexOf(name) > -1
2017-04-24 23:51:30 +08:00
} else if (isRegExp(pattern)) {
2016-11-21 10:35:55 +08:00
return pattern.test(name)
}
2017-01-21 00:19:11 +08:00
/* istanbul ignore next */
return false
2016-11-21 10:35:55 +08:00
}
2021-04-03 22:06:56 +08:00
function pruneCache(keepAliveInstance: any, filter: Function) {
2017-10-06 10:01:50 +08:00
const { cache, keys, _vnode } = keepAliveInstance
for (const key in cache) {
2021-04-03 22:06:56 +08:00
const cachedNode: VNode | null = cache[key]
if (cachedNode) {
2021-04-03 22:06:56 +08:00
const name = getComponentName(cachedNode.componentOptions)
if (name && !filter(name)) {
2017-10-06 10:01:50 +08:00
pruneCacheEntry(cache, key, keys, _vnode)
}
}
}
}
2021-04-03 22:06:56 +08:00
function pruneCacheEntry(
2017-10-06 10:01:50 +08:00
cache: VNodeCache,
key: string,
keys: Array<string>,
current?: VNode
) {
const cached = cache[key]
if (cached && (!current || cached.tag !== current.tag)) {
2017-10-06 10:01:50 +08:00
cached.componentInstance.$destroy()
}
2017-10-06 10:01:50 +08:00
cache[key] = null
remove(keys, key)
}
2017-10-06 10:01:50 +08:00
const patternTypes: Array<Function> = [String, RegExp, Array]
2021-04-03 22:06:56 +08:00
// TODO use defineComponent
2016-06-04 22:47:14 +08:00
export default {
name: 'keep-alive',
2016-07-20 01:01:19 +08:00
abstract: true,
2016-11-21 10:35:55 +08:00
props: {
include: patternTypes,
2017-10-06 10:01:50 +08:00
exclude: patternTypes,
2021-04-03 22:06:56 +08:00
max: [String, Number],
2016-11-21 10:35:55 +08:00
},
2021-04-03 22:06:56 +08:00
created() {
2016-06-04 22:47:14 +08:00
this.cache = Object.create(null)
2017-10-06 10:01:50 +08:00
this.keys = []
2016-06-04 22:47:14 +08:00
},
2021-04-03 22:06:56 +08:00
destroyed() {
for (const key in this.cache) {
2017-10-06 10:01:50 +08:00
pruneCacheEntry(this.cache, key, this.keys)
}
},
2021-04-03 22:06:56 +08:00
mounted() {
this.$watch('include', (val) => {
pruneCache(this, (name) => matches(val, name))
})
2021-04-03 22:06:56 +08:00
this.$watch('exclude', (val) => {
pruneCache(this, (name) => !matches(val, name))
})
},
2021-04-03 22:06:56 +08:00
render() {
const slot = this.$slots.default
const vnode: VNode = getFirstComponentChild(slot)
2021-04-03 22:06:56 +08:00
const componentOptions: ?VNodeComponentOptions =
vnode && vnode.componentOptions
if (componentOptions) {
2016-11-21 10:35:55 +08:00
// check pattern
2017-02-17 04:57:59 +08:00
const name: ?string = getComponentName(componentOptions)
const { include, exclude } = this
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
2016-11-21 10:35:55 +08:00
return vnode
}
2017-10-06 10:01:50 +08:00
const { cache, keys } = this
2021-04-03 22:06:56 +08:00
const key: ?string =
vnode.key == null
? // same constructor may get registered as different local components
// so cid alone is not enough (#3269)
componentOptions.Ctor.cid +
(componentOptions.tag ? `::${componentOptions.tag}` : '')
: vnode.key
2017-10-06 10:01:50 +08:00
if (cache[key]) {
vnode.componentInstance = cache[key].componentInstance
// make current key freshest
remove(keys, key)
keys.push(key)
2016-07-08 22:36:53 +08:00
} else {
2017-10-06 10:01:50 +08:00
cache[key] = vnode
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
}
2016-07-08 22:36:53 +08:00
}
2017-10-06 10:01:50 +08:00
vnode.data.keepAlive = true
2016-06-04 22:47:14 +08:00
}
return vnode || (slot && slot[0])
2021-04-03 22:06:56 +08:00
},
2016-06-04 22:47:14 +08:00
}