fix(runtime-core/scheduler): handle nested flushPostFlushCbs calls

fix #1947
This commit is contained in:
Evan You 2020-08-24 18:46:53 -04:00
parent 499bc0bfc4
commit 36fa42a88c
2 changed files with 32 additions and 2 deletions

View File

@ -4,7 +4,8 @@ import {
queuePostFlushCb, queuePostFlushCb,
invalidateJob, invalidateJob,
queuePreFlushCb, queuePreFlushCb,
flushPreFlushCbs flushPreFlushCbs,
flushPostFlushCbs
} from '../src/scheduler' } from '../src/scheduler'
describe('scheduler', () => { describe('scheduler', () => {
@ -505,4 +506,24 @@ describe('scheduler', () => {
await nextTick() await nextTick()
expect(count).toBe(1) expect(count).toBe(1)
}) })
// #1947 flushPostFlushCbs should handle nested calls
// e.g. app.mount inside app.mount
test('flushPostFlushCbs', async () => {
let count = 0
const queueAndFlush = (hook: Function) => {
queuePostFlushCb(hook)
flushPostFlushCbs()
}
queueAndFlush(() => {
queueAndFlush(() => {
count++
})
})
await nextTick()
expect(count).toBe(1)
})
}) })

View File

@ -151,8 +151,16 @@ export function flushPreFlushCbs(
export function flushPostFlushCbs(seen?: CountMap) { export function flushPostFlushCbs(seen?: CountMap) {
if (pendingPostFlushCbs.length) { if (pendingPostFlushCbs.length) {
activePostFlushCbs = [...new Set(pendingPostFlushCbs)] const deduped = [...new Set(pendingPostFlushCbs)]
pendingPostFlushCbs.length = 0 pendingPostFlushCbs.length = 0
// #1947 already has active queue, nested flushPostFlushCbs call
if (activePostFlushCbs) {
activePostFlushCbs.push(...deduped)
return
}
activePostFlushCbs = deduped
if (__DEV__) { if (__DEV__) {
seen = seen || new Map() seen = seen || new Map()
} }
@ -167,6 +175,7 @@ export function flushPostFlushCbs(seen?: CountMap) {
if (__DEV__) { if (__DEV__) {
checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex]) checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex])
} }
console.log(postFlushIndex)
activePostFlushCbs[postFlushIndex]() activePostFlushCbs[postFlushIndex]()
} }
activePostFlushCbs = null activePostFlushCbs = null