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()
+ })
})
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
type CountMap = Map<Job | Function, number>
export function nextTick(fn?: () => void): Promise<void> {
+ const p = currentFlushPromise || resolvedPromise
return fn ? p.then(fn) : p
}
function queueFlush() {
if (!isFlushing && !isFlushPending) {
isFlushPending = true
- nextTick(flushJobs)
+ currentFlushPromise = resolvedPromise.then(flushJobs)
}
}
flushPostFlushCbs(seen)
isFlushing = false
+ currentFlushPromise = null
// some postFlushCb queued jobs!
// keep flushing until it drains.
if (queue.length || postFlushCbs.length) {