Skip to content

Commit

Permalink
fix(scheduler): ensure jobs are in the correct order (#7748)
Browse files Browse the repository at this point in the history
close #7576
  • Loading branch information
skirtles-code authored Oct 21, 2023
1 parent 089d36d commit a8f6638
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
54 changes: 53 additions & 1 deletion packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe('scheduler', () => {
queueJob(job1)
// cb2 should execute before the job
queueJob(cb2)
queueJob(cb3)
}
cb1.pre = true

Expand All @@ -152,9 +153,60 @@ describe('scheduler', () => {
cb2.pre = true
cb2.id = 1

const cb3 = () => {
calls.push('cb3')
}
cb3.pre = true
cb3.id = 1

queueJob(cb1)
await nextTick()
expect(calls).toEqual(['cb1', 'cb2', 'job1'])
expect(calls).toEqual(['cb1', 'cb2', 'cb3', 'job1'])
})

it('should insert jobs after pre jobs with the same id', async () => {
const calls: string[] = []
const job1 = () => {
calls.push('job1')
}
job1.id = 1
job1.pre = true
const job2 = () => {
calls.push('job2')
queueJob(job5)
queueJob(job6)
}
job2.id = 2
job2.pre = true
const job3 = () => {
calls.push('job3')
}
job3.id = 2
job3.pre = true
const job4 = () => {
calls.push('job4')
}
job4.id = 3
job4.pre = true
const job5 = () => {
calls.push('job5')
}
job5.id = 2
const job6 = () => {
calls.push('job6')
}
job6.id = 2
job6.pre = true

// We need several jobs to test this properly, otherwise
// findInsertionIndex can yield the correct index by chance
queueJob(job4)
queueJob(job2)
queueJob(job3)
queueJob(job1)

await nextTick()
expect(calls).toEqual(['job1', 'job2', 'job3', 'job6', 'job5', 'job4'])
})

it('preFlushCb inside queueJob', async () => {
Expand Down
9 changes: 7 additions & 2 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,13 @@ function findInsertionIndex(id: number) {

while (start < end) {
const middle = (start + end) >>> 1
const middleJobId = getId(queue[middle])
middleJobId < id ? (start = middle + 1) : (end = middle)
const middleJob = queue[middle]
const middleJobId = getId(middleJob)
if (middleJobId < id || (middleJobId === id && middleJob.pre)) {
start = middle + 1
} else {
end = middle
}
}

return start
Expand Down

0 comments on commit a8f6638

Please sign in to comment.