Skip to content

Commit d810a1a

Browse files
committed
fix(scheduler): fix insertion for id-less job
fix #4148
1 parent 6eb47f0 commit d810a1a

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ describe('scheduler', () => {
5151

5252
queueJob(job2)
5353
queueJob(job3)
54-
queueJob(job4)
5554
}
5655

5756
const job2 = () => {
5857
calls.push('job2')
58+
queueJob(job4)
59+
queueJob(job5)
5960
}
6061
job2.id = 10
6162

@@ -64,16 +65,19 @@ describe('scheduler', () => {
6465
}
6566
job3.id = 1
6667

67-
// job4 gets the Infinity as it's id
6868
const job4 = () => {
6969
calls.push('job4')
7070
}
7171

72+
const job5 = () => {
73+
calls.push('job5')
74+
}
75+
7276
queueJob(job1)
7377

7478
expect(calls).toEqual([])
7579
await nextTick()
76-
expect(calls).toEqual(['job1', 'job3', 'job2', 'job4'])
80+
expect(calls).toEqual(['job1', 'job3', 'job2', 'job4', 'job5'])
7781
})
7882

7983
it('should dedupe queued jobs', async () => {

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ setComputedScheduler(queueJob)
1010
export interface SchedulerJob extends Function {
1111
id?: number
1212
active?: boolean
13+
computed?: boolean
1314
/**
1415
* Indicates whether the effect is allowed to recursively trigger itself
1516
* when managed by the scheduler.
@@ -70,16 +71,15 @@ export function nextTick<T = void>(
7071
// Use binary-search to find a suitable position in the queue,
7172
// so that the queue maintains the increasing order of job's id,
7273
// which can prevent the job from being skipped and also can avoid repeated patching.
73-
function findInsertionIndex(job: SchedulerJob) {
74+
function findInsertionIndex(id: number) {
7475
// the start index should be `flushIndex + 1`
7576
let start = flushIndex + 1
7677
let end = queue.length
77-
const jobId = getId(job)
7878

7979
while (start < end) {
8080
const middle = (start + end) >>> 1
8181
const middleJobId = getId(queue[middle])
82-
middleJobId < jobId ? (start = middle + 1) : (end = middle)
82+
middleJobId < id ? (start = middle + 1) : (end = middle)
8383
}
8484

8585
return start
@@ -100,11 +100,10 @@ export function queueJob(job: SchedulerJob) {
100100
)) &&
101101
job !== currentPreFlushParentJob
102102
) {
103-
const pos = findInsertionIndex(job)
104-
if (pos > -1) {
105-
queue.splice(pos, 0, job)
106-
} else {
103+
if (job.id == null) {
107104
queue.push(job)
105+
} else {
106+
queue.splice(findInsertionIndex(job.id), 0, job)
108107
}
109108
queueFlush()
110109
}
@@ -253,6 +252,7 @@ function flushJobs(seen?: CountMap) {
253252
if (__DEV__ && checkRecursiveUpdates(seen!, job)) {
254253
continue
255254
}
255+
// console.log(`running:`, job.id)
256256
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
257257
}
258258
}

0 commit comments

Comments
 (0)