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

165 lines
4.2 KiB
TypeScript
Raw Normal View History

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 {
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") {
return pattern.split(",").indexOf(name) > -1;
2017-04-24 23:51:30 +08:00
} else if (isRegExp(pattern)) {
return pattern.test(name);
2016-11-21 10:35:55 +08:00
}
2017-01-21 00:19:11 +08:00
/* istanbul ignore next */
return false;
2016-11-21 10:35:55 +08:00
}
function pruneCache(
keepAliveInstance: { cache: CacheEntryMap; keys: string[]; _vnode: VNode },
filter: Function
) {
const { cache, keys, _vnode } = keepAliveInstance;
for (const key in cache) {
const entry = cache[key];
if (entry) {
const name = entry.name;
if (name && !filter(name)) {
pruneCacheEntry(cache, key, keys, _vnode);
}
}
}
}
2021-04-03 22:06:56 +08:00
function pruneCacheEntry(
cache: CacheEntryMap,
2017-10-06 10:01:50 +08:00
key: string,
keys: Array<string>,
current?: VNode
) {
const entry = cache[key];
if (entry && (!current || entry.tag !== current.tag)) {
// @ts-expect-error can be undefined
entry.componentInstance.$destroy();
}
cache[key] = null;
remove(keys, key);
}
const patternTypes: Array<Function> = [String, RegExp, Array];
2017-10-06 10:01:50 +08:00
// TODO 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
},
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() {
this.cache = Object.create(null);
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) {
pruneCacheEntry(this.cache, key, this.keys);
}
},
2021-04-03 22:06:56 +08:00
mounted() {
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();
},
2021-04-03 22:06:56 +08:00
render() {
const slot = this.$slots.default;
const vnode = getFirstComponentChild(slot);
const componentOptions = vnode && vnode.componentOptions;
if (componentOptions) {
2016-11-21 10:35:55 +08:00
// check pattern
const name = getComponentName(componentOptions);
const { include, exclude } = this;
if (
// not included
(include && (!name || !matches(include, name))) ||
// excluded
(exclude && name && matches(exclude, name))
) {
return vnode;
2016-11-21 10:35:55 +08:00
}
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}` : "")
: vnode.key;
2017-10-06 10:01:50 +08:00
if (cache[key]) {
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 {
// 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
// @ts-expect-error can vnode.data can be undefined
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
},
};