fix(watch): fix flush: pre watchers triggered synchronously in setup
fix #5721
This commit is contained in:
parent
82bdf86254
commit
74d2a76af6
@ -18,7 +18,8 @@ import {
|
|||||||
h,
|
h,
|
||||||
createApp,
|
createApp,
|
||||||
watchPostEffect,
|
watchPostEffect,
|
||||||
watchSyncEffect
|
watchSyncEffect,
|
||||||
|
onMounted
|
||||||
} from '@vue/runtime-test'
|
} from '@vue/runtime-test'
|
||||||
import {
|
import {
|
||||||
ITERATE_KEY,
|
ITERATE_KEY,
|
||||||
@ -581,6 +582,33 @@ describe('api: watch', () => {
|
|||||||
expect(calls).toEqual(['render', 'watcher 1', 'watcher 2', 'render'])
|
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
|
// #1852
|
||||||
it('flush: post watcher should fire after template refs updated', async () => {
|
it('flush: post watcher should fire after template refs updated', async () => {
|
||||||
const toggle = ref(false)
|
const toggle = ref(false)
|
||||||
|
@ -345,15 +345,7 @@ function doWatch(
|
|||||||
scheduler = () => queuePostRenderEffect(job, instance && instance.suspense)
|
scheduler = () => queuePostRenderEffect(job, instance && instance.suspense)
|
||||||
} else {
|
} else {
|
||||||
// default: 'pre'
|
// default: 'pre'
|
||||||
scheduler = () => {
|
scheduler = () => queuePreFlushCb(job)
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const effect = new ReactiveEffect(getter, scheduler)
|
const effect = new ReactiveEffect(getter, scheduler)
|
||||||
|
@ -182,6 +182,8 @@ export function flushPreFlushCbs(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function flushPostFlushCbs(seen?: CountMap) {
|
export function flushPostFlushCbs(seen?: CountMap) {
|
||||||
|
// flush any pre cbs queued during the flush (e.g. pre watchers)
|
||||||
|
flushPreFlushCbs()
|
||||||
if (pendingPostFlushCbs.length) {
|
if (pendingPostFlushCbs.length) {
|
||||||
const deduped = [...new Set(pendingPostFlushCbs)]
|
const deduped = [...new Set(pendingPostFlushCbs)]
|
||||||
pendingPostFlushCbs.length = 0
|
pendingPostFlushCbs.length = 0
|
||||||
|
Loading…
Reference in New Issue
Block a user