Skip to content

Commit 7e8b26e

Browse files
committed
refactor(runtime-core): make nextTick() promise reject on scheduler flush error
1 parent 3cc768f commit 7e8b26e

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/runtime-core/__tests__/scheduler.spec.ts

+15
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,19 @@ describe('scheduler', () => {
294294
await nextTick()
295295
expect(calls).toEqual(['cb1', 'cb2'])
296296
})
297+
298+
test('nextTick should capture scheduler flush errors', async () => {
299+
const err = new Error('test')
300+
queueJob(() => {
301+
throw err
302+
})
303+
try {
304+
await nextTick()
305+
} catch (e) {
306+
expect(e).toBe(err)
307+
}
308+
expect(
309+
`Unhandled error during execution of scheduler flush`
310+
).toHaveBeenWarned()
311+
})
297312
})

packages/runtime-core/src/scheduler.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export interface Job {
88

99
const queue: (Job | null)[] = []
1010
const postFlushCbs: Function[] = []
11-
const p = Promise.resolve()
11+
const resolvedPromise: Promise<any> = Promise.resolve()
12+
let currentFlushPromise: Promise<void> | null = null
1213

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

2223
export function nextTick(fn?: () => void): Promise<void> {
24+
const p = currentFlushPromise || resolvedPromise
2325
return fn ? p.then(fn) : p
2426
}
2527

@@ -57,7 +59,7 @@ export function queuePostFlushCb(cb: Function | Function[]) {
5759
function queueFlush() {
5860
if (!isFlushing && !isFlushPending) {
5961
isFlushPending = true
60-
nextTick(flushJobs)
62+
currentFlushPromise = resolvedPromise.then(flushJobs)
6163
}
6264
}
6365

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

118120
flushPostFlushCbs(seen)
119121
isFlushing = false
122+
currentFlushPromise = null
120123
// some postFlushCb queued jobs!
121124
// keep flushing until it drains.
122125
if (queue.length || postFlushCbs.length) {

0 commit comments

Comments
 (0)