refactor(runtime-core): make nextTick() promise reject on scheduler flush error

This commit is contained in:
Evan You 2020-07-28 00:20:30 -04:00
parent 3cc768f9f2
commit 7e8b26eba8
2 changed files with 20 additions and 2 deletions

View File

@ -294,4 +294,19 @@ describe('scheduler', () => {
await nextTick() await nextTick()
expect(calls).toEqual(['cb1', 'cb2']) 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()
})
}) })

View File

@ -8,7 +8,8 @@ export interface Job {
const queue: (Job | null)[] = [] const queue: (Job | null)[] = []
const postFlushCbs: Function[] = [] const postFlushCbs: Function[] = []
const p = Promise.resolve() const resolvedPromise: Promise<any> = Promise.resolve()
let currentFlushPromise: Promise<void> | null = null
let isFlushing = false let isFlushing = false
let isFlushPending = false let isFlushPending = false
@ -20,6 +21,7 @@ const RECURSION_LIMIT = 100
type CountMap = Map<Job | Function, number> type CountMap = Map<Job | Function, number>
export function nextTick(fn?: () => void): Promise<void> { export function nextTick(fn?: () => void): Promise<void> {
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(fn) : p return fn ? p.then(fn) : p
} }
@ -57,7 +59,7 @@ export function queuePostFlushCb(cb: Function | Function[]) {
function queueFlush() { function queueFlush() {
if (!isFlushing && !isFlushPending) { if (!isFlushing && !isFlushPending) {
isFlushPending = true isFlushPending = true
nextTick(flushJobs) currentFlushPromise = resolvedPromise.then(flushJobs)
} }
} }
@ -117,6 +119,7 @@ function flushJobs(seen?: CountMap) {
flushPostFlushCbs(seen) flushPostFlushCbs(seen)
isFlushing = false isFlushing = false
currentFlushPromise = null
// some postFlushCb queued jobs! // some postFlushCb queued jobs!
// keep flushing until it drains. // keep flushing until it drains.
if (queue.length || postFlushCbs.length) { if (queue.length || postFlushCbs.length) {