Skip to content

Commit

Permalink
Fabric: Introducing TreeMutationInstruction
Browse files Browse the repository at this point in the history
Summary: The Great Diffing algorithm is coming.

Reviewed By: fkgozali

Differential Revision: D7376528

fbshipit-source-id: bdfef69551980136cfd1717a11ae376d5eef126b
  • Loading branch information
shergin authored and facebook-github-bot committed Mar 26, 2018
1 parent 53dfbcc commit 6f17e3b
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 0 deletions.
150 changes: 150 additions & 0 deletions ReactCommon/fabric/uimanager/TreeMutationInstruction.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include "TreeMutationInstruction.h"

#include <fabric/debug/DebugStringConvertibleItem.h>

namespace facebook {
namespace react {

const TreeMutationInstruction TreeMutationInstruction::Insert(
SharedShadowNode parentNode,
SharedShadowNode childNode,
int index
) {
assert(parentNode);
assert(childNode);
assert(index != -1);

return TreeMutationInstruction(
Inserting,
parentNode,
nullptr,
childNode,
index
);
}

const TreeMutationInstruction TreeMutationInstruction::Delete(
SharedShadowNode parentNode,
SharedShadowNode childNode,
int index
) {
assert(parentNode);
assert(childNode);
assert(index != -1);

return TreeMutationInstruction(
Deleting,
parentNode,
childNode,
nullptr,
index
);
}

const TreeMutationInstruction TreeMutationInstruction::Update(
SharedShadowNode parentNode,
SharedShadowNode oldChildNode,
SharedShadowNode newChildNode,
int index
) {
assert(parentNode);
assert(oldChildNode);
assert(newChildNode);
assert(index != -1);

return TreeMutationInstruction(
Updating,
parentNode,
oldChildNode,
newChildNode,
index
);
}

TreeMutationInstruction::TreeMutationInstruction(
Type type,
SharedShadowNode parentNode,
SharedShadowNode oldChildNode,
SharedShadowNode newChildNode,
int index
):
type_(type),
parentNode_(parentNode),
oldChildNode_(oldChildNode),
newChildNode_(newChildNode),
index_(index) {};

#pragma mark - Getters

TreeMutationInstruction::Type TreeMutationInstruction::getType() const {
return type_;
}

SharedShadowNode TreeMutationInstruction::getParentNode() const {
assert(parentNode_);
return parentNode_;
}

SharedShadowNode TreeMutationInstruction::getOldChildNode() const {
assert(oldChildNode_);
return oldChildNode_;
}

SharedShadowNode TreeMutationInstruction::getNewChildNode() const {
assert(newChildNode_);
return newChildNode_;
}

int TreeMutationInstruction::getIndex() const {
assert(index_ != -1);
return index_;
}

#pragma mark - DebugStringConvertible

std::string TreeMutationInstruction::getDebugName() const {
switch (type_) {
case Inserting:
return "Insert";
case Deleting:
return "Delete";
case Updating:
return "Update";
}
};

SharedDebugStringConvertibleList TreeMutationInstruction::getDebugProps() const {
DebugStringConvertibleOptions options = {.maximumDepth = 1, .format = false};

switch (type_) {
case Inserting:
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:
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:
return SharedDebugStringConvertibleList {
std::make_shared<DebugStringConvertibleItem>("parentNode", parentNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("oldChildNode", oldChildNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("newChildNode", newChildNode_->getDebugDescription(options)),
std::make_shared<DebugStringConvertibleItem>("index", std::to_string(index_))
};
}
}

} // namespace react
} // namespace facebook
109 changes: 109 additions & 0 deletions ReactCommon/fabric/uimanager/TreeMutationInstruction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#pragma once

#include <vector>

#include <fabric/core/ShadowNode.h>
#include <fabric/debug/DebugStringConvertible.h>

namespace facebook {
namespace react {

class TreeMutationInstruction;

using TreeMutationInstructionList = std::vector<TreeMutationInstruction>;

/*
* Describes single native views tree mutation instruction which may contain
* pointers to an old shadow node, a new shadow node, a parent shadow node and
* final index of inserted or updated node.
* The relationship between native view instances and shadow node instances is
* defined by `tag` value.
*/
class TreeMutationInstruction:
public DebugStringConvertible {
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.
*/
static const TreeMutationInstruction Insert(
SharedShadowNode parentNode,
SharedShadowNode childNode,
int index
);

/*
* 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.
*/
static const TreeMutationInstruction Delete(
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).
*/
static const TreeMutationInstruction Update(
SharedShadowNode parentNode,
SharedShadowNode oldChildNode,
SharedShadowNode newChildNode,
int index
);

#pragma mark - Type

enum Type {
Inserting,
Deleting,
Updating
};

#pragma mark - Getters

Type getType() const;
SharedShadowNode getParentNode() const;
SharedShadowNode getOldChildNode() const;
SharedShadowNode getNewChildNode() const;
int getIndex() const;

#pragma mark - DebugStringConvertible

std::string getDebugName() const override;
SharedDebugStringConvertibleList getDebugProps() const override;

private:
TreeMutationInstruction(
Type type,
SharedShadowNode parentNode,
SharedShadowNode oldChildNode,
SharedShadowNode newChildNode,
int index
);

Type type_ {Inserting};
SharedShadowNode parentNode_ {nullptr};
SharedShadowNode oldChildNode_ {nullptr};
SharedShadowNode newChildNode_ {nullptr};
int index_ {-1};
};

} // namespace react
} // namespace facebook

0 comments on commit 6f17e3b

Please sign in to comment.