Skip to content

Commit

Permalink
fix(scheduler): sort nested postFlushCbs
Browse files Browse the repository at this point in the history
close #10003
  • Loading branch information
yyx990803 committed Jan 8, 2024
1 parent 324e817 commit d9162df
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 21 additions & 0 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
})
})
6 changes: 3 additions & 3 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down

0 comments on commit d9162df

Please sign in to comment.