From 9fca81213967210ea3f74daab7a0f64a454bd3b6 Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Mon, 18 Jul 2016 18:11:28 -0700 Subject: [PATCH] Fix NoWork bug Thanks @acdlite. Add comment about future unit test coverage. This was actually hiding the fact that we are only able to reuse existing work if it was marked as completely finished which it won't be if we reuse its pending priority. However, we should be able to bail out even if there is work remaining in a subtree. --- .../shared/fiber/ReactFiberBeginWork.js | 22 ++++++++++++++----- .../shared/fiber/ReactFiberPendingWork.js | 13 +++++++++-- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index 49888b7337..984c09de98 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -169,7 +169,7 @@ module.exports = function(config : HostConfig) { // it until the next tick. workInProgress.child = current.child; reuseChildren(workInProgress, workInProgress.child); - if (workInProgress.pendingWorkPriority <= priorityLevel) { + if (workInProgress.pendingWorkPriority !== NoWork && workInProgress.pendingWorkPriority <= priorityLevel) { // TODO: This passes the current node and reads the priority level and // pending props from that. We want it to read our priority level and // pending props from the work in progress. Needs restructuring. @@ -184,15 +184,25 @@ module.exports = function(config : HostConfig) { } if (!workInProgress.hasWorkInProgress && - workInProgress.pendingProps === workInProgress.memoizedProps && - workInProgress.pendingWorkPriority === NoWork) { + workInProgress.pendingProps === workInProgress.memoizedProps) { // If we started this work before, and finished it, or if we're in a // ping-pong update scenario, this version could already be what we're // looking for. In that case, we should be able to just bail out. + const priorityLevel = workInProgress.pendingWorkPriority; workInProgress.pendingProps = null; - // TODO: We should be able to bail out if there is remaining work at a lower - // priority too. However, I don't know if that is safe or even better since - // the other tree could've potentially finished that work. + workInProgress.pendingWorkPriority = NoWork; + if (workInProgress.child) { + // If we bail out but still has work with the current priority in this + // subtree, we need to go find it right now. If we don't, we won't flush + // it until the next tick. + reuseChildren(workInProgress, workInProgress.child); + if (workInProgress.pendingWorkPriority !== NoWork && workInProgress.pendingWorkPriority <= priorityLevel) { + // TODO: This passes the current node and reads the priority level and + // pending props from that. We want it to read our priority level and + // pending props from the work in progress. Needs restructuring. + return findNextUnitOfWorkAtPriority(workInProgress, priorityLevel); + } + } return null; } diff --git a/src/renderers/shared/fiber/ReactFiberPendingWork.js b/src/renderers/shared/fiber/ReactFiberPendingWork.js index 8505473de3..bf837c0426 100644 --- a/src/renderers/shared/fiber/ReactFiberPendingWork.js +++ b/src/renderers/shared/fiber/ReactFiberPendingWork.js @@ -22,6 +22,7 @@ var { } = require('ReactPriorityLevel'); function cloneSiblings(current : Fiber, workInProgress : Fiber, returnFiber : Fiber) { + workInProgress.return = returnFiber; while (current.sibling) { current = current.sibling; workInProgress = workInProgress.sibling = cloneFiber( @@ -49,6 +50,7 @@ exports.findNextUnitOfWorkAtPriority = function(currentRoot : Fiber, priorityLev workInProgress.pendingProps = current.pendingProps; return workInProgress; } + // If we have a child let's see if any of our children has work to do. // Only bother doing this at all if the current priority level matches // because it is the highest priority for the whole subtree. @@ -62,8 +64,15 @@ exports.findNextUnitOfWorkAtPriority = function(currentRoot : Fiber, priorityLev throw new Error('Should have wip now'); } workInProgress.pendingWorkPriority = current.pendingWorkPriority; - workInProgress.child = cloneFiber(currentChild, NoWork); - workInProgress.child.return = workInProgress; + // TODO: The below priority used to be set to NoWork which would've + // dropped work. This is currently unobservable but will become + // observable when the first sibling has lower priority work remaining + // than the next sibling. At that point we should add tests that catches + // this. + workInProgress.child = cloneFiber( + currentChild, + currentChild.pendingWorkPriority + ); cloneSiblings(currentChild, workInProgress.child, workInProgress); current = current.child; continue;