test: more tests for keep-alive

This commit is contained in:
Evan You
2019-10-30 23:32:29 -04:00
parent 5fcb81050a
commit 4631f5323b
3 changed files with 340 additions and 20 deletions

View File

@@ -901,7 +901,11 @@ export function createRenderer<
queuePostRenderEffect(instance.m, parentSuspense)
}
// activated hook for keep-alive roots.
if (instance.a !== null) {
if (
instance.a !== null &&
instance.vnode.shapeFlag &
ShapeFlags.STATEFUL_COMPONENT_SHOULD_KEEP_ALIVE
) {
queuePostRenderEffect(instance.a, parentSuspense)
}
mounted = true
@@ -1477,7 +1481,11 @@ export function createRenderer<
queuePostRenderEffect(um, parentSuspense)
}
// deactivated hook
if (da !== null && !isDeactivated) {
if (
da !== null &&
!isDeactivated &&
instance.vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT_SHOULD_KEEP_ALIVE
) {
queuePostRenderEffect(da, parentSuspense)
}
queuePostFlushCb(() => {

View File

@@ -22,7 +22,7 @@ import {
type MatchPattern = string | RegExp | string[] | RegExp[]
interface KeepAliveProps {
export interface KeepAliveProps {
include?: MatchPattern
exclude?: MatchPattern
max?: number | string
@@ -62,16 +62,22 @@ export const KeepAlive = {
sink.activate = (vnode, container, anchor) => {
move(vnode, container, anchor)
queuePostRenderEffect(() => {
vnode.component!.isDeactivated = false
invokeHooks(vnode.component!.a!)
const component = vnode.component!
component.isDeactivated = false
if (component.a !== null) {
invokeHooks(component.a)
}
}, parentSuspense)
}
sink.deactivate = (vnode: VNode) => {
move(vnode, storageContainer, null)
queuePostRenderEffect(() => {
invokeHooks(vnode.component!.da!)
vnode.component!.isDeactivated = true
const component = vnode.component!
if (component.da !== null) {
invokeHooks(component.da)
}
component.isDeactivated = true
}, parentSuspense)
}
@@ -94,6 +100,10 @@ export const KeepAlive = {
const cached = cache.get(key) as VNode
if (!current || cached.type !== current.type) {
unmount(cached)
} else if (current) {
// current active instance should no longer be kept-alive.
// we can't unmount it now but it might be later, so reset its flag now.
current.shapeFlag = ShapeFlags.STATEFUL_COMPONENT
}
cache.delete(key)
keys.delete(key)