|
|
@@ -5,6 +5,10 @@ import { getFirstComponentChild } from 'core/vdom/helpers/index'
|
|
|
|
|
|
const patternTypes = [String, RegExp]
|
|
|
|
|
|
+function getComponentName (opts: ?VNodeComponentOptions): ?string {
|
|
|
+ return opts && (opts.Ctor.options.name || opts.tag)
|
|
|
+}
|
|
|
+
|
|
|
function matches (pattern: string | RegExp, name: string): boolean {
|
|
|
if (typeof pattern === 'string') {
|
|
|
return pattern.split(',').indexOf(name) > -1
|
|
|
@@ -13,22 +17,62 @@ function matches (pattern: string | RegExp, name: string): boolean {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function pruneCache (cache, filter) {
|
|
|
+ for (const key in cache) {
|
|
|
+ const cachedNode = cache[key]
|
|
|
+ if (cachedNode) {
|
|
|
+ const name = getComponentName(cachedNode.componentOptions)
|
|
|
+ if (name && !filter(name)) {
|
|
|
+ pruneCacheEntry(cachedNode)
|
|
|
+ cache[key] = null
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function pruneCacheEntry (vnode: ?MountedComponentVNode) {
|
|
|
+ if (vnode) {
|
|
|
+ if (!vnode.componentInstance._inactive) {
|
|
|
+ callHook(vnode.componentInstance, 'deactivated')
|
|
|
+ }
|
|
|
+ vnode.componentInstance.$destroy()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export default {
|
|
|
name: 'keep-alive',
|
|
|
abstract: true,
|
|
|
+
|
|
|
props: {
|
|
|
include: patternTypes,
|
|
|
exclude: patternTypes
|
|
|
},
|
|
|
+
|
|
|
created () {
|
|
|
this.cache = Object.create(null)
|
|
|
},
|
|
|
+
|
|
|
+ destroyed () {
|
|
|
+ for (const key in this.cache) {
|
|
|
+ pruneCacheEntry(this.cache[key])
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
+ watch: {
|
|
|
+ include (val: string | RegExp) {
|
|
|
+ pruneCache(this.cache, name => matches(val, name))
|
|
|
+ },
|
|
|
+ exclude (val: string | RegExp) {
|
|
|
+ pruneCache(this.cache, name => !matches(val, name))
|
|
|
+ }
|
|
|
+ },
|
|
|
+
|
|
|
render () {
|
|
|
const vnode: VNode = getFirstComponentChild(this.$slots.default)
|
|
|
- if (vnode && vnode.componentOptions) {
|
|
|
- const opts: VNodeComponentOptions = vnode.componentOptions
|
|
|
+ const componentOptions = vnode && vnode.componentOptions
|
|
|
+ if (componentOptions) {
|
|
|
// check pattern
|
|
|
- const name = opts.Ctor.options.name || opts.tag
|
|
|
+ const name = getComponentName(componentOptions)
|
|
|
if (name && (
|
|
|
(this.include && !matches(this.include, name)) ||
|
|
|
(this.exclude && matches(this.exclude, name))
|
|
|
@@ -38,7 +82,7 @@ export default {
|
|
|
const key = vnode.key == null
|
|
|
// same constructor may get registered as different local components
|
|
|
// so cid alone is not enough (#3269)
|
|
|
- ? opts.Ctor.cid + (opts.tag ? `::${opts.tag}` : '')
|
|
|
+ ? componentOptions.Ctor.cid + (componentOptions.tag ? `::${componentOptions.tag}` : '')
|
|
|
: vnode.key
|
|
|
if (this.cache[key]) {
|
|
|
vnode.componentInstance = this.cache[key].componentInstance
|
|
|
@@ -48,12 +92,5 @@ export default {
|
|
|
vnode.data.keepAlive = true
|
|
|
}
|
|
|
return vnode
|
|
|
- },
|
|
|
- destroyed () {
|
|
|
- for (const key in this.cache) {
|
|
|
- const vnode = this.cache[key]
|
|
|
- callHook(vnode.componentInstance, 'deactivated')
|
|
|
- vnode.componentInstance.$destroy()
|
|
|
- }
|
|
|
}
|
|
|
}
|