Skip to content

Commit b332f80

Browse files
fix(runtime-core): pre jobs without an id should run first (#7746)
1 parent 7a6c665 commit b332f80

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

Diff for: packages/runtime-core/__tests__/scheduler.spec.ts

+40-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,37 @@ describe('scheduler', () => {
239239
expect(calls).toEqual(['cb1', 'cb2', 'job1'])
240240
})
241241

242+
it('should insert pre jobs without ids first during flushing', async () => {
243+
const calls: string[] = []
244+
const job1: SchedulerJob = () => {
245+
calls.push('job1')
246+
queueJob(job3)
247+
queueJob(job4)
248+
}
249+
// job1 has no id
250+
job1.flags! |= SchedulerJobFlags.PRE
251+
const job2: SchedulerJob = () => {
252+
calls.push('job2')
253+
}
254+
job2.id = 1
255+
job2.flags! |= SchedulerJobFlags.PRE
256+
const job3: SchedulerJob = () => {
257+
calls.push('job3')
258+
}
259+
// job3 has no id
260+
job3.flags! |= SchedulerJobFlags.PRE
261+
const job4: SchedulerJob = () => {
262+
calls.push('job4')
263+
}
264+
// job4 has no id
265+
job4.flags! |= SchedulerJobFlags.PRE
266+
267+
queueJob(job1)
268+
queueJob(job2)
269+
await nextTick()
270+
expect(calls).toEqual(['job1', 'job3', 'job4', 'job2'])
271+
})
272+
242273
// #3806
243274
it('queue preFlushCb inside postFlushCb', async () => {
244275
const spy = vi.fn()
@@ -448,12 +479,20 @@ describe('scheduler', () => {
448479
job2.id = 2
449480
const job3 = () => calls.push('job3')
450481
job3.id = 1
482+
const job4: SchedulerJob = () => calls.push('job4')
483+
job4.id = 2
484+
job4.flags! |= SchedulerJobFlags.PRE
485+
const job5: SchedulerJob = () => calls.push('job5')
486+
// job5 has no id
487+
job5.flags! |= SchedulerJobFlags.PRE
451488

452489
queueJob(job1)
453490
queueJob(job2)
454491
queueJob(job3)
492+
queueJob(job4)
493+
queueJob(job5)
455494
await nextTick()
456-
expect(calls).toEqual(['job3', 'job2', 'job1'])
495+
expect(calls).toEqual(['job5', 'job3', 'job4', 'job2', 'job1'])
457496
})
458497

459498
test('sort SchedulerCbs based on id', async () => {

Diff for: packages/runtime-core/src/scheduler.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ function findInsertionIndex(id: number) {
9292

9393
export function queueJob(job: SchedulerJob): void {
9494
if (!(job.flags! & SchedulerJobFlags.QUEUED)) {
95-
if (job.id == null) {
96-
queue.push(job)
97-
} else if (
95+
const jobId = getId(job)
96+
const lastJob = queue[queue.length - 1]
97+
if (
98+
!lastJob ||
9899
// fast path when the job id is larger than the tail
99-
!(job.flags! & SchedulerJobFlags.PRE) &&
100-
job.id >= ((queue[queue.length - 1] && queue[queue.length - 1].id) || 0)
100+
(!(job.flags! & SchedulerJobFlags.PRE) && jobId >= getId(lastJob))
101101
) {
102102
queue.push(job)
103103
} else {
104-
queue.splice(findInsertionIndex(job.id), 0, job)
104+
queue.splice(findInsertionIndex(jobId), 0, job)
105105
}
106106

107107
if (!(job.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
@@ -206,7 +206,7 @@ export function flushPostFlushCbs(seen?: CountMap): void {
206206
}
207207

208208
const getId = (job: SchedulerJob): number =>
209-
job.id == null ? Infinity : job.id
209+
job.id == null ? (job.flags! & SchedulerJobFlags.PRE ? -1 : Infinity) : job.id
210210

211211
const comparator = (a: SchedulerJob, b: SchedulerJob): number => {
212212
const diff = getId(a) - getId(b)

0 commit comments

Comments
 (0)