Skip to content

Commit 3c3561e

Browse files
committed
fix: fix post watcher fire timing on nested app mounts
close #10005
1 parent d9162df commit 3c3561e

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

packages/runtime-core/__tests__/apiCreateApp.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import {
55
getCurrentInstance,
66
h,
77
inject,
8+
nextTick,
89
nodeOps,
10+
onMounted,
911
provide,
1012
ref,
1113
resolveComponent,
1214
resolveDirective,
1315
serializeInner,
16+
watch,
1417
withDirectives,
1518
} from '@vue/runtime-test'
1619

@@ -551,6 +554,35 @@ describe('api: createApp', () => {
551554
).not.toHaveBeenWarned()
552555
})
553556

557+
// #10005
558+
test('flush order edge case on nested createApp', async () => {
559+
const order: string[] = []
560+
const App = defineComponent({
561+
setup(props) {
562+
const message = ref('m1')
563+
watch(
564+
message,
565+
() => {
566+
order.push('post watcher')
567+
},
568+
{ flush: 'post' },
569+
)
570+
onMounted(() => {
571+
message.value = 'm2'
572+
createApp(() => '').mount(nodeOps.createElement('div'))
573+
})
574+
return () => {
575+
order.push('render')
576+
return h('div', [message.value])
577+
}
578+
},
579+
})
580+
581+
createApp(App).mount(nodeOps.createElement('div'))
582+
await nextTick()
583+
expect(order).toMatchObject(['render', 'render', 'post watcher'])
584+
})
585+
554586
// config.compilerOptions is tested in packages/vue since it is only
555587
// supported in the full build.
556588
})

packages/runtime-core/src/renderer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,7 @@ function baseCreateRenderer(
23482348
return hostNextSibling((vnode.anchor || vnode.el)!)
23492349
}
23502350

2351+
let isFlushing = false
23512352
const render: RootRenderFunction = (vnode, container, namespace) => {
23522353
if (vnode == null) {
23532354
if (container._vnode) {
@@ -2364,8 +2365,12 @@ function baseCreateRenderer(
23642365
namespace,
23652366
)
23662367
}
2367-
flushPreFlushCbs()
2368-
flushPostFlushCbs()
2368+
if (!isFlushing) {
2369+
isFlushing = true
2370+
flushPreFlushCbs()
2371+
flushPostFlushCbs()
2372+
isFlushing = false
2373+
}
23692374
container._vnode = vnode
23702375
}
23712376

0 commit comments

Comments
 (0)