Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): ensure jobs are in the correct order #7748

Merged
merged 1 commit into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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