Skip to content

Commit

Permalink
fix(runtime-core/scheduler): handle nested flushPostFlushCbs calls
Browse files Browse the repository at this point in the history
fix #1947
  • Loading branch information
yyx990803 committed Aug 24, 2020
1 parent 499bc0b commit 36fa42a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
23 changes: 22 additions & 1 deletion packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
queuePostFlushCb,
invalidateJob,
queuePreFlushCb,
flushPreFlushCbs
flushPreFlushCbs,
flushPostFlushCbs
} from '../src/scheduler'

describe('scheduler', () => {
Expand Down Expand Up @@ -505,4 +506,24 @@ describe('scheduler', () => {
await nextTick()
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)
})
})
11 changes: 10 additions & 1 deletion packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,16 @@ export function flushPreFlushCbs(

export function flushPostFlushCbs(seen?: CountMap) {
if (pendingPostFlushCbs.length) {
activePostFlushCbs = [...new Set(pendingPostFlushCbs)]
const deduped = [...new Set(pendingPostFlushCbs)]
pendingPostFlushCbs.length = 0

// #1947 already has active queue, nested flushPostFlushCbs call
if (activePostFlushCbs) {
activePostFlushCbs.push(...deduped)
return
}

activePostFlushCbs = deduped
if (__DEV__) {
seen = seen || new Map()
}
Expand All @@ -167,6 +175,7 @@ export function flushPostFlushCbs(seen?: CountMap) {
if (__DEV__) {
checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex])
}
console.log(postFlushIndex)
activePostFlushCbs[postFlushIndex]()
}
activePostFlushCbs = null
Expand Down

0 comments on commit 36fa42a

Please sign in to comment.