Skip to content

Commit d9162df

Browse files
committedJan 8, 2024
fix(scheduler): sort nested postFlushCbs
close #10003
1 parent 324e817 commit d9162df

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed
 

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

+21
Original file line numberDiff line numberDiff line change
@@ -610,4 +610,25 @@ describe('scheduler', () => {
610610
expect(await p).toBe(1)
611611
expect(fn).toHaveBeenCalledTimes(1)
612612
})
613+
614+
// #10003
615+
test('nested flushPostFlushCbs', async () => {
616+
const calls: string[] = []
617+
const cb1 = () => calls.push('cb1')
618+
// cb1 has no id
619+
const cb2 = () => calls.push('cb2')
620+
cb2.id = -1
621+
const queueAndFlush = (hook: Function) => {
622+
queuePostFlushCb(hook)
623+
flushPostFlushCbs()
624+
}
625+
626+
queueAndFlush(() => {
627+
queuePostFlushCb([cb1, cb2])
628+
flushPostFlushCbs()
629+
})
630+
631+
await nextTick()
632+
expect(calls).toEqual(['cb2', 'cb1'])
633+
})
613634
})

‎packages/runtime-core/src/scheduler.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ export function flushPreFlushCbs(
164164

165165
export function flushPostFlushCbs(seen?: CountMap) {
166166
if (pendingPostFlushCbs.length) {
167-
const deduped = [...new Set(pendingPostFlushCbs)]
167+
const deduped = [...new Set(pendingPostFlushCbs)].sort(
168+
(a, b) => getId(a) - getId(b),
169+
)
168170
pendingPostFlushCbs.length = 0
169171

170172
// #1947 already has active queue, nested flushPostFlushCbs call
@@ -178,8 +180,6 @@ export function flushPostFlushCbs(seen?: CountMap) {
178180
seen = seen || new Map()
179181
}
180182

181-
activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
182-
183183
for (
184184
postFlushIndex = 0;
185185
postFlushIndex < activePostFlushCbs.length;

0 commit comments

Comments
 (0)
Please sign in to comment.