Skip to content

Commit

Permalink
keep-alive fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dyu committed Jun 1, 2020
1 parent ec78fc8 commit 9989018
Showing 1 changed file with 38 additions and 13 deletions.
51 changes: 38 additions & 13 deletions src/core/components/keep-alive.js
Expand Up @@ -3,7 +3,13 @@
import { isRegExp, remove } from 'shared/util'
import { getFirstComponentChild } from 'core/vdom/helpers/index'

type VNodeCache = { [key: string]: ?VNode };
type CacheEntry = {
name: ?string;
tag: ?string;
componentInstance: Component;
};

type CacheEntryMap = { [key: string]: ?CacheEntry };

function getComponentName (opts: ?VNodeComponentOptions): ?string {
return opts && (opts.Ctor.options.name || opts.tag)
Expand All @@ -24,9 +30,9 @@ function matches (pattern: string | RegExp | Array<string>, name: string): boole
function pruneCache (keepAliveInstance: any, filter: Function) {
const { cache, keys, _vnode } = keepAliveInstance
for (const key in cache) {
const cachedNode: ?VNode = cache[key]
if (cachedNode) {
const name: ?string = getComponentName(cachedNode.componentOptions)
const entry: ?CacheEntry = cache[key]
if (entry) {
const name: ?string = entry.name
if (name && !filter(name)) {
pruneCacheEntry(cache, key, keys, _vnode)
}
Expand All @@ -35,14 +41,14 @@ function pruneCache (keepAliveInstance: any, filter: Function) {
}

function pruneCacheEntry (
cache: VNodeCache,
cache: CacheEntryMap,
key: string,
keys: Array<string>,
current?: VNode
) {
const cached = cache[key]
if (cached && (!current || cached.tag !== current.tag)) {
cached.componentInstance.$destroy()
const entry: ?CacheEntry = cache[key]
if (entry && (!current || entry.tag !== current.tag)) {
entry.componentInstance.$destroy()
}
cache[key] = null
remove(keys, key)
Expand Down Expand Up @@ -72,6 +78,10 @@ export default {
},

mounted () {
if (this.putEntry) {
this.putEntry()
this.putEntry = null
}
this.$watch('include', val => {
pruneCache(this, name => matches(val, name))
})
Expand All @@ -80,6 +90,13 @@ export default {
})
},

updated () {
if (this.putEntry) {
this.putEntry()
this.putEntry = null
}
},

render () {
const slot = this.$slots.default
const vnode: VNode = getFirstComponentChild(slot)
Expand Down Expand Up @@ -109,11 +126,19 @@ export default {
remove(keys, key)
keys.push(key)
} else {
cache[key] = vnode
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
// put entry until component is instantiated
this.putEntry = () => {
const { tag, componentInstance } = vnode
cache[key] = {
name,
tag,
componentInstance
}
keys.push(key)
// prune oldest entry
if (this.max && keys.length > parseInt(this.max)) {
pruneCacheEntry(cache, keys[0], keys, this._vnode)
}
}
}

Expand Down

2 comments on commit 9989018

@lming
Copy link

@lming lming commented on 9989018 Jun 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied patch from here vuejs#9962

@superchangme
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

大哥,你这个方式是为了解决什么问题呢,我现在遇到一个棘手的问题,vue+element-ui项目,在ie11浏览器下,查询海量表格数据后,关闭页面tab页签内存不会释放

Please sign in to comment.