Преглед изворни кода

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

Evan You пре 5 година
родитељ
комит
7e8b26eba8

+ 15 - 0
packages/runtime-core/__tests__/scheduler.spec.ts

@@ -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()
+  })
 })

+ 5 - 2
packages/runtime-core/src/scheduler.ts

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