From 4b8e1641b77d0a5443403b8a2a8fa141c220c1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Markb=C3=A5ge?= Date: Thu, 21 Mar 2019 13:20:52 -0700 Subject: [PATCH] Fork performWork instead of using boolean flag (#15169) I inline it into performAsyncWork instead. Code that was only relevant to the async callback had leaked into the performWork call which is an indication that this was a bad abstraction and therefore the wrong place to DRY. By inlining I also discovered that minExpirationTime is actually irrelevant in the yieldy case so we can clean that up. --- .../src/ReactFiberScheduler.old.js | 71 ++++++++++++------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/packages/react-reconciler/src/ReactFiberScheduler.old.js b/packages/react-reconciler/src/ReactFiberScheduler.old.js index e24e2ad714803..37983fac18158 100644 --- a/packages/react-reconciler/src/ReactFiberScheduler.old.js +++ b/packages/react-reconciler/src/ReactFiberScheduler.old.js @@ -2234,23 +2234,18 @@ function performAsyncWork(didTimeout) { } while (root !== firstScheduledRoot); } } - let isYieldy = true; - if (disableYielding) { - isYieldy = false; - } - performWork(NoWork, isYieldy); -} - -function performSyncWork() { - performWork(Sync, false); -} -function performWork(minExpirationTime: ExpirationTime, isYieldy: boolean) { // Keep working on roots until there's no more work, or until there's a higher // priority event. findHighestPriorityRoot(); - if (isYieldy) { + if (disableYielding) { + // Just do it all + while (nextFlushedRoot !== null && nextFlushedExpirationTime !== NoWork) { + performWorkOnRoot(nextFlushedRoot, nextFlushedExpirationTime, false); + findHighestPriorityRoot(); + } + } else { recomputeCurrentRendererTime(); currentSchedulerTime = currentRendererTime; @@ -2263,7 +2258,6 @@ function performWork(minExpirationTime: ExpirationTime, isYieldy: boolean) { while ( nextFlushedRoot !== null && nextFlushedExpirationTime !== NoWork && - minExpirationTime <= nextFlushedExpirationTime && !(shouldYield() && currentRendererTime > nextFlushedExpirationTime) ) { performWorkOnRoot( @@ -2275,25 +2269,48 @@ function performWork(minExpirationTime: ExpirationTime, isYieldy: boolean) { recomputeCurrentRendererTime(); currentSchedulerTime = currentRendererTime; } - } else { - while ( - nextFlushedRoot !== null && - nextFlushedExpirationTime !== NoWork && - minExpirationTime <= nextFlushedExpirationTime - ) { - performWorkOnRoot(nextFlushedRoot, nextFlushedExpirationTime, false); - findHighestPriorityRoot(); - } } // We're done flushing work. Either we ran out of time in this callback, // or there's no more work left with sufficient priority. // If we're inside a callback, set this to false since we just completed it. - if (isYieldy) { - callbackExpirationTime = NoWork; - callbackID = null; + callbackExpirationTime = NoWork; + callbackID = null; + + // If there's work left over, schedule a new callback. + if (nextFlushedExpirationTime !== NoWork) { + scheduleCallbackWithExpirationTime( + ((nextFlushedRoot: any): FiberRoot), + nextFlushedExpirationTime, + ); + } + + // Clean-up. + finishRendering(); +} + +function performSyncWork() { + performWork(Sync); +} + +function performWork(minExpirationTime: ExpirationTime) { + // Keep working on roots until there's no more work, or until there's a higher + // priority event. + findHighestPriorityRoot(); + + while ( + nextFlushedRoot !== null && + nextFlushedExpirationTime !== NoWork && + minExpirationTime <= nextFlushedExpirationTime + ) { + performWorkOnRoot(nextFlushedRoot, nextFlushedExpirationTime, false); + findHighestPriorityRoot(); } + + // We're done flushing work. Either we ran out of time in this callback, + // or there's no more work left with sufficient priority. + // If there's work left over, schedule a new callback. if (nextFlushedExpirationTime !== NoWork) { scheduleCallbackWithExpirationTime( @@ -2547,7 +2564,7 @@ function interactiveUpdates( lowestPriorityPendingInteractiveExpirationTime !== NoWork ) { // Synchronously flush pending interactive updates. - performWork(lowestPriorityPendingInteractiveExpirationTime, false); + performWork(lowestPriorityPendingInteractiveExpirationTime); lowestPriorityPendingInteractiveExpirationTime = NoWork; } const previousIsBatchingInteractiveUpdates = isBatchingInteractiveUpdates; @@ -2571,7 +2588,7 @@ function flushInteractiveUpdates() { lowestPriorityPendingInteractiveExpirationTime !== NoWork ) { // Synchronously flush pending interactive updates. - performWork(lowestPriorityPendingInteractiveExpirationTime, false); + performWork(lowestPriorityPendingInteractiveExpirationTime); lowestPriorityPendingInteractiveExpirationTime = NoWork; } }