Skip to content

Commit 4b608a9

Browse files
refactor(scheduler): remove invalidateJob (#11650)
Co-authored-by: Evan You <[email protected]>
1 parent c183405 commit 4b608a9

File tree

4 files changed

+99
-39
lines changed

4 files changed

+99
-39
lines changed

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,105 @@ describe('renderer: component', () => {
236236
expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
237237
})
238238

239+
test('child only updates once when triggered in multiple ways', async () => {
240+
const a = ref(0)
241+
const calls: string[] = []
242+
243+
const Parent = {
244+
setup() {
245+
return () => {
246+
calls.push('render parent')
247+
return h(Child, { count: a.value }, () => a.value)
248+
}
249+
},
250+
}
251+
252+
const Child = {
253+
props: ['count'],
254+
setup(props: any) {
255+
return () => {
256+
calls.push('render child')
257+
return `${props.count} - ${a.value}`
258+
}
259+
},
260+
}
261+
262+
render(h(Parent), nodeOps.createElement('div'))
263+
expect(calls).toEqual(['render parent', 'render child'])
264+
265+
// This will trigger child rendering directly, as well as via a prop change
266+
a.value++
267+
await nextTick()
268+
expect(calls).toEqual([
269+
'render parent',
270+
'render child',
271+
'render parent',
272+
'render child',
273+
])
274+
})
275+
276+
// #7745
277+
test(`an earlier update doesn't lead to excessive subsequent updates`, async () => {
278+
const globalCount = ref(0)
279+
const parentCount = ref(0)
280+
const calls: string[] = []
281+
282+
const Root = {
283+
setup() {
284+
return () => {
285+
calls.push('render root')
286+
return h(Parent, { count: globalCount.value })
287+
}
288+
},
289+
}
290+
291+
const Parent = {
292+
props: ['count'],
293+
setup(props: any) {
294+
return () => {
295+
calls.push('render parent')
296+
return [
297+
`${globalCount.value} - ${props.count}`,
298+
h(Child, { count: parentCount.value }),
299+
]
300+
}
301+
},
302+
}
303+
304+
const Child = {
305+
props: ['count'],
306+
setup(props: any) {
307+
watch(
308+
() => props.count,
309+
() => {
310+
calls.push('child watcher')
311+
globalCount.value = props.count
312+
},
313+
)
314+
315+
return () => {
316+
calls.push('render child')
317+
}
318+
},
319+
}
320+
321+
render(h(Root), nodeOps.createElement('div'))
322+
expect(calls).toEqual(['render root', 'render parent', 'render child'])
323+
324+
parentCount.value++
325+
await nextTick()
326+
expect(calls).toEqual([
327+
'render root',
328+
'render parent',
329+
'render child',
330+
'render parent',
331+
'child watcher',
332+
'render child',
333+
'render root',
334+
'render parent',
335+
])
336+
})
337+
239338
// #2521
240339
test('should pause tracking deps when initializing legacy options', async () => {
241340
let childInstance = null as any

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
SchedulerJobFlags,
44
flushPostFlushCbs,
55
flushPreFlushCbs,
6-
invalidateJob,
76
nextTick,
87
queueJob,
98
queuePostFlushCb,
@@ -444,33 +443,6 @@ describe('scheduler', () => {
444443
})
445444
})
446445

447-
test('invalidateJob', async () => {
448-
const calls: string[] = []
449-
const job1 = () => {
450-
calls.push('job1')
451-
invalidateJob(job2)
452-
job2()
453-
}
454-
const job2 = () => {
455-
calls.push('job2')
456-
}
457-
const job3 = () => {
458-
calls.push('job3')
459-
}
460-
const job4 = () => {
461-
calls.push('job4')
462-
}
463-
// queue all jobs
464-
queueJob(job1)
465-
queueJob(job2)
466-
queueJob(job3)
467-
queuePostFlushCb(job4)
468-
expect(calls).toEqual([])
469-
await nextTick()
470-
// job2 should be called only once
471-
expect(calls).toEqual(['job1', 'job2', 'job3', 'job4'])
472-
})
473-
474446
test('sort job based on id', async () => {
475447
const calls: string[] = []
476448
const job1 = () => calls.push('job1')

packages/runtime-core/src/renderer.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import {
4545
type SchedulerJobs,
4646
flushPostFlushCbs,
4747
flushPreFlushCbs,
48-
invalidateJob,
4948
queueJob,
5049
queuePostFlushCb,
5150
} from './scheduler'
@@ -1255,9 +1254,6 @@ function baseCreateRenderer(
12551254
} else {
12561255
// normal update
12571256
instance.next = n2
1258-
// in case the child component is also queued, remove it to avoid
1259-
// double updating the same child component in the same flush.
1260-
invalidateJob(instance.update)
12611257
// instance.update is the reactive effect.
12621258
instance.update()
12631259
}

packages/runtime-core/src/scheduler.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,6 @@ function queueFlush() {
122122
}
123123
}
124124

125-
export function invalidateJob(job: SchedulerJob): void {
126-
const i = queue.indexOf(job)
127-
if (i > flushIndex) {
128-
queue.splice(i, 1)
129-
}
130-
}
131-
132125
export function queuePostFlushCb(cb: SchedulerJobs): void {
133126
if (!isArray(cb)) {
134127
if (activePostFlushCbs && cb.id === -1) {

0 commit comments

Comments
 (0)