From d673dd1b7b3f189cbaf688ed368939f071135d89 Mon Sep 17 00:00:00 2001 From: ganhaobo Date: Wed, 18 Aug 2021 22:19:43 +0800 Subject: [PATCH] perf(scheduler): remove unnecessary statement --- .../runtime-core/__tests__/scheduler.spec.ts | 6 +++-- packages/runtime-core/src/renderer.ts | 2 +- packages/runtime-core/src/scheduler.ts | 27 ++++++------------- 3 files changed, 13 insertions(+), 22 deletions(-) diff --git a/packages/runtime-core/__tests__/scheduler.spec.ts b/packages/runtime-core/__tests__/scheduler.spec.ts index 750a2ed19b5..92b243e2ef4 100644 --- a/packages/runtime-core/__tests__/scheduler.spec.ts +++ b/packages/runtime-core/__tests__/scheduler.spec.ts @@ -211,6 +211,8 @@ describe('scheduler', () => { it('preFlushCb inside queueJob', async () => { const calls: string[] = [] + + // job1.allowRecurse is undefined, so it equal to false const job1 = () => { // the only case where a pre-flush cb can be queued inside a job is // when updating the props of a child component. This is handled @@ -218,12 +220,12 @@ describe('scheduler', () => { // cb triggers (#1763) queuePreFlushCb(cb1) queuePreFlushCb(cb2) - flushPreFlushCbs(undefined, job1) + flushPreFlushCbs() calls.push('job1') } const cb1 = () => { calls.push('cb1') - // a cb triggers its parent job, which should be skipped + // with job1.allowRecurse false,it won't be added into queue again queueJob(job1) } const cb2 = () => { diff --git a/packages/runtime-core/src/renderer.ts b/packages/runtime-core/src/renderer.ts index 76848cb52f3..233369c9f70 100644 --- a/packages/runtime-core/src/renderer.ts +++ b/packages/runtime-core/src/renderer.ts @@ -1593,7 +1593,7 @@ function baseCreateRenderer( pauseTracking() // props update may have triggered pre-flush watchers. // flush them before the render update. - flushPreFlushCbs(undefined, instance.update) + flushPreFlushCbs() resetTracking() } diff --git a/packages/runtime-core/src/scheduler.ts b/packages/runtime-core/src/scheduler.ts index 333125d578d..63d45e1b1f2 100644 --- a/packages/runtime-core/src/scheduler.ts +++ b/packages/runtime-core/src/scheduler.ts @@ -50,8 +50,6 @@ let postFlushIndex = 0 const resolvedPromise: Promise = Promise.resolve() let currentFlushPromise: Promise | null = null -let currentPreFlushParentJob: SchedulerJob | null = null - const RECURSION_LIMIT = 100 type CountMap = Map @@ -89,12 +87,11 @@ export function queueJob(job: SchedulerJob) { // allow it recursively trigger itself - it is the user's responsibility to // ensure it doesn't end up in an infinite loop. if ( - (!queue.length || - !queue.includes( - job, - isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex - )) && - job !== currentPreFlushParentJob + !queue.length || + !queue.includes( + job, + isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex + ) ) { if (job.id == null) { queue.push(job) @@ -128,10 +125,7 @@ function queueCb( if (!isArray(cb)) { if ( !activeQueue || - !activeQueue.includes( - cb, - cb.allowRecurse ? index + 1 : index - ) + !activeQueue.includes(cb, cb.allowRecurse ? index + 1 : index) ) { pendingQueue.push(cb) } @@ -152,12 +146,8 @@ export function queuePostFlushCb(cb: SchedulerJobs) { queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex) } -export function flushPreFlushCbs( - seen?: CountMap, - parentJob: SchedulerJob | null = null -) { +export function flushPreFlushCbs(seen?: CountMap) { if (pendingPreFlushCbs.length) { - currentPreFlushParentJob = parentJob activePreFlushCbs = [...new Set(pendingPreFlushCbs)] pendingPreFlushCbs.length = 0 if (__DEV__) { @@ -178,9 +168,8 @@ export function flushPreFlushCbs( } activePreFlushCbs = null preFlushIndex = 0 - currentPreFlushParentJob = null // recursively flush until it drains - flushPreFlushCbs(seen, parentJob) + flushPreFlushCbs(seen) } }