From 57d69772b767e76c3306e7328c5a4bd7769864bd Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 15 May 2018 23:32:34 -0700 Subject: [PATCH] Fabric: Improved signature of `calculateMutationInstructions` Summary: Apparently, `calculateMutationInstructions` must also produce mutation instructions for root node as well. To make it possible we have to change the signature of the function and weak some restrictions in TreeMutationInstruction. Reviewed By: fkgozali Differential Revision: D7958248 fbshipit-source-id: 4109a6bce3a77f7eb89157201fd0e80f98487dbd --- .../fabric/uimanager/Differentiator.cpp | 30 ++++++++++++++++++- ReactCommon/fabric/uimanager/Differentiator.h | 5 ++-- ReactCommon/fabric/uimanager/ShadowTree.cpp | 3 +- .../uimanager/TreeMutationInstruction.cpp | 2 -- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/ReactCommon/fabric/uimanager/Differentiator.cpp b/ReactCommon/fabric/uimanager/Differentiator.cpp index 87f92990450..a689d51d9db 100644 --- a/ReactCommon/fabric/uimanager/Differentiator.cpp +++ b/ReactCommon/fabric/uimanager/Differentiator.cpp @@ -5,7 +5,7 @@ namespace facebook { namespace react { -void calculateMutationInstructions( +static void calculateMutationInstructions( TreeMutationInstructionList &instructions, SharedShadowNode parentNode, SharedShadowNodeSharedList oldChildNodes, @@ -154,5 +154,33 @@ void calculateMutationInstructions( instructions.insert(instructions.end(), downwardInstructions.begin(), downwardInstructions.end()); } + +void calculateMutationInstructions( + TreeMutationInstructionList &instructions, + SharedShadowNode oldRootShadowNode, + SharedShadowNode newRootShadowNode +) { + // Root shadow nodes must have same tag. + assert(oldRootShadowNode->getTag() == newRootShadowNode->getTag()); + + if (*oldRootShadowNode != *newRootShadowNode) { + instructions.push_back( + TreeMutationInstruction::Replace( + nullptr, + oldRootShadowNode, + newRootShadowNode, + -1 + ) + ); + } + + calculateMutationInstructions( + instructions, + oldRootShadowNode, + oldRootShadowNode->getChildren(), + newRootShadowNode->getChildren() + ); +} + } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/uimanager/Differentiator.h b/ReactCommon/fabric/uimanager/Differentiator.h index 7eb9fa332ff..c1d7a8b1d4e 100644 --- a/ReactCommon/fabric/uimanager/Differentiator.h +++ b/ReactCommon/fabric/uimanager/Differentiator.h @@ -15,9 +15,8 @@ namespace react { */ void calculateMutationInstructions( TreeMutationInstructionList &instructions, - SharedShadowNode parentNode, - SharedShadowNodeSharedList oldChildNodes, - SharedShadowNodeSharedList newChildNodes + SharedShadowNode oldNode, + SharedShadowNode newNode ); } // namespace react diff --git a/ReactCommon/fabric/uimanager/ShadowTree.cpp b/ReactCommon/fabric/uimanager/ShadowTree.cpp index 32a7870d96a..30b24643864 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.cpp +++ b/ReactCommon/fabric/uimanager/ShadowTree.cpp @@ -69,8 +69,7 @@ void ShadowTree::complete(UnsharedRootShadowNode newRootShadowNode) { calculateMutationInstructions( instructions, oldRootShadowNode, - oldRootShadowNode->ShadowNode::getChildren(), - newRootShadowNode->ShadowNode::getChildren() + newRootShadowNode ); if (commit(newRootShadowNode)) { diff --git a/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp b/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp index 42882ee6cfc..fd728dd7a41 100644 --- a/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp +++ b/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp @@ -82,10 +82,8 @@ const TreeMutationInstruction TreeMutationInstruction::Replace( SharedShadowNode newChildNode, int index ) { - assert(parentNode); assert(oldChildNode); assert(newChildNode); - assert(index != -1); return TreeMutationInstruction( Replacement,