Skip to content

Commit 328e59b

Browse files
committed
chore: minor update
1 parent a6242ac commit 328e59b

File tree

3 files changed

+24
-31
lines changed

3 files changed

+24
-31
lines changed

packages/runtime-core/src/component.ts

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

509-
keepAliveEffect: Function[]
509+
/**
510+
* effects when the component is activated in keep-alive
511+
* @internal
512+
*/
513+
activateEffects?: Function[]
510514

511515
// lifecycle
512516
isMounted: boolean
513517
isUnmounted: boolean
514-
isActivated: boolean
518+
isDeactivating?: boolean
515519
isDeactivated: boolean
516520
/**
517521
* @internal
@@ -672,13 +676,10 @@ export function createComponentInstance(
672676
asyncDep: null,
673677
asyncResolved: false,
674678

675-
keepAliveEffect: [],
676-
677679
// lifecycle hooks
678680
// not using enums here because it results in computed properties
679681
isMounted: false,
680682
isUnmounted: false,
681-
isActivated: false,
682683
isDeactivated: false,
683684
bc: null,
684685
c: null,

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

+7-5
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
@@ -175,12 +176,13 @@ const KeepAliveImpl: ComponentOptions = {
175176

176177
sharedContext.deactivate = (vnode: VNode) => {
177178
const instance = vnode.component!
178-
instance.isActivated = true
179+
instance.isDeactivating = true
179180
invalidateMount(instance.m)
180181
invalidateMount(instance.a)
181182

182183
move(vnode, storageContainer, null, MoveType.LEAVE, parentSuspense)
183184
queuePostRenderEffect(() => {
185+
instance.isDeactivating = false
184186
if (instance.da) {
185187
invokeArrayFns(instance.da)
186188
}

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, isDeactivating } = 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 component is deactivating.
1435+
// the update will be stored and applied once it reactivates.
1436+
if (isDeactivating) {
1437+
;(instance.activateEffects || (instance.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)