Skip to content

Commit 1b0d881

Browse files
committed
refactor(runtime-core): decouple pre type scheduler job processing
1 parent 4972e17 commit 1b0d881

File tree

3 files changed

+156
-122
lines changed

3 files changed

+156
-122
lines changed

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

+31-54
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ describe('scheduler', () => {
125125
calls.push('cb1')
126126
queueJob(job1)
127127
}
128-
cb1.flags! |= SchedulerJobFlags.PRE
129128

130-
queueJob(cb1)
129+
queueJob(cb1, true)
131130
await nextTick()
132131
expect(calls).toEqual(['cb1', 'job1'])
133132
})
@@ -143,24 +142,21 @@ describe('scheduler', () => {
143142
calls.push('cb1')
144143
queueJob(job1)
145144
// cb2 should execute before the job
146-
queueJob(cb2)
147-
queueJob(cb3)
145+
queueJob(cb2, true)
146+
queueJob(cb3, true)
148147
}
149-
cb1.flags! |= SchedulerJobFlags.PRE
150148

151149
const cb2: SchedulerJob = () => {
152150
calls.push('cb2')
153151
}
154-
cb2.flags! |= SchedulerJobFlags.PRE
155152
cb2.id = 1
156153

157154
const cb3: SchedulerJob = () => {
158155
calls.push('cb3')
159156
}
160-
cb3.flags! |= SchedulerJobFlags.PRE
161157
cb3.id = 1
162158

163-
queueJob(cb1)
159+
queueJob(cb1, true)
164160
await nextTick()
165161
expect(calls).toEqual(['cb1', 'cb2', 'cb3', 'job1'])
166162
})
@@ -171,24 +167,20 @@ describe('scheduler', () => {
171167
calls.push('job1')
172168
}
173169
job1.id = 1
174-
job1.flags! |= SchedulerJobFlags.PRE
175170
const job2: SchedulerJob = () => {
176171
calls.push('job2')
177172
queueJob(job5)
178-
queueJob(job6)
173+
queueJob(job6, true)
179174
}
180175
job2.id = 2
181-
job2.flags! |= SchedulerJobFlags.PRE
182176
const job3: SchedulerJob = () => {
183177
calls.push('job3')
184178
}
185179
job3.id = 2
186-
job3.flags! |= SchedulerJobFlags.PRE
187180
const job4: SchedulerJob = () => {
188181
calls.push('job4')
189182
}
190183
job4.id = 3
191-
job4.flags! |= SchedulerJobFlags.PRE
192184
const job5: SchedulerJob = () => {
193185
calls.push('job5')
194186
}
@@ -197,14 +189,13 @@ describe('scheduler', () => {
197189
calls.push('job6')
198190
}
199191
job6.id = 2
200-
job6.flags! |= SchedulerJobFlags.PRE
201192

202193
// We need several jobs to test this properly, otherwise
203194
// findInsertionIndex can yield the correct index by chance
204-
queueJob(job4)
205-
queueJob(job2)
206-
queueJob(job3)
207-
queueJob(job1)
195+
queueJob(job4, true)
196+
queueJob(job2, true)
197+
queueJob(job3, true)
198+
queueJob(job1, true)
208199

209200
await nextTick()
210201
expect(calls).toEqual(['job1', 'job2', 'job3', 'job6', 'job5', 'job4'])
@@ -217,8 +208,8 @@ describe('scheduler', () => {
217208
// when updating the props of a child component. This is handled
218209
// directly inside `updateComponentPreRender` to avoid non atomic
219210
// cb triggers (#1763)
220-
queueJob(cb1)
221-
queueJob(cb2)
211+
queueJob(cb1, true)
212+
queueJob(cb2, true)
222213
flushPreFlushCbs()
223214
calls.push('job1')
224215
}
@@ -227,11 +218,9 @@ describe('scheduler', () => {
227218
// a cb triggers its parent job, which should be skipped
228219
queueJob(job1)
229220
}
230-
cb1.flags! |= SchedulerJobFlags.PRE
231221
const cb2: SchedulerJob = () => {
232222
calls.push('cb2')
233223
}
234-
cb2.flags! |= SchedulerJobFlags.PRE
235224

236225
queueJob(job1)
237226
await nextTick()
@@ -242,29 +231,25 @@ describe('scheduler', () => {
242231
const calls: string[] = []
243232
const job1: SchedulerJob = () => {
244233
calls.push('job1')
245-
queueJob(job3)
246-
queueJob(job4)
234+
queueJob(job3, true)
235+
queueJob(job4, true)
247236
}
248237
// job1 has no id
249-
job1.flags! |= SchedulerJobFlags.PRE
250238
const job2: SchedulerJob = () => {
251239
calls.push('job2')
252240
}
253241
job2.id = 1
254-
job2.flags! |= SchedulerJobFlags.PRE
255242
const job3: SchedulerJob = () => {
256243
calls.push('job3')
257244
}
258245
// job3 has no id
259-
job3.flags! |= SchedulerJobFlags.PRE
260246
const job4: SchedulerJob = () => {
261247
calls.push('job4')
262248
}
263249
// job4 has no id
264-
job4.flags! |= SchedulerJobFlags.PRE
265250

266-
queueJob(job1)
267-
queueJob(job2)
251+
queueJob(job1, true)
252+
queueJob(job2, true)
268253
await nextTick()
269254
expect(calls).toEqual(['job1', 'job3', 'job4', 'job2'])
270255
})
@@ -273,9 +258,8 @@ describe('scheduler', () => {
273258
it('queue preFlushCb inside postFlushCb', async () => {
274259
const spy = vi.fn()
275260
const cb: SchedulerJob = () => spy()
276-
cb.flags! |= SchedulerJobFlags.PRE
277261
queuePostFlushCb(() => {
278-
queueJob(cb)
262+
queueJob(cb, true)
279263
})
280264
await nextTick()
281265
expect(spy).toHaveBeenCalled()
@@ -476,16 +460,14 @@ describe('scheduler', () => {
476460
job3.id = 1
477461
const job4: SchedulerJob = () => calls.push('job4')
478462
job4.id = 2
479-
job4.flags! |= SchedulerJobFlags.PRE
480463
const job5: SchedulerJob = () => calls.push('job5')
481464
// job5 has no id
482-
job5.flags! |= SchedulerJobFlags.PRE
483465

484466
queueJob(job1)
485467
queueJob(job2)
486468
queueJob(job3)
487-
queueJob(job4)
488-
queueJob(job5)
469+
queueJob(job4, true)
470+
queueJob(job5, true)
489471
await nextTick()
490472
expect(calls).toEqual(['job5', 'job3', 'job4', 'job2', 'job1'])
491473
})
@@ -685,40 +667,38 @@ describe('scheduler', () => {
685667
let recurse = true
686668

687669
const job1: SchedulerJob = vi.fn(() => {
688-
queueJob(job3)
689-
queueJob(job3)
670+
queueJob(job3, true)
671+
queueJob(job3, true)
690672
flushPreFlushCbs()
691673
})
692674
job1.id = 1
693-
job1.flags = SchedulerJobFlags.PRE
694675

695676
const job2: SchedulerJob = vi.fn(() => {
696677
if (recurse) {
697678
// job2 does not allow recurse, so this shouldn't do anything
698-
queueJob(job2)
679+
queueJob(job2, true)
699680

700681
// job3 is already queued, so this shouldn't do anything
701-
queueJob(job3)
682+
queueJob(job3, true)
702683
recurse = false
703684
}
704685
})
705686
job2.id = 2
706-
job2.flags = SchedulerJobFlags.PRE
707687

708688
const job3: SchedulerJob = vi.fn(() => {
709689
if (recurse) {
710-
queueJob(job2)
711-
queueJob(job3)
690+
queueJob(job2, true)
691+
queueJob(job3, true)
712692

713693
// The jobs are already queued, so these should have no effect
714-
queueJob(job2)
715-
queueJob(job3)
694+
queueJob(job2, true)
695+
queueJob(job3, true)
716696
}
717697
})
718698
job3.id = 3
719-
job3.flags = SchedulerJobFlags.ALLOW_RECURSE | SchedulerJobFlags.PRE
699+
job3.flags = SchedulerJobFlags.ALLOW_RECURSE
720700

721-
queueJob(job1)
701+
queueJob(job1, true)
722702

723703
await nextTick()
724704

@@ -775,8 +755,7 @@ describe('scheduler', () => {
775755
spy()
776756
flushPreFlushCbs()
777757
}
778-
job.flags! |= SchedulerJobFlags.PRE
779-
queueJob(job)
758+
queueJob(job, true)
780759
await nextTick()
781760
expect(spy).toHaveBeenCalledTimes(1)
782761
})
@@ -789,17 +768,15 @@ describe('scheduler', () => {
789768
calls.push('job1')
790769
}
791770
job1.id = 1
792-
job1.flags! |= SchedulerJobFlags.PRE
793771

794772
const job2: SchedulerJob = () => {
795773
calls.push('job2')
796774
}
797775
job2.id = 2
798-
job2.flags! |= SchedulerJobFlags.PRE
799776

800777
queuePostFlushCb(() => {
801-
queueJob(job2)
802-
queueJob(job1)
778+
queueJob(job2, true)
779+
queueJob(job1, true)
803780

804781
// e.g. nested app.mount() call
805782
flushPreFlushCbs()

packages/runtime-core/src/apiWatch.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ function doWatch(
203203
if (isFirstRun) {
204204
job()
205205
} else {
206-
queueJob(job)
206+
queueJob(job, true)
207207
}
208208
}
209209
}
@@ -214,12 +214,9 @@ function doWatch(
214214
if (cb) {
215215
job.flags! |= SchedulerJobFlags.ALLOW_RECURSE
216216
}
217-
if (isPre) {
218-
job.flags! |= SchedulerJobFlags.PRE
219-
if (instance) {
220-
job.id = instance.uid
221-
;(job as SchedulerJob).i = instance
222-
}
217+
if (isPre && instance) {
218+
job.id = instance.uid
219+
;(job as SchedulerJob).i = instance
223220
}
224221
}
225222

0 commit comments

Comments
 (0)