From 5ac87e0d0fe3227ec64bd1fec55aa6bb5918026d Mon Sep 17 00:00:00 2001 From: Sebastian Markbage Date: Mon, 18 Jul 2016 16:22:07 -0700 Subject: [PATCH] When a reconciliation gets down prioritized, reuse children When we downprioritize children we need to remember to reuse the old children in the update side-effect. This whole set up is very unfortunate since we can have children in our active tree that never actually finished rendering. This strategy might be fundamentally flawed, not sure. --- src/renderers/shared/fiber/ReactFiber.js | 6 +++ .../shared/fiber/ReactFiberBeginWork.js | 2 + .../shared/fiber/ReactFiberCommitWork.js | 9 ++++- .../shared/fiber/ReactFiberCompleteWork.js | 11 +++++- .../ReactIncrementalSideEffects-test.js | 39 +++++++++++++++++++ 5 files changed, 64 insertions(+), 3 deletions(-) diff --git a/src/renderers/shared/fiber/ReactFiber.js b/src/renderers/shared/fiber/ReactFiber.js index a813dcc9fa..02bb854c22 100644 --- a/src/renderers/shared/fiber/ReactFiber.js +++ b/src/renderers/shared/fiber/ReactFiber.js @@ -106,6 +106,11 @@ export type Fiber = Instance & { // I'm not really sure so I'll use a flag for now. // TODO: Find another way to infer this flag. hasWorkInProgress: bool, + // Keeps track if we've deprioritized the children of this node so we know to + // reuse the existing children. + // TODO: Find another way to infer this flag or separate children updates from + // property updates so that we simply don't try to update children here. + wasDeprioritized: bool, // Conceptual aliases // workInProgress : Fiber -> alternate The alternate used for reuse happens @@ -146,6 +151,7 @@ var createFiber = function(tag : TypeOfWork, key : null | string) : Fiber { pendingWorkPriority: NoWork, hasWorkInProgress: false, + wasDeprioritized: false, alternate: null, diff --git a/src/renderers/shared/fiber/ReactFiberBeginWork.js b/src/renderers/shared/fiber/ReactFiberBeginWork.js index 86f96db2f7..49888b7337 100644 --- a/src/renderers/shared/fiber/ReactFiberBeginWork.js +++ b/src/renderers/shared/fiber/ReactFiberBeginWork.js @@ -82,6 +82,7 @@ module.exports = function(config : HostConfig) { OffscreenPriority ); workInProgress.pendingWorkPriority = OffscreenPriority; + workInProgress.wasDeprioritized = true; return null; } else { workInProgress.child = reconcileChildFibers( @@ -91,6 +92,7 @@ module.exports = function(config : HostConfig) { priority ); workInProgress.pendingWorkPriority = NoWork; + workInProgress.wasDeprioritized = false; return workInProgress.child; } } diff --git a/src/renderers/shared/fiber/ReactFiberCommitWork.js b/src/renderers/shared/fiber/ReactFiberCommitWork.js index 7792be2102..5a5a6ae74d 100644 --- a/src/renderers/shared/fiber/ReactFiberCommitWork.js +++ b/src/renderers/shared/fiber/ReactFiberCommitWork.js @@ -47,7 +47,14 @@ module.exports = function(config : HostConfig) { throw new Error('This should only be done during updates.'); } // Commit the work prepared earlier. - const child = (finishedWork.child : ?Fiber); + let child; + if (finishedWork.wasDeprioritized) { + // If this was a down priority, we need to preserve the old child in + // the output. + child = finishedWork.alternate ? finishedWork.alternate.child : null; + } else { + child = finishedWork.child; + } const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child; const newProps = finishedWork.memoizedProps; const current = finishedWork.alternate; diff --git a/src/renderers/shared/fiber/ReactFiberCompleteWork.js b/src/renderers/shared/fiber/ReactFiberCompleteWork.js index f75ca6b0f2..dd27062f6f 100644 --- a/src/renderers/shared/fiber/ReactFiberCompleteWork.js +++ b/src/renderers/shared/fiber/ReactFiberCompleteWork.js @@ -140,9 +140,16 @@ module.exports = function(config : HostConfig) { markForPreEffect(workInProgress); return null; case HostComponent: - const child = workInProgress.child; - const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child; + let child; let newProps = workInProgress.pendingProps; + if (workInProgress.wasDeprioritized) { + // If this was a down priority, we need to preserve the old child in + // the output. + child = current ? current.child : null; + } else { + child = workInProgress.child; + } + const children = (child && !child.sibling) ? (child.output : ?Fiber | I) : child; if (current && workInProgress.stateNode != null) { // If we have an alternate, that means this is an update and we need to // schedule a side-effect to do the updates. diff --git a/src/renderers/shared/fiber/__tests__/ReactIncrementalSideEffects-test.js b/src/renderers/shared/fiber/__tests__/ReactIncrementalSideEffects-test.js index d4974fc3e0..f5ffa84a45 100644 --- a/src/renderers/shared/fiber/__tests__/ReactIncrementalSideEffects-test.js +++ b/src/renderers/shared/fiber/__tests__/ReactIncrementalSideEffects-test.js @@ -89,6 +89,45 @@ describe('ReactIncremental', function() { }); + it('preserves a previously rendered node when deprioritized', function() { + + function Middle(props) { + return ; + } + + function Foo(props) { + return ( +
+ +
+ ); + } + + ReactNoop.render(); + ReactNoop.flush(); + + expect(ReactNoop.root.children).toEqual([ + div(div(span('foo'))), + ]); + + ReactNoop.render(); + ReactNoop.flushLowPri(20); + + expect(ReactNoop.root.children).toEqual([ + div(div(span('foo'))), + ]); + + ReactNoop.flush(); + + expect(ReactNoop.root.children).toEqual([ + div(div(span('bar'))), + ]); + + }); + + it('updates a child even though the old props is empty', function() { function Foo(props) { return (