Skip to content

Commit

Permalink
fix(watch): fix flush: pre watchers triggered synchronously in setup
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 15, 2022
1 parent 82bdf86 commit 74d2a76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
30 changes: 29 additions & 1 deletion packages/runtime-core/__tests__/apiWatch.spec.ts
Expand Up @@ -18,7 +18,8 @@ import {
h,
createApp,
watchPostEffect,
watchSyncEffect
watchSyncEffect,
onMounted
} from '@vue/runtime-test'
import {
ITERATE_KEY,
Expand Down Expand Up @@ -581,6 +582,33 @@ describe('api: watch', () => {
expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
})

// #5721
it('flush: pre triggered in component setup should be buffered and called before mounted', () => {
const count = ref(0)
const calls: string[] = []
const App = {
render() {},
setup() {
watch(
count,
() => {
calls.push('watch ' + count.value)
},
{ flush: 'pre' }
)
onMounted(() => {
calls.push('mounted')
})
// mutate multiple times
count.value++
count.value++
count.value++
}
}
render(h(App), nodeOps.createElement('div'))
expect(calls).toMatchObject(['watch 3', 'mounted'])
})

// #1852
it('flush: post watcher should fire after template refs updated', async () => {
const toggle = ref(false)
Expand Down
10 changes: 1 addition & 9 deletions packages/runtime-core/src/apiWatch.ts
Expand Up @@ -345,15 +345,7 @@ function doWatch(
scheduler = () => queuePostRenderEffect(job, instance && instance.suspense)
} else {
// default: 'pre'
scheduler = () => {
if (!instance || instance.isMounted) {
queuePreFlushCb(job)
} else {
// with 'pre' option, the first call must happen before
// the component is mounted so it is called synchronously.
job()
}
}
scheduler = () => queuePreFlushCb(job)
}

const effect = new ReactiveEffect(getter, scheduler)
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/scheduler.ts
Expand Up @@ -182,6 +182,8 @@ export function flushPreFlushCbs(
}

export function flushPostFlushCbs(seen?: CountMap) {
// flush any pre cbs queued during the flush (e.g. pre watchers)
flushPreFlushCbs()
if (pendingPostFlushCbs.length) {
const deduped = [...new Set(pendingPostFlushCbs)]
pendingPostFlushCbs.length = 0
Expand Down

0 comments on commit 74d2a76

Please sign in to comment.