Skip to content

Commit

Permalink
refactor(runtime-core): make nextTick() promise reject on scheduler f…
Browse files Browse the repository at this point in the history
…lush error
  • Loading branch information
yyx990803 committed Jul 28, 2020
1 parent 3cc768f commit 7e8b26e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,19 @@ describe('scheduler', () => {
await nextTick()
expect(calls).toEqual(['cb1', 'cb2'])
})

test('nextTick should capture scheduler flush errors', async () => {
const err = new Error('test')
queueJob(() => {
throw err
})
try {
await nextTick()
} catch (e) {
expect(e).toBe(err)
}
expect(
`Unhandled error during execution of scheduler flush`
).toHaveBeenWarned()
})
})
7 changes: 5 additions & 2 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface Job {

const queue: (Job | null)[] = []
const postFlushCbs: Function[] = []
const p = Promise.resolve()
const resolvedPromise: Promise<any> = Promise.resolve()
let currentFlushPromise: Promise<void> | null = null

let isFlushing = false
let isFlushPending = false
Expand All @@ -20,6 +21,7 @@ const RECURSION_LIMIT = 100
type CountMap = Map<Job | Function, number>

export function nextTick(fn?: () => void): Promise<void> {
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(fn) : p
}

Expand Down Expand Up @@ -57,7 +59,7 @@ export function queuePostFlushCb(cb: Function | Function[]) {
function queueFlush() {
if (!isFlushing && !isFlushPending) {
isFlushPending = true
nextTick(flushJobs)
currentFlushPromise = resolvedPromise.then(flushJobs)
}
}

Expand Down Expand Up @@ -117,6 +119,7 @@ function flushJobs(seen?: CountMap) {

flushPostFlushCbs(seen)
isFlushing = false
currentFlushPromise = null
// some postFlushCb queued jobs!
// keep flushing until it drains.
if (queue.length || postFlushCbs.length) {
Expand Down

0 comments on commit 7e8b26e

Please sign in to comment.