Skip to content

Commit

Permalink
refactor(scheduler): remove invalidateJob (#11650)
Browse files Browse the repository at this point in the history
Co-authored-by: Evan You <evan@vuejs.org>
  • Loading branch information
skirtles-code and yyx990803 committed Aug 19, 2024
1 parent c183405 commit 4b608a9
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 39 deletions.
99 changes: 99 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,105 @@ describe('renderer: component', () => {
expect(serializeInner(root)).toBe(`<div>1</div><div>1</div>`)
})

test('child only updates once when triggered in multiple ways', async () => {
const a = ref(0)
const calls: string[] = []

const Parent = {
setup() {
return () => {
calls.push('render parent')
return h(Child, { count: a.value }, () => a.value)
}
},
}

const Child = {
props: ['count'],
setup(props: any) {
return () => {
calls.push('render child')
return `${props.count} - ${a.value}`
}
},
}

render(h(Parent), nodeOps.createElement('div'))
expect(calls).toEqual(['render parent', 'render child'])

// This will trigger child rendering directly, as well as via a prop change
a.value++
await nextTick()
expect(calls).toEqual([
'render parent',
'render child',
'render parent',
'render child',
])
})

// #7745
test(`an earlier update doesn't lead to excessive subsequent updates`, async () => {
const globalCount = ref(0)
const parentCount = ref(0)
const calls: string[] = []

const Root = {
setup() {
return () => {
calls.push('render root')
return h(Parent, { count: globalCount.value })
}
},
}

const Parent = {
props: ['count'],
setup(props: any) {
return () => {
calls.push('render parent')
return [
`${globalCount.value} - ${props.count}`,
h(Child, { count: parentCount.value }),
]
}
},
}

const Child = {
props: ['count'],
setup(props: any) {
watch(
() => props.count,
() => {
calls.push('child watcher')
globalCount.value = props.count
},
)

return () => {
calls.push('render child')
}
},
}

render(h(Root), nodeOps.createElement('div'))
expect(calls).toEqual(['render root', 'render parent', 'render child'])

parentCount.value++
await nextTick()
expect(calls).toEqual([
'render root',
'render parent',
'render child',
'render parent',
'child watcher',
'render child',
'render root',
'render parent',
])
})

// #2521
test('should pause tracking deps when initializing legacy options', async () => {
let childInstance = null as any
Expand Down
28 changes: 0 additions & 28 deletions packages/runtime-core/__tests__/scheduler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
SchedulerJobFlags,
flushPostFlushCbs,
flushPreFlushCbs,
invalidateJob,
nextTick,
queueJob,
queuePostFlushCb,
Expand Down Expand Up @@ -444,33 +443,6 @@ describe('scheduler', () => {
})
})

test('invalidateJob', async () => {
const calls: string[] = []
const job1 = () => {
calls.push('job1')
invalidateJob(job2)
job2()
}
const job2 = () => {
calls.push('job2')
}
const job3 = () => {
calls.push('job3')
}
const job4 = () => {
calls.push('job4')
}
// queue all jobs
queueJob(job1)
queueJob(job2)
queueJob(job3)
queuePostFlushCb(job4)
expect(calls).toEqual([])
await nextTick()
// job2 should be called only once
expect(calls).toEqual(['job1', 'job2', 'job3', 'job4'])
})

test('sort job based on id', async () => {
const calls: string[] = []
const job1 = () => calls.push('job1')
Expand Down
4 changes: 0 additions & 4 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
type SchedulerJobs,
flushPostFlushCbs,
flushPreFlushCbs,
invalidateJob,
queueJob,
queuePostFlushCb,
} from './scheduler'
Expand Down Expand Up @@ -1255,9 +1254,6 @@ function baseCreateRenderer(
} else {
// normal update
instance.next = n2
// in case the child component is also queued, remove it to avoid
// double updating the same child component in the same flush.
invalidateJob(instance.update)
// instance.update is the reactive effect.
instance.update()
}
Expand Down
7 changes: 0 additions & 7 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,6 @@ function queueFlush() {
}
}

export function invalidateJob(job: SchedulerJob): void {
const i = queue.indexOf(job)
if (i > flushIndex) {
queue.splice(i, 1)
}
}

export function queuePostFlushCb(cb: SchedulerJobs): void {
if (!isArray(cb)) {
if (activePostFlushCbs && cb.id === -1) {
Expand Down

0 comments on commit 4b608a9

Please sign in to comment.