Skip to content

Commit 0d0970f

Browse files
authored
refactor(runtime-core): remove the deactivated branch in unmountComponent method (#2012)
1 parent 691a4b9 commit 0d0970f

File tree

2 files changed

+42
-32
lines changed

2 files changed

+42
-32
lines changed

packages/runtime-core/__tests__/components/KeepAlive.spec.ts

+41-23
Original file line numberDiff line numberDiff line change
@@ -347,40 +347,58 @@ describe('KeepAlive', () => {
347347
})
348348

349349
test('max', async () => {
350-
const spyA = jest.fn()
351-
const spyB = jest.fn()
352-
const spyC = jest.fn()
353-
const spyAD = jest.fn()
354-
const spyBD = jest.fn()
355-
const spyCD = jest.fn()
350+
const spyAC = jest.fn()
351+
const spyBC = jest.fn()
352+
const spyCC = jest.fn()
353+
const spyAA = jest.fn()
354+
const spyBA = jest.fn()
355+
const spyCA = jest.fn()
356+
const spyADA = jest.fn()
357+
const spyBDA = jest.fn()
358+
const spyCDA = jest.fn()
359+
const spyAUM = jest.fn()
360+
const spyBUM = jest.fn()
361+
const spyCUM = jest.fn()
356362

357363
function assertCount(calls: number[]) {
358364
expect([
359-
spyA.mock.calls.length,
360-
spyAD.mock.calls.length,
361-
spyB.mock.calls.length,
362-
spyBD.mock.calls.length,
363-
spyC.mock.calls.length,
364-
spyCD.mock.calls.length
365+
spyAC.mock.calls.length,
366+
spyAA.mock.calls.length,
367+
spyADA.mock.calls.length,
368+
spyAUM.mock.calls.length,
369+
spyBC.mock.calls.length,
370+
spyBA.mock.calls.length,
371+
spyBDA.mock.calls.length,
372+
spyBUM.mock.calls.length,
373+
spyCC.mock.calls.length,
374+
spyCA.mock.calls.length,
375+
spyCDA.mock.calls.length,
376+
spyCUM.mock.calls.length
365377
]).toEqual(calls)
366378
}
367379

368380
const viewRef = ref('a')
369381
const views: Record<string, ComponentOptions> = {
370382
a: {
371383
render: () => `one`,
372-
created: spyA,
373-
unmounted: spyAD
384+
created: spyAC,
385+
activated: spyAA,
386+
deactivated: spyADA,
387+
unmounted: spyAUM
374388
},
375389
b: {
376390
render: () => `two`,
377-
created: spyB,
378-
unmounted: spyBD
391+
created: spyBC,
392+
activated: spyBA,
393+
deactivated: spyBDA,
394+
unmounted: spyBUM
379395
},
380396
c: {
381397
render: () => `three`,
382-
created: spyC,
383-
unmounted: spyCD
398+
created: spyCC,
399+
activated: spyCA,
400+
deactivated: spyCDA,
401+
unmounted: spyCUM
384402
}
385403
}
386404

@@ -392,26 +410,26 @@ describe('KeepAlive', () => {
392410
}
393411
}
394412
render(h(App), root)
395-
assertCount([1, 0, 0, 0, 0, 0])
413+
assertCount([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
396414

397415
viewRef.value = 'b'
398416
await nextTick()
399-
assertCount([1, 0, 1, 0, 0, 0])
417+
assertCount([1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0])
400418

401419
viewRef.value = 'c'
402420
await nextTick()
403421
// should prune A because max cache reached
404-
assertCount([1, 1, 1, 0, 1, 0])
422+
assertCount([1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0])
405423

406424
viewRef.value = 'b'
407425
await nextTick()
408426
// B should be reused, and made latest
409-
assertCount([1, 1, 1, 0, 1, 0])
427+
assertCount([1, 1, 1, 1, 1, 2, 1, 0, 1, 1, 1, 0])
410428

411429
viewRef.value = 'a'
412430
await nextTick()
413431
// C should be pruned because B was used last so C is the oldest cached
414-
assertCount([2, 1, 1, 0, 1, 1])
432+
assertCount([2, 2, 1, 1, 1, 2, 2, 0, 1, 1, 1, 1])
415433
})
416434
})
417435

packages/runtime-core/src/renderer.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -2096,7 +2096,7 @@ function baseCreateRenderer(
20962096
unregisterHMR(instance)
20972097
}
20982098

2099-
const { bum, effects, update, subTree, um, da, isDeactivated } = instance
2099+
const { bum, effects, update, subTree, um } = instance
21002100
// beforeUnmount hook
21012101
if (bum) {
21022102
invokeArrayFns(bum)
@@ -2116,14 +2116,6 @@ function baseCreateRenderer(
21162116
if (um) {
21172117
queuePostRenderEffect(um, parentSuspense)
21182118
}
2119-
// deactivated hook
2120-
if (
2121-
da &&
2122-
!isDeactivated &&
2123-
instance.vnode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
2124-
) {
2125-
queuePostRenderEffect(da, parentSuspense)
2126-
}
21272119
queuePostRenderEffect(() => {
21282120
instance.isUnmounted = true
21292121
}, parentSuspense)

0 commit comments

Comments
 (0)