Skip to content

Commit b4c4fef

Browse files
committed
refactor(runtime-core): update queueJob to accept order parameter for job prioritization
1 parent 0ab95b7 commit b4c4fef

File tree

11 files changed

+235
-244
lines changed

11 files changed

+235
-244
lines changed

packages/runtime-core/__tests__/scheduler.spec.ts

Lines changed: 55 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,19 @@ describe('scheduler', () => {
4949
const job1 = () => {
5050
calls.push('job1')
5151

52-
queueJob(job2)
53-
queueJob(job3)
52+
queueJob(job2, 10)
53+
queueJob(job3, 1)
5454
}
5555

5656
const job2 = () => {
5757
calls.push('job2')
5858
queueJob(job4)
5959
queueJob(job5)
6060
}
61-
job2.id = 10
6261

6362
const job3 = () => {
6463
calls.push('job3')
6564
}
66-
job3.id = 1
6765

6866
const job4 = () => {
6967
calls.push('job4')
@@ -126,7 +124,7 @@ describe('scheduler', () => {
126124
queueJob(job1)
127125
}
128126

129-
queueJob(cb1, true)
127+
queueJob(cb1, undefined, true)
130128
await nextTick()
131129
expect(calls).toEqual(['cb1', 'job1'])
132130
})
@@ -136,27 +134,23 @@ describe('scheduler', () => {
136134
const job1 = () => {
137135
calls.push('job1')
138136
}
139-
job1.id = 1
140137

141138
const cb1: SchedulerJob = () => {
142139
calls.push('cb1')
143-
queueJob(job1)
140+
queueJob(job1, 1)
144141
// cb2 should execute before the job
145-
queueJob(cb2, true)
146-
queueJob(cb3, true)
142+
queueJob(cb2, 1, true)
143+
queueJob(cb3, 1, true)
147144
}
148145

149146
const cb2: SchedulerJob = () => {
150147
calls.push('cb2')
151148
}
152-
cb2.id = 1
153-
154149
const cb3: SchedulerJob = () => {
155150
calls.push('cb3')
156151
}
157-
cb3.id = 1
158152

159-
queueJob(cb1, true)
153+
queueJob(cb1, undefined, true)
160154
await nextTick()
161155
expect(calls).toEqual(['cb1', 'cb2', 'cb3', 'job1'])
162156
})
@@ -166,36 +160,30 @@ describe('scheduler', () => {
166160
const job1: SchedulerJob = () => {
167161
calls.push('job1')
168162
}
169-
job1.id = 1
170163
const job2: SchedulerJob = () => {
171164
calls.push('job2')
172-
queueJob(job5)
173-
queueJob(job6, true)
165+
queueJob(job5, 2)
166+
queueJob(job6, 2, true)
174167
}
175-
job2.id = 2
176168
const job3: SchedulerJob = () => {
177169
calls.push('job3')
178170
}
179-
job3.id = 2
180171
const job4: SchedulerJob = () => {
181172
calls.push('job4')
182173
}
183-
job4.id = 3
184174
const job5: SchedulerJob = () => {
185175
calls.push('job5')
186176
}
187-
job5.id = 2
188177
const job6: SchedulerJob = () => {
189178
calls.push('job6')
190179
}
191-
job6.id = 2
192180

193181
// We need several jobs to test this properly, otherwise
194182
// findInsertionIndex can yield the correct index by chance
195-
queueJob(job4, true)
196-
queueJob(job2, true)
197-
queueJob(job3, true)
198-
queueJob(job1, true)
183+
queueJob(job4, 3, true)
184+
queueJob(job2, 2, true)
185+
queueJob(job3, 2, true)
186+
queueJob(job1, 1, true)
199187

200188
await nextTick()
201189
expect(calls).toEqual(['job1', 'job2', 'job3', 'job6', 'job5', 'job4'])
@@ -208,8 +196,8 @@ describe('scheduler', () => {
208196
// when updating the props of a child component. This is handled
209197
// directly inside `updateComponentPreRender` to avoid non atomic
210198
// cb triggers (#1763)
211-
queueJob(cb1, true)
212-
queueJob(cb2, true)
199+
queueJob(cb1, undefined, true)
200+
queueJob(cb2, undefined, true)
213201
flushPreFlushCbs()
214202
calls.push('job1')
215203
}
@@ -231,14 +219,13 @@ describe('scheduler', () => {
231219
const calls: string[] = []
232220
const job1: SchedulerJob = () => {
233221
calls.push('job1')
234-
queueJob(job3, true)
235-
queueJob(job4, true)
222+
queueJob(job3, undefined, true)
223+
queueJob(job4, undefined, true)
236224
}
237225
// job1 has no id
238226
const job2: SchedulerJob = () => {
239227
calls.push('job2')
240228
}
241-
job2.id = 1
242229
const job3: SchedulerJob = () => {
243230
calls.push('job3')
244231
}
@@ -248,8 +235,8 @@ describe('scheduler', () => {
248235
}
249236
// job4 has no id
250237

251-
queueJob(job1, true)
252-
queueJob(job2, true)
238+
queueJob(job1, undefined, true)
239+
queueJob(job2, 1, true)
253240
await nextTick()
254241
expect(calls).toEqual(['job1', 'job3', 'job4', 'job2'])
255242
})
@@ -259,7 +246,7 @@ describe('scheduler', () => {
259246
const spy = vi.fn()
260247
const cb: SchedulerJob = () => spy()
261248
queuePostFlushCb(() => {
262-
queueJob(cb, true)
249+
queueJob(cb, undefined, true)
263250
})
264251
await nextTick()
265252
expect(spy).toHaveBeenCalled()
@@ -432,16 +419,13 @@ describe('scheduler', () => {
432419
const job1: SchedulerJob = () => {
433420
calls.push('job1')
434421
}
435-
job1.id = 1
436-
437422
const job2: SchedulerJob = () => {
438423
calls.push('job2')
439424
}
440-
job2.id = 2
441425

442426
queuePostFlushCb(() => {
443-
queueJob(job2)
444-
queueJob(job1)
427+
queueJob(job2, 2)
428+
queueJob(job1, 1)
445429
})
446430

447431
await nextTick()
@@ -455,19 +439,16 @@ describe('scheduler', () => {
455439
const job1 = () => calls.push('job1')
456440
// job1 has no id
457441
const job2 = () => calls.push('job2')
458-
job2.id = 2
459442
const job3 = () => calls.push('job3')
460-
job3.id = 1
461443
const job4: SchedulerJob = () => calls.push('job4')
462-
job4.id = 2
463444
const job5: SchedulerJob = () => calls.push('job5')
464445
// job5 has no id
465446

466447
queueJob(job1)
467-
queueJob(job2)
468-
queueJob(job3)
469-
queueJob(job4, true)
470-
queueJob(job5, true)
448+
queueJob(job2, 2)
449+
queueJob(job3, 1)
450+
queueJob(job4, 2, true)
451+
queueJob(job5, undefined, true)
471452
await nextTick()
472453
expect(calls).toEqual(['job5', 'job3', 'job4', 'job2', 'job1'])
473454
})
@@ -477,13 +458,11 @@ describe('scheduler', () => {
477458
const cb1 = () => calls.push('cb1')
478459
// cb1 has no id
479460
const cb2 = () => calls.push('cb2')
480-
cb2.id = 2
481461
const cb3 = () => calls.push('cb3')
482-
cb3.id = 1
483462

484463
queuePostFlushCb(cb1)
485-
queuePostFlushCb(cb2)
486-
queuePostFlushCb(cb3)
464+
queuePostFlushCb(cb2, 2)
465+
queuePostFlushCb(cb3, 1)
487466
await nextTick()
488467
expect(calls).toEqual(['cb3', 'cb2', 'cb1'])
489468
})
@@ -532,13 +511,10 @@ describe('scheduler', () => {
532511
throw err
533512
}
534513
})
535-
job1.id = 1
536-
537514
const job2: SchedulerJob = vi.fn()
538-
job2.id = 2
539515

540-
queueJob(job1)
541-
queueJob(job2)
516+
queueJob(job1, 1)
517+
queueJob(job2, 2)
542518

543519
try {
544520
await nextTick()
@@ -552,8 +528,8 @@ describe('scheduler', () => {
552528
expect(job1).toHaveBeenCalledTimes(1)
553529
expect(job2).toHaveBeenCalledTimes(0)
554530

555-
queueJob(job1)
556-
queueJob(job2)
531+
queueJob(job1, 1)
532+
queueJob(job2, 2)
557533

558534
await nextTick()
559535

@@ -604,11 +580,10 @@ describe('scheduler', () => {
604580

605581
test('recursive jobs can only be queued once non-recursively', async () => {
606582
const job: SchedulerJob = vi.fn()
607-
job.id = 1
608583
job.flags = SchedulerJobFlags.ALLOW_RECURSE
609584

610-
queueJob(job)
611-
queueJob(job)
585+
queueJob(job, 1)
586+
queueJob(job, 1)
612587

613588
await nextTick()
614589

@@ -620,15 +595,14 @@ describe('scheduler', () => {
620595

621596
const job: SchedulerJob = vi.fn(() => {
622597
if (recurse) {
623-
queueJob(job)
624-
queueJob(job)
598+
queueJob(job, 1)
599+
queueJob(job, 1)
625600
recurse = false
626601
}
627602
})
628-
job.id = 1
629603
job.flags = SchedulerJobFlags.ALLOW_RECURSE
630604

631-
queueJob(job)
605+
queueJob(job, 1)
632606

633607
await nextTick()
634608

@@ -641,22 +615,19 @@ describe('scheduler', () => {
641615
const job1: SchedulerJob = () => {
642616
if (recurse) {
643617
// job2 is already queued, so this shouldn't do anything
644-
queueJob(job2)
618+
queueJob(job2, 2)
645619
recurse = false
646620
}
647621
}
648-
job1.id = 1
649-
650622
const job2: SchedulerJob = vi.fn(() => {
651623
if (recurse) {
652-
queueJob(job1)
653-
queueJob(job2)
624+
queueJob(job1, 1)
625+
queueJob(job2, 2)
654626
}
655627
})
656-
job2.id = 2
657628
job2.flags = SchedulerJobFlags.ALLOW_RECURSE
658629

659-
queueJob(job2)
630+
queueJob(job2, 2)
660631

661632
await nextTick()
662633

@@ -667,38 +638,35 @@ describe('scheduler', () => {
667638
let recurse = true
668639

669640
const job1: SchedulerJob = vi.fn(() => {
670-
queueJob(job3, true)
671-
queueJob(job3, true)
641+
queueJob(job3, 3, true)
642+
queueJob(job3, 3, true)
672643
flushPreFlushCbs()
673644
})
674-
job1.id = 1
675645

676646
const job2: SchedulerJob = vi.fn(() => {
677647
if (recurse) {
678648
// job2 does not allow recurse, so this shouldn't do anything
679-
queueJob(job2, true)
649+
queueJob(job2, 2, true)
680650

681651
// job3 is already queued, so this shouldn't do anything
682-
queueJob(job3, true)
652+
queueJob(job3, 3, true)
683653
recurse = false
684654
}
685655
})
686-
job2.id = 2
687656

688657
const job3: SchedulerJob = vi.fn(() => {
689658
if (recurse) {
690-
queueJob(job2, true)
691-
queueJob(job3, true)
659+
queueJob(job2, 2, true)
660+
queueJob(job3, 3, true)
692661

693662
// The jobs are already queued, so these should have no effect
694-
queueJob(job2, true)
695-
queueJob(job3, true)
663+
queueJob(job2, 2, true)
664+
queueJob(job3, 3, true)
696665
}
697666
})
698-
job3.id = 3
699667
job3.flags = SchedulerJobFlags.ALLOW_RECURSE
700668

701-
queueJob(job1, true)
669+
queueJob(job1, 1, true)
702670

703671
await nextTick()
704672

@@ -755,7 +723,7 @@ describe('scheduler', () => {
755723
spy()
756724
flushPreFlushCbs()
757725
}
758-
queueJob(job, true)
726+
queueJob(job, undefined, true)
759727
await nextTick()
760728
expect(spy).toHaveBeenCalledTimes(1)
761729
})
@@ -767,16 +735,14 @@ describe('scheduler', () => {
767735
const job1: SchedulerJob = () => {
768736
calls.push('job1')
769737
}
770-
job1.id = 1
771738

772739
const job2: SchedulerJob = () => {
773740
calls.push('job2')
774741
}
775-
job2.id = 2
776742

777743
queuePostFlushCb(() => {
778-
queueJob(job2, true)
779-
queueJob(job1, true)
744+
queueJob(job2, 2, true)
745+
queueJob(job1, 1, true)
780746

781747
// e.g. nested app.mount() call
782748
flushPreFlushCbs()
@@ -807,14 +773,14 @@ describe('scheduler', () => {
807773
const cb1 = () => calls.push('cb1')
808774
// cb1 has no id
809775
const cb2 = () => calls.push('cb2')
810-
cb2.id = -1
811776
const queueAndFlush = (hook: Function) => {
812777
queuePostFlushCb(hook)
813778
flushPostFlushCbs()
814779
}
815780

816781
queueAndFlush(() => {
817-
queuePostFlushCb([cb1, cb2])
782+
queuePostFlushCb(cb1)
783+
queuePostFlushCb(cb2, -1)
818784
flushPostFlushCbs()
819785
})
820786

0 commit comments

Comments
 (0)