Skip to content

Commit a1306f9

Browse files
committed
chore: minor update
1 parent a6242ac commit a1306f9

File tree

3 files changed

+35
-39
lines changed

3 files changed

+35
-39
lines changed

packages/runtime-core/src/component.ts

+10-5
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,20 @@ export interface ComponentInternalInstance {
506506
*/
507507
asyncResolved: boolean
508508

509-
keepAliveEffect: Function[]
509+
/**
510+
* keep-alive instance
511+
* @internal
512+
*/
513+
keepAlive?: ComponentInternalInstance
514+
/**
515+
* effects when the component is activated in keep-alive
516+
* @internal
517+
*/
518+
activateEffects?: Function[]
510519

511520
// lifecycle
512521
isMounted: boolean
513522
isUnmounted: boolean
514-
isActivated: boolean
515523
isDeactivated: boolean
516524
/**
517525
* @internal
@@ -672,13 +680,10 @@ export function createComponentInstance(
672680
asyncDep: null,
673681
asyncResolved: false,
674682

675-
keepAliveEffect: [],
676-
677683
// lifecycle hooks
678684
// not using enums here because it results in computed properties
679685
isMounted: false,
680686
isUnmounted: false,
681-
isActivated: false,
682687
isDeactivated: false,
683688
bc: null,
684689
c: null,

packages/runtime-core/src/components/KeepAlive.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ const KeepAliveImpl: ComponentOptions = {
137137
optimized,
138138
) => {
139139
const instance = vnode.component!
140-
instance.isActivated = false
141140
move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
142141
// in case props have changed
143142
patch(
@@ -152,9 +151,11 @@ const KeepAliveImpl: ComponentOptions = {
152151
optimized,
153152
)
154153

155-
const effects = instance.keepAliveEffect
156-
queuePostFlushCb(effects)
157-
instance.keepAliveEffect.length = 0
154+
const effects = instance.activateEffects
155+
if (effects) {
156+
queuePostFlushCb(effects)
157+
instance.activateEffects!.length = 0
158+
}
158159

159160
queuePostRenderEffect(() => {
160161
instance.isDeactivated = false
@@ -174,26 +175,26 @@ const KeepAliveImpl: ComponentOptions = {
174175
}
175176

176177
sharedContext.deactivate = (vnode: VNode) => {
177-
const instance = vnode.component!
178-
instance.isActivated = true
179-
invalidateMount(instance.m)
180-
invalidateMount(instance.a)
178+
const i = vnode.component!
179+
i.keepAlive = instance
180+
invalidateMount(i.m)
181+
invalidateMount(i.a)
181182

182183
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
183184
queuePostRenderEffect(() => {
184-
if (instance.da) {
185-
invokeArrayFns(instance.da)
185+
if (i.da) {
186+
invokeArrayFns(i.da)
186187
}
187188
const vnodeHook = vnode.props && vnode.props.onVnodeUnmounted
188189
if (vnodeHook) {
189-
invokeVNodeHook(vnodeHook, instance.parent, vnode)
190+
invokeVNodeHook(vnodeHook, i.parent, vnode)
190191
}
191-
instance.isDeactivated = true
192+
i.isDeactivated = true
192193
}, parentSuspense)
193194

194195
if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) {
195196
// Update components tree
196-
devtoolsComponentAdded(instance)
197+
devtoolsComponentAdded(i)
197198
}
198199
}
199200

packages/runtime-core/src/renderer.ts

+11-21
Original file line numberDiff line numberDiff line change
@@ -1429,15 +1429,18 @@ function baseCreateRenderer(
14291429
// #2458: deference mount-only object parameters to prevent memleaks
14301430
initialVNode = container = anchor = null as any
14311431
} else {
1432-
let { next, bu, u, parent, vnode } = instance
1432+
let { next, bu, u, parent, vnode, keepAlive } = instance
14331433

1434-
const keepAliveParent = locateDeactiveKeepAlive(instance)
1435-
if (keepAliveParent) {
1436-
keepAliveParent.keepAliveEffect.push(() => {
1437-
if (!instance.isUnmounted) {
1438-
componentUpdateFn()
1439-
}
1440-
})
1434+
// skip unnecessary update while parent KeepAlive is deactivated.
1435+
// the update will be stored and applied once KeepAlive reactivates.
1436+
if (keepAlive) {
1437+
;(keepAlive.activateEffects || (keepAlive.activateEffects = [])).push(
1438+
() => {
1439+
if (!instance.isUnmounted) {
1440+
componentUpdateFn()
1441+
}
1442+
},
1443+
)
14411444
return
14421445
}
14431446

@@ -2552,19 +2555,6 @@ function locateNonHydratedAsyncRoot(
25522555
}
25532556
}
25542557

2555-
function locateDeactiveKeepAlive(instance: ComponentInternalInstance | null) {
2556-
while (instance) {
2557-
if (instance.isActivated) {
2558-
return instance
2559-
}
2560-
if (isKeepAlive(instance.vnode)) {
2561-
break
2562-
}
2563-
instance = instance.parent
2564-
}
2565-
return null
2566-
}
2567-
25682558
export function invalidateMount(hooks: LifecycleHook): void {
25692559
if (hooks) {
25702560
for (let i = 0; i < hooks.length; i++)

0 commit comments

Comments
 (0)