fix(scheduler): sort nested postFlushCbs

close #10003
This commit is contained in:
Evan You 2024-01-08 18:20:29 +08:00
parent 324e817ef8
commit d9162dfc2e
2 changed files with 24 additions and 3 deletions

View 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'])
})
})

View 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;