2017-10-06 10:01:50 +08:00
|
|
|
import { isRegExp, remove } from 'shared/util'
|
2016-10-13 02:07:45 +08:00
|
|
|
import { getFirstComponentChild } from 'core/vdom/helpers/index'
|
2021-04-03 22:06:56 +08:00
|
|
|
import VNode from 'core/vdom/vnode'
|
2021-04-07 00:12:14 +08:00
|
|
|
import type { VNodeComponentOptions } 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 {
|
2017-01-14 07:54:35 +08:00
|
|
|
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 {
|
2017-06-30 08:50:30 +08:00
|
|
|
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 */
|
2017-01-19 23:13:55 +08:00
|
|
|
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
|
2017-01-14 07:54:35 +08:00
|
|
|
for (const key in cache) {
|
2021-04-03 22:06:56 +08:00
|
|
|
const cachedNode: VNode | null = cache[key]
|
2017-01-14 07:54:35 +08:00
|
|
|
if (cachedNode) {
|
2021-04-03 22:06:56 +08:00
|
|
|
const name = getComponentName(cachedNode.componentOptions)
|
2017-01-14 07:54:35 +08:00
|
|
|
if (name && !filter(name)) {
|
2017-10-06 10:01:50 +08:00
|
|
|
pruneCacheEntry(cache, key, keys, _vnode)
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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]
|
2017-11-23 04:26:39 +08:00
|
|
|
if (cached && (!current || cached.tag !== current.tag)) {
|
2021-04-07 00:12:14 +08:00
|
|
|
//@ts-expect-error has void type
|
2017-10-06 10:01:50 +08:00
|
|
|
cached.componentInstance.$destroy()
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
2017-10-06 10:01:50 +08:00
|
|
|
cache[key] = null
|
|
|
|
remove(keys, key)
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2016-06-06 04:14:51 +08:00
|
|
|
name: 'keep-alive',
|
2016-07-20 01:01:19 +08:00
|
|
|
abstract: true,
|
2017-01-14 07:54:35 +08:00
|
|
|
|
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
|
|
|
},
|
2017-01-14 07:54:35 +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
|
|
|
},
|
2017-01-14 07:54:35 +08:00
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
destroyed() {
|
2017-01-14 07:54:35 +08:00
|
|
|
for (const key in this.cache) {
|
2017-10-06 10:01:50 +08:00
|
|
|
pruneCacheEntry(this.cache, key, this.keys)
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
mounted() {
|
|
|
|
this.$watch('include', (val) => {
|
|
|
|
pruneCache(this, (name) => matches(val, name))
|
2018-03-13 01:05:31 +08:00
|
|
|
})
|
2021-04-03 22:06:56 +08:00
|
|
|
this.$watch('exclude', (val) => {
|
|
|
|
pruneCache(this, (name) => !matches(val, name))
|
2018-03-13 01:05:31 +08:00
|
|
|
})
|
2017-01-14 07:54:35 +08:00
|
|
|
},
|
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
render() {
|
2017-11-17 23:23:23 +08:00
|
|
|
const slot = this.$slots.default
|
2021-04-07 00:12:14 +08:00
|
|
|
const vnode = getFirstComponentChild(slot)
|
|
|
|
const componentOptions =
|
2021-04-03 22:06:56 +08:00
|
|
|
vnode && vnode.componentOptions
|
2017-01-14 07:54:35 +08:00
|
|
|
if (componentOptions) {
|
2016-11-21 10:35:55 +08:00
|
|
|
// check pattern
|
2021-04-07 00:12:14 +08:00
|
|
|
const name = getComponentName(componentOptions)
|
2017-11-20 23:04:06 +08:00
|
|
|
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-07 00:12:14 +08:00
|
|
|
const key =
|
|
|
|
vnode!.key == null
|
2021-04-03 22:06:56 +08:00
|
|
|
? // same constructor may get registered as different local components
|
|
|
|
// so cid alone is not enough (#3269)
|
|
|
|
componentOptions.Ctor.cid +
|
|
|
|
(componentOptions.tag ? `::${componentOptions.tag}` : '')
|
2021-04-07 00:12:14 +08:00
|
|
|
: vnode!.key
|
2017-10-06 10:01:50 +08:00
|
|
|
if (cache[key]) {
|
2021-04-07 00:12:14 +08:00
|
|
|
vnode!.componentInstance = cache[key].componentInstance
|
2017-10-06 10:01:50 +08:00
|
|
|
// 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
|
|
|
|
2021-04-07 00:12:14 +08:00
|
|
|
vnode!.data!.keepAlive = true
|
2016-06-04 22:47:14 +08:00
|
|
|
}
|
2017-11-17 23:23:23 +08:00
|
|
|
return vnode || (slot && slot[0])
|
2021-04-03 22:06:56 +08:00
|
|
|
},
|
2016-06-04 22:47:14 +08:00
|
|
|
}
|