]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(scheduler): sort nested postFlushCbs
authorEvan You <yyx990803@gmail.com>
Mon, 8 Jan 2024 10:20:29 +0000 (18:20 +0800)
committerEvan You <yyx990803@gmail.com>
Mon, 8 Jan 2024 10:20:29 +0000 (18:20 +0800)
close #10003

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

index c6fe292fb7b4761ed5b8a77bdfff2a40d68b6c46..781aa6eb0b773ff97b1f9f97991f02363c3c12aa 100644 (file)
@@ -610,4 +610,25 @@ describe('scheduler', () => {
     expect(await p).toBe(1)
     expect(fn).toHaveBeenCalledTimes(1)
   })
+
+  // #10003
+  test('nested flushPostFlushCbs', async () => {
+    const calls: string[] = []
+    const cb1 = () => calls.push('cb1')
+    // cb1 has no id
+    const cb2 = () => calls.push('cb2')
+    cb2.id = -1
+    const queueAndFlush = (hook: Function) => {
+      queuePostFlushCb(hook)
+      flushPostFlushCbs()
+    }
+
+    queueAndFlush(() => {
+      queuePostFlushCb([cb1, cb2])
+      flushPostFlushCbs()
+    })
+
+    await nextTick()
+    expect(calls).toEqual(['cb2', 'cb1'])
+  })
 })
index 78b4d7d9a98c718ea28e116ac2deff80e32e847c..b8d1445a183db551226294d68e65f7f0a1f3350d 100644 (file)
@@ -164,7 +164,9 @@ export function flushPreFlushCbs(
 
 export function flushPostFlushCbs(seen?: CountMap) {
   if (pendingPostFlushCbs.length) {
-    const deduped = [...new Set(pendingPostFlushCbs)]
+    const deduped = [...new Set(pendingPostFlushCbs)].sort(
+      (a, b) => getId(a) - getId(b),
+    )
     pendingPostFlushCbs.length = 0
 
     // #1947 already has active queue, nested flushPostFlushCbs call
@@ -178,8 +180,6 @@ export function flushPostFlushCbs(seen?: CountMap) {
       seen = seen || new Map()
     }
 
-    activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
-
     for (
       postFlushIndex = 0;
       postFlushIndex < activePostFlushCbs.length;