From 45f02bdea216b5ee9ce0330d53bb9e71eaed918d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Jedli=C4=8Dka?= Date: Wed, 2 Nov 2022 10:43:34 +0100 Subject: [PATCH 1/2] Dont run post flush cbs in suspense --- .../__tests__/components/Suspense.spec.ts | 70 ++++++++++++++++++- .../runtime-core/src/components/Suspense.ts | 2 +- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/packages/runtime-core/__tests__/components/Suspense.spec.ts b/packages/runtime-core/__tests__/components/Suspense.spec.ts index a2c38b2846e..0256c27d592 100644 --- a/packages/runtime-core/__tests__/components/Suspense.spec.ts +++ b/packages/runtime-core/__tests__/components/Suspense.spec.ts @@ -12,7 +12,9 @@ import { watchEffect, onUnmounted, onErrorCaptured, - shallowRef + shallowRef, + inject, + provide } from '@vue/runtime-test' import { createApp } from 'vue' @@ -1233,6 +1235,72 @@ describe('Suspense', () => { expect(serializeInner(root)).toBe(`
parent
`) }) + // #2215 + test('post flush watchers in toggled components', async () => { + let cnt = 0 + + const CompA = { + template: `
A
`, + setup: async () => { + const route = inject('route') + + watch( + () => route.value, + () => cnt++, + { immediate: true, flush: 'post' } + ) + } + } + + const CompB = { + template: `
B
`, + setup: async () => { + const route = inject('route') + + watch( + () => route.value, + () => cnt++, + { immediate: true, flush: 'post' } + ) + } + } + + const route = shallowRef(CompA) + + const Parent = { + template: ` + + + + `, + setup: () => { + provide('route', route) + return { route } + } + } + + const root = nodeOps.createElement('div') + render(h(Parent), root) + + // wait for flush + await nextTick() + // wait for child async setup resolve + await nextTick() + + expect(serializeInner(root)).toBe(`
A
`) + expect(cnt).toBe(1) + + route.value = CompB + + // wait for flush + await nextTick() + // wait for child async setup resolve + await nextTick() + + expect(serializeInner(root)).toBe(`
B
`) + expect(cnt).toBe(2) + }) + test('warn if using async setup when not in a Suspense boundary', () => { const Child = { name: 'Child', diff --git a/packages/runtime-core/src/components/Suspense.ts b/packages/runtime-core/src/components/Suspense.ts index baf57088626..2118df4c560 100644 --- a/packages/runtime-core/src/components/Suspense.ts +++ b/packages/runtime-core/src/components/Suspense.ts @@ -769,7 +769,7 @@ export function queueEffectWithSuspense( } else { suspense.effects.push(fn) } - } else { + } else if (!suspense) { queuePostFlushCb(fn) } } From 77d4422c753830591fe3d750e9cb8090c39204f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Jedli=C4=8Dka?= Date: Wed, 2 Nov 2022 11:10:33 +0100 Subject: [PATCH 2/2] Update issue number --- packages/runtime-core/__tests__/components/Suspense.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/components/Suspense.spec.ts b/packages/runtime-core/__tests__/components/Suspense.spec.ts index 0256c27d592..b6f13eff80f 100644 --- a/packages/runtime-core/__tests__/components/Suspense.spec.ts +++ b/packages/runtime-core/__tests__/components/Suspense.spec.ts @@ -1235,7 +1235,7 @@ describe('Suspense', () => { expect(serializeInner(root)).toBe(`
parent
`) }) - // #2215 + // #6811 test('post flush watchers in toggled components', async () => { let cnt = 0