2022-04-30 02:40:58 +08:00
|
|
|
import { isRegExp, remove } from "shared/util";
|
|
|
|
import { getFirstComponentChild } from "core/vdom/helpers/index";
|
|
|
|
import type VNode from "core/vdom/vnode";
|
|
|
|
import type { VNodeComponentOptions } from "typescript/vnode";
|
|
|
|
import type { Component } from "typescript/component";
|
|
|
|
|
|
|
|
type CacheEntry = {
|
|
|
|
name?: string;
|
|
|
|
tag?: string;
|
|
|
|
componentInstance?: Component;
|
|
|
|
};
|
|
|
|
|
|
|
|
type CacheEntryMap = Record<string, CacheEntry | null>;
|
2017-02-17 04:57:59 +08:00
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
function getComponentName(opts?: VNodeComponentOptions): string | null {
|
2022-04-30 02:40:58 +08:00
|
|
|
return opts && (opts.Ctor.options.name || opts.tag);
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
|
|
|
|
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)) {
|
2022-04-30 02:40:58 +08:00
|
|
|
return pattern.indexOf(name) > -1;
|
|
|
|
} else if (typeof pattern === "string") {
|
|
|
|
return pattern.split(",").indexOf(name) > -1;
|
2017-04-24 23:51:30 +08:00
|
|
|
} else if (isRegExp(pattern)) {
|
2022-04-30 02:40:58 +08:00
|
|
|
return pattern.test(name);
|
2016-11-21 10:35:55 +08:00
|
|
|
}
|
2017-01-21 00:19:11 +08:00
|
|
|
/* istanbul ignore next */
|
2022-04-30 02:40:58 +08:00
|
|
|
return false;
|
2016-11-21 10:35:55 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
function pruneCache(
|
|
|
|
keepAliveInstance: { cache: CacheEntryMap; keys: string[]; _vnode: VNode },
|
|
|
|
filter: Function
|
|
|
|
) {
|
|
|
|
const { cache, keys, _vnode } = keepAliveInstance;
|
2017-01-14 07:54:35 +08:00
|
|
|
for (const key in cache) {
|
2022-04-30 02:40:58 +08:00
|
|
|
const entry = cache[key];
|
|
|
|
if (entry) {
|
|
|
|
const name = entry.name;
|
2017-01-14 07:54:35 +08:00
|
|
|
if (name && !filter(name)) {
|
2022-04-30 02:40:58 +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(
|
2022-04-30 02:40:58 +08:00
|
|
|
cache: CacheEntryMap,
|
2017-10-06 10:01:50 +08:00
|
|
|
key: string,
|
|
|
|
keys: Array<string>,
|
|
|
|
current?: VNode
|
|
|
|
) {
|
2022-04-30 02:40:58 +08:00
|
|
|
const entry = cache[key];
|
|
|
|
if (entry && (!current || entry.tag !== current.tag)) {
|
|
|
|
// @ts-expect-error can be undefined
|
|
|
|
entry.componentInstance.$destroy();
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
2022-04-30 02:40:58 +08:00
|
|
|
cache[key] = null;
|
|
|
|
remove(keys, key);
|
2017-01-14 07:54:35 +08:00
|
|
|
}
|
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
const patternTypes: Array<Function> = [String, RegExp, Array];
|
2017-10-06 10:01:50 +08:00
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
// TODO defineComponent
|
2016-06-04 22:47:14 +08:00
|
|
|
export default {
|
2022-04-30 02:40:58 +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
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
methods: {
|
|
|
|
cacheVNode() {
|
|
|
|
const { cache, keys, vnodeToCache, keyToCache } = this;
|
|
|
|
if (vnodeToCache) {
|
|
|
|
const { tag, componentInstance, componentOptions } = vnodeToCache;
|
|
|
|
cache[keyToCache] = {
|
|
|
|
name: getComponentName(componentOptions),
|
|
|
|
tag,
|
|
|
|
componentInstance,
|
|
|
|
};
|
|
|
|
keys.push(keyToCache);
|
|
|
|
// prune oldest entry
|
|
|
|
if (this.max && keys.length > parseInt(this.max)) {
|
|
|
|
pruneCacheEntry(cache, keys[0], keys, this._vnode);
|
|
|
|
}
|
|
|
|
this.vnodeToCache = null;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
created() {
|
2022-04-30 02:40:58 +08:00
|
|
|
this.cache = Object.create(null);
|
|
|
|
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) {
|
2022-04-30 02:40:58 +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() {
|
2022-04-30 02:40:58 +08:00
|
|
|
this.cacheVNode();
|
|
|
|
this.$watch("include", (val) => {
|
|
|
|
pruneCache(this, (name) => matches(val, name));
|
|
|
|
});
|
|
|
|
this.$watch("exclude", (val) => {
|
|
|
|
pruneCache(this, (name) => !matches(val, name));
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
updated() {
|
|
|
|
this.cacheVNode();
|
2017-01-14 07:54:35 +08:00
|
|
|
},
|
|
|
|
|
2021-04-03 22:06:56 +08:00
|
|
|
render() {
|
2022-04-30 02:40:58 +08:00
|
|
|
const slot = this.$slots.default;
|
|
|
|
const vnode = getFirstComponentChild(slot);
|
|
|
|
const componentOptions = vnode && vnode.componentOptions;
|
2017-01-14 07:54:35 +08:00
|
|
|
if (componentOptions) {
|
2016-11-21 10:35:55 +08:00
|
|
|
// check pattern
|
2022-04-30 02:40:58 +08:00
|
|
|
const name = getComponentName(componentOptions);
|
|
|
|
const { include, exclude } = this;
|
2017-11-20 23:04:06 +08:00
|
|
|
if (
|
|
|
|
// not included
|
|
|
|
(include && (!name || !matches(include, name))) ||
|
|
|
|
// excluded
|
|
|
|
(exclude && name && matches(exclude, name))
|
|
|
|
) {
|
2022-04-30 02:40:58 +08:00
|
|
|
return vnode;
|
2016-11-21 10:35:55 +08:00
|
|
|
}
|
2017-10-06 10:01:50 +08:00
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
const { cache, keys } = this;
|
2021-04-07 00:12:14 +08:00
|
|
|
const key =
|
2022-04-30 02:40:58 +08:00
|
|
|
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 +
|
2022-04-30 02:40:58 +08:00
|
|
|
(componentOptions.tag ? `::${componentOptions.tag}` : "")
|
|
|
|
: vnode.key;
|
2017-10-06 10:01:50 +08:00
|
|
|
if (cache[key]) {
|
2022-04-30 02:40:58 +08:00
|
|
|
vnode.componentInstance = cache[key].componentInstance;
|
2017-10-06 10:01:50 +08:00
|
|
|
// make current key freshest
|
2022-04-30 02:40:58 +08:00
|
|
|
remove(keys, key);
|
|
|
|
keys.push(key);
|
2016-07-08 22:36:53 +08:00
|
|
|
} else {
|
2022-04-30 02:40:58 +08:00
|
|
|
// delay setting the cache until update
|
|
|
|
this.vnodeToCache = vnode;
|
|
|
|
this.keyToCache = key;
|
2016-07-08 22:36:53 +08:00
|
|
|
}
|
2017-10-06 10:01:50 +08:00
|
|
|
|
2022-04-30 02:40:58 +08:00
|
|
|
// @ts-expect-error can vnode.data can be undefined
|
|
|
|
vnode.data.keepAlive = true;
|
2016-06-04 22:47:14 +08:00
|
|
|
}
|
2022-04-30 02:40:58 +08:00
|
|
|
return vnode || (slot && slot[0]);
|
2021-04-03 22:06:56 +08:00
|
|
|
},
|
2022-04-30 02:40:58 +08:00
|
|
|
};
|