From dc1a9680de59b68f6efc5d553bf5a8b8382c2c1d Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 10 Apr 2018 12:45:40 -0700 Subject: [PATCH] Fabric: Refined TreeMutationInstruction class Summary: Two additional types of instructions were added and now all of them have explicitly clear semantic. Reviewed By: fkgozali Differential Revision: D7467798 fbshipit-source-id: 83c0e774d56975be504aa3fe892035f5f724f809 --- .../uimanager/TreeMutationInstruction.cpp | 66 +++++++++++++++---- .../uimanager/TreeMutationInstruction.h | 43 +++++++----- 2 files changed, 79 insertions(+), 30 deletions(-) diff --git a/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp b/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp index da60fb83cf0..42882ee6cfc 100644 --- a/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp +++ b/ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp @@ -12,6 +12,34 @@ namespace facebook { namespace react { +const TreeMutationInstruction TreeMutationInstruction::Create( + SharedShadowNode node +) { + assert(node); + + return TreeMutationInstruction( + Creation, + nullptr, + nullptr, + node, + -1 + ); +} + +const TreeMutationInstruction TreeMutationInstruction::Delete( + SharedShadowNode node +) { + assert(node); + + return TreeMutationInstruction( + Deletion, + nullptr, + node, + nullptr, + -1 + ); +} + const TreeMutationInstruction TreeMutationInstruction::Insert( SharedShadowNode parentNode, SharedShadowNode childNode, @@ -22,7 +50,7 @@ const TreeMutationInstruction TreeMutationInstruction::Insert( assert(index != -1); return TreeMutationInstruction( - Inserting, + Insertion, parentNode, nullptr, childNode, @@ -30,7 +58,7 @@ const TreeMutationInstruction TreeMutationInstruction::Insert( ); } -const TreeMutationInstruction TreeMutationInstruction::Delete( +const TreeMutationInstruction TreeMutationInstruction::Remove( SharedShadowNode parentNode, SharedShadowNode childNode, int index @@ -40,7 +68,7 @@ const TreeMutationInstruction TreeMutationInstruction::Delete( assert(index != -1); return TreeMutationInstruction( - Deleting, + Removal, parentNode, childNode, nullptr, @@ -48,7 +76,7 @@ const TreeMutationInstruction TreeMutationInstruction::Delete( ); } -const TreeMutationInstruction TreeMutationInstruction::Update( +const TreeMutationInstruction TreeMutationInstruction::Replace( SharedShadowNode parentNode, SharedShadowNode oldChildNode, SharedShadowNode newChildNode, @@ -60,7 +88,7 @@ const TreeMutationInstruction TreeMutationInstruction::Update( assert(index != -1); return TreeMutationInstruction( - Updating, + Replacement, parentNode, oldChildNode, newChildNode, @@ -111,12 +139,16 @@ int TreeMutationInstruction::getIndex() const { std::string TreeMutationInstruction::getDebugName() const { switch (type_) { - case Inserting: - return "Insert"; - case Deleting: + case Creation: + return "Create"; + case Deletion: return "Delete"; - case Updating: - return "Update"; + case Insertion: + return "Insert"; + case Removal: + return "Remove"; + case Replacement: + return "Replace"; } }; @@ -124,19 +156,27 @@ SharedDebugStringConvertibleList TreeMutationInstruction::getDebugProps() const DebugStringConvertibleOptions options = {.maximumDepth = 1, .format = false}; switch (type_) { - case Inserting: + case Creation: + return SharedDebugStringConvertibleList { + std::make_shared("node", newChildNode_->getDebugDescription(options)), + }; + case Deletion: + return SharedDebugStringConvertibleList { + std::make_shared("node", oldChildNode_->getDebugDescription(options)), + }; + case Insertion: return SharedDebugStringConvertibleList { std::make_shared("parentNode", parentNode_->getDebugDescription(options)), std::make_shared("childNode", newChildNode_->getDebugDescription(options)), std::make_shared("index", std::to_string(index_)) }; - case Deleting: + case Removal: return SharedDebugStringConvertibleList { std::make_shared("parentNode", parentNode_->getDebugDescription(options)), std::make_shared("childNode", oldChildNode_->getDebugDescription(options)), std::make_shared("index", std::to_string(index_)) }; - case Updating: + case Replacement: return SharedDebugStringConvertibleList { std::make_shared("parentNode", parentNode_->getDebugDescription(options)), std::make_shared("oldChildNode", oldChildNode_->getDebugDescription(options)), diff --git a/ReactCommon/fabric/uimanager/TreeMutationInstruction.h b/ReactCommon/fabric/uimanager/TreeMutationInstruction.h index f0e09c17d35..920cd27f87c 100644 --- a/ReactCommon/fabric/uimanager/TreeMutationInstruction.h +++ b/ReactCommon/fabric/uimanager/TreeMutationInstruction.h @@ -25,6 +25,7 @@ using TreeMutationInstructionList = std::vector; * final index of inserted or updated node. * The relationship between native view instances and shadow node instances is * defined by `tag` value. + * Use static methods to instantiate mutation instructions of different types. */ class TreeMutationInstruction: public DebugStringConvertible { @@ -33,10 +34,21 @@ public: #pragma mark - Designated Initializers /* - * Creates and returns an *Insert* instruction with following semantic: - * 1. Create a native view for the shadow node if needed; - * 2. Unmount the native view from a previous superview if needed; - * 3. Mount the native view to the new superview. + * Creates and returns an *Creation* instruction. + */ + static const TreeMutationInstruction Create( + SharedShadowNode node + ); + + /* + * Creates and returns an *Deletion* instruction. + */ + static const TreeMutationInstruction Delete( + SharedShadowNode node + ); + + /* + * Creates and returns an *Insertion* instruction. */ static const TreeMutationInstruction Insert( SharedShadowNode parentNode, @@ -45,23 +57,18 @@ public: ); /* - * Creates and returns a *Delete* instruction with following semantic: - * 1. Unmount the native view from a previous superview if needed; - * 2. Destroy (or return to a recycle pool) the native view. + * Creates and returns a *Removal* instruction. */ - static const TreeMutationInstruction Delete( + static const TreeMutationInstruction Remove( SharedShadowNode parentNode, SharedShadowNode childNode, int index ); /* - * Creates and returns an *Update* instruction with following semantic: - * 1. Update the presentation of a native view based on the new shadow node; - * 2. The exact set of changes are not specified but might contain - * new props and/or new layout (or might be empty). + * Creates and returns an *Replacement* instruction. */ - static const TreeMutationInstruction Update( + static const TreeMutationInstruction Replace( SharedShadowNode parentNode, SharedShadowNode oldChildNode, SharedShadowNode newChildNode, @@ -71,9 +78,11 @@ public: #pragma mark - Type enum Type { - Inserting, - Deleting, - Updating + Creation, + Deletion, + Insertion, + Removal, + Replacement }; #pragma mark - Getters @@ -98,7 +107,7 @@ private: int index ); - Type type_ {Inserting}; + Type type_ {Creation}; SharedShadowNode parentNode_ {nullptr}; SharedShadowNode oldChildNode_ {nullptr}; SharedShadowNode newChildNode_ {nullptr};