fix(runtime-core): pre jobs without an id should run first (#7746)

This commit is contained in:
skirtle 2024-08-19 03:13:50 +01:00 committed by GitHub
parent 7a6c6652e0
commit b332f80f0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 8 deletions

View File

@ -239,6 +239,37 @@ describe('scheduler', () => {
expect(calls).toEqual(['cb1', 'cb2', 'job1'])
})
it('should insert pre jobs without ids first during flushing', async () => {
const calls: string[] = []
const job1: SchedulerJob = () => {
calls.push('job1')
queueJob(job3)
queueJob(job4)
}
// job1 has no id
job1.flags! |= SchedulerJobFlags.PRE
const job2: SchedulerJob = () => {
calls.push('job2')
}
job2.id = 1
job2.flags! |= SchedulerJobFlags.PRE
const job3: SchedulerJob = () => {
calls.push('job3')
}
// job3 has no id
job3.flags! |= SchedulerJobFlags.PRE
const job4: SchedulerJob = () => {
calls.push('job4')
}
// job4 has no id
job4.flags! |= SchedulerJobFlags.PRE
queueJob(job1)
queueJob(job2)
await nextTick()
expect(calls).toEqual(['job1', 'job3', 'job4', 'job2'])
})
// #3806
it('queue preFlushCb inside postFlushCb', async () => {
const spy = vi.fn()
@ -448,12 +479,20 @@ describe('scheduler', () => {
job2.id = 2
const job3 = () => calls.push('job3')
job3.id = 1
const job4: SchedulerJob = () => calls.push('job4')
job4.id = 2
job4.flags! |= SchedulerJobFlags.PRE
const job5: SchedulerJob = () => calls.push('job5')
// job5 has no id
job5.flags! |= SchedulerJobFlags.PRE
queueJob(job1)
queueJob(job2)
queueJob(job3)
queueJob(job4)
queueJob(job5)
await nextTick()
expect(calls).toEqual(['job3', 'job2', 'job1'])
expect(calls).toEqual(['job5', 'job3', 'job4', 'job2', 'job1'])
})
test('sort SchedulerCbs based on id', async () => {

View File

@ -92,16 +92,16 @@ function findInsertionIndex(id: number) {
export function queueJob(job: SchedulerJob): void {
if (!(job.flags! & SchedulerJobFlags.QUEUED)) {
if (job.id == null) {
queue.push(job)
} else if (
const jobId = getId(job)
const lastJob = queue[queue.length - 1]
if (
!lastJob ||
// fast path when the job id is larger than the tail
!(job.flags! & SchedulerJobFlags.PRE) &&
job.id >= ((queue[queue.length - 1] && queue[queue.length - 1].id) || 0)
(!(job.flags! & SchedulerJobFlags.PRE) && jobId >= getId(lastJob))
) {
queue.push(job)
} else {
queue.splice(findInsertionIndex(job.id), 0, job)
queue.splice(findInsertionIndex(jobId), 0, job)
}
if (!(job.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
@ -206,7 +206,7 @@ export function flushPostFlushCbs(seen?: CountMap): void {
}
const getId = (job: SchedulerJob): number =>
job.id == null ? Infinity : job.id
job.id == null ? (job.flags! & SchedulerJobFlags.PRE ? -1 : Infinity) : job.id
const comparator = (a: SchedulerJob, b: SchedulerJob): number => {
const diff = getId(a) - getId(b)