feat: activated/deactivated hooks

This commit is contained in:
Evan You
2018-09-26 18:34:21 -04:00
parent 7c2ec8ace0
commit ee50fb9723
5 changed files with 80 additions and 14 deletions

View File

@@ -29,7 +29,7 @@ export class KeepAlive extends Component<{}, KeepAliveProps> {
this.keys = new Set()
}
unmounted() {
beforeUnmount() {
this.cache.forEach(vnode => {
// change flag so it can be properly unmounted
vnode.flags = VNodeFlags.COMPONENT_STATEFUL_NORMAL
@@ -88,21 +88,23 @@ export class KeepAlive extends Component<{}, KeepAliveProps> {
const { cache, keys } = this
const key = vnode.key == null ? comp : vnode.key
const cached = cache.get(key)
cache.set(key, vnode)
if (cached) {
vnode.children = cached.children
vnode.el = cached.el
// avoid vnode being mounted as fresh
vnode.flags |= VNodeFlags.COMPONENT_STATEFUL_KEPT_ALIVE
// make this key the freshest
keys.delete(key)
keys.add(key)
} else {
cache.set(key, vnode)
keys.add(key)
// prune oldest entry
if (max && keys.size > parseInt(max, 10)) {
this.pruneCacheEntry(Array.from(this.keys)[0])
}
}
// avoid vnode being unmounted
vnode.flags |= VNodeFlags.COMPONENT_STATEFUL_SHOULD_KEEP_ALIVE
return vnode
}