File tree 2 files changed +20
-2
lines changed
2 files changed +20
-2
lines changed Original file line number Diff line number Diff line change @@ -294,4 +294,19 @@ describe('scheduler', () => {
294
294
await nextTick ( )
295
295
expect ( calls ) . toEqual ( [ 'cb1' , 'cb2' ] )
296
296
} )
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
+ } )
297
312
} )
Original file line number Diff line number Diff line change @@ -8,7 +8,8 @@ export interface Job {
8
8
9
9
const queue : ( Job | null ) [ ] = [ ]
10
10
const postFlushCbs : Function [ ] = [ ]
11
- const p = Promise . resolve ( )
11
+ const resolvedPromise : Promise < any > = Promise . resolve ( )
12
+ let currentFlushPromise : Promise < void > | null = null
12
13
13
14
let isFlushing = false
14
15
let isFlushPending = false
@@ -20,6 +21,7 @@ const RECURSION_LIMIT = 100
20
21
type CountMap = Map < Job | Function , number >
21
22
22
23
export function nextTick ( fn ?: ( ) => void ) : Promise < void > {
24
+ const p = currentFlushPromise || resolvedPromise
23
25
return fn ? p . then ( fn ) : p
24
26
}
25
27
@@ -57,7 +59,7 @@ export function queuePostFlushCb(cb: Function | Function[]) {
57
59
function queueFlush ( ) {
58
60
if ( ! isFlushing && ! isFlushPending ) {
59
61
isFlushPending = true
60
- nextTick ( flushJobs )
62
+ currentFlushPromise = resolvedPromise . then ( flushJobs )
61
63
}
62
64
}
63
65
@@ -117,6 +119,7 @@ function flushJobs(seen?: CountMap) {
117
119
118
120
flushPostFlushCbs ( seen )
119
121
isFlushing = false
122
+ currentFlushPromise = null
120
123
// some postFlushCb queued jobs!
121
124
// keep flushing until it drains.
122
125
if ( queue . length || postFlushCbs . length ) {
You can’t perform that action at this time.
0 commit comments