Skip to content

Commit 436296c

Browse files
committed
chore: minor tweaks
1 parent c45db42 commit 436296c

File tree

3 files changed

+24
-15
lines changed

3 files changed

+24
-15
lines changed

packages/runtime-core/src/component.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,11 @@ export interface ComponentInternalInstance {
506506
*/
507507
asyncResolved: boolean
508508

509-
keepAliveEffect: Function[]
509+
/**
510+
* effects to be triggered on component activated in keep-alive
511+
* @internal
512+
*/
513+
activatedEffects?: Function[]
510514

511515
// lifecycle
512516
isMounted: boolean
@@ -672,13 +676,11 @@ 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,
683+
isActivated: true,
682684
isDeactivated: false,
683685
bc: null,
684686
c: null,

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

+7-5
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ const KeepAliveImpl: ComponentOptions = {
137137
optimized,
138138
) => {
139139
const instance = vnode.component!
140-
instance.isActivated = false
140+
instance.isActivated = true
141141
move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
142142
// in case props have changed
143143
patch(
@@ -152,9 +152,11 @@ const KeepAliveImpl: ComponentOptions = {
152152
optimized,
153153
)
154154

155-
const effects = instance.keepAliveEffect
156-
queuePostFlushCb(effects)
157-
instance.keepAliveEffect.length = 0
155+
const effects = instance.activatedEffects
156+
if (effects) {
157+
queuePostFlushCb(effects)
158+
instance.activatedEffects!.length = 0
159+
}
158160

159161
queuePostRenderEffect(() => {
160162
instance.isDeactivated = false
@@ -175,7 +177,7 @@ const KeepAliveImpl: ComponentOptions = {
175177

176178
sharedContext.deactivate = (vnode: VNode) => {
177179
const instance = vnode.component!
178-
instance.isActivated = true
180+
instance.isActivated = false
179181
invalidateMount(instance.m)
180182
invalidateMount(instance.a)
181183

packages/runtime-core/src/renderer.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1431,11 +1431,16 @@ function baseCreateRenderer(
14311431
} else {
14321432
let { next, bu, u, parent, vnode } = instance
14331433

1434-
const keepAliveParent = locateDeactiveKeepAlive(instance)
1435-
if (keepAliveParent) {
1436-
keepAliveParent.keepAliveEffect.push(() => {
1434+
// skip updates while parent component is deactivated
1435+
// but store effects for next activation
1436+
const deactivatedParent = locateDeactivatedParent(instance)
1437+
if (deactivatedParent) {
1438+
;(
1439+
deactivatedParent.activatedEffects ||
1440+
(deactivatedParent.activatedEffects = [])
1441+
).push(() => {
14371442
if (!instance.isUnmounted) {
1438-
componentUpdateFn()
1443+
update()
14391444
}
14401445
})
14411446
return
@@ -2552,9 +2557,9 @@ function locateNonHydratedAsyncRoot(
25522557
}
25532558
}
25542559

2555-
function locateDeactiveKeepAlive(instance: ComponentInternalInstance | null) {
2560+
function locateDeactivatedParent(instance: ComponentInternalInstance | null) {
25562561
while (instance) {
2557-
if (instance.isActivated) {
2562+
if (!instance.isActivated) {
25582563
return instance
25592564
}
25602565
if (isKeepAlive(instance.vnode)) {

0 commit comments

Comments
 (0)