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
This commit is contained in:
Valentin Shergin 2018-04-10 12:45:40 -07:00 committed by Facebook Github Bot
parent 7e84cadc9c
commit dc1a9680de
2 changed files with 79 additions and 30 deletions

View File

@ -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<DebugStringConvertibleItem>("node", newChildNode_->getDebugDescription(options)),
};
case Deletion:
return SharedDebugStringConvertibleList {
std::make_shared<DebugStringConvertibleItem>("node", oldChildNode_->getDebugDescription(options)),
};
case Insertion:
return SharedDebugStringConvertibleList {
std::make_shared<DebugStringConvertibleItem>("parentNode", parentNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("childNode", newChildNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("index", std::to_string(index_))
};
case Deleting:
case Removal:
return SharedDebugStringConvertibleList {
std::make_shared<DebugStringConvertibleItem>("parentNode", parentNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("childNode", oldChildNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("index", std::to_string(index_))
};
case Updating:
case Replacement:
return SharedDebugStringConvertibleList {
std::make_shared<DebugStringConvertibleItem>("parentNode", parentNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("oldChildNode", oldChildNode_->getDebugDescription(options)),

View File

@ -25,6 +25,7 @@ using TreeMutationInstructionList = std::vector<TreeMutationInstruction>;
* 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};