Valentin Shergin 840638c441 Introducing ComponentDescriptor
Summary:
Abstract class and default template implementation.
`ComponentDescriptor`s define basic logic of managing (creation, cloning, applying props) ShadowNode of particular type.

Reviewed By: fkgozali

Differential Revision: D7230671

fbshipit-source-id: c32636f4db0716b55a1637f61c4f1872fc52cea7
2018-03-18 19:17:39 -07:00

137 lines
3.7 KiB
C++

/**
* 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 "ShadowNode.h"
#include <fabric/debug/DebugStringConvertible.h>
#include <fabric/debug/DebugStringConvertibleItem.h>
namespace facebook {
namespace react {
SharedShadowNodeSharedList ShadowNode::emptySharedShadowNodeSharedList() {
static const auto emptySharedShadowNodeSharedList = std::make_shared<SharedShadowNodeList>();
return emptySharedShadowNodeSharedList;
}
#pragma mark - Constructors
ShadowNode::ShadowNode(
Tag tag,
Tag rootTag,
InstanceHandle instanceHandle,
SharedProps props,
SharedShadowNodeSharedList children
):
tag_(tag),
rootTag_(rootTag),
instanceHandle_(instanceHandle),
props_(props),
children_(children) {}
ShadowNode::ShadowNode(
SharedShadowNode shadowNode,
SharedProps props,
SharedShadowNodeSharedList children
):
tag_(shadowNode->tag_),
instanceHandle_(shadowNode->instanceHandle_),
props_(props ? props : shadowNode->props_),
children_(children ? children : shadowNode->children_) {}
#pragma mark - Getters
SharedShadowNodeSharedList ShadowNode::getChildren() const {
return children_;
}
SharedProps ShadowNode::getProps() const {
return props_;
}
Tag ShadowNode::getTag() const {
return tag_;
}
#pragma mark - Mutating Methods
void ShadowNode::sealRecursive() const {
if (getSealed()) {
return;
}
seal();
props_->seal();
for (auto child : *children_) {
child->sealRecursive();
}
}
void ShadowNode::appendChild(const SharedShadowNode &child) {
ensureUnsealed();
// We cannot mutate `children_` in place here because it is a *shared*
// data structure which means other `ShadowNodes` might refer to its old value.
// So, we have to clone this and only then mutate.
auto nonConstChildrenCopy = SharedShadowNodeList(*children_);
nonConstChildrenCopy.push_back(child);
children_ = std::make_shared<const SharedShadowNodeList>(nonConstChildrenCopy);
}
void ShadowNode::replaceChild(const SharedShadowNode &oldChild, const SharedShadowNode &newChild) {
ensureUnsealed();
// We cannot mutate `children_` in place here because it is a *shared*
// data structure which means other `ShadowNodes` might refer to its old value.
// So, we have to clone this and only then mutate.
auto nonConstChildrenCopy = SharedShadowNodeList(*children_);
std::replace(nonConstChildrenCopy.begin(), nonConstChildrenCopy.end(), oldChild, newChild);
children_ = std::make_shared<const SharedShadowNodeList>(nonConstChildrenCopy);
}
#pragma mark - DebugStringConvertible
std::string ShadowNode::getDebugName() const {
return getComponentName();
}
std::string ShadowNode::getDebugValue() const {
return getSealed() ? "sealed" : "";
}
SharedDebugStringConvertibleList ShadowNode::getDebugChildren() const {
SharedDebugStringConvertibleList debugChildren = {};
for (auto child : *children_) {
auto debugChild = std::dynamic_pointer_cast<const DebugStringConvertible>(child);
if (debugChild) {
debugChildren.push_back(debugChild);
}
}
return debugChildren;
}
SharedDebugStringConvertibleList ShadowNode::getDebugProps() const {
SharedDebugStringConvertibleList list = {};
list.push_back(std::make_shared<DebugStringConvertibleItem>("tag", std::to_string(tag_)));
if (instanceHandle_) {
list.push_back(std::make_shared<DebugStringConvertibleItem>("handle", std::to_string((size_t)instanceHandle_)));
}
SharedDebugStringConvertibleList propsList = props_->getDebugProps();
std::move(propsList.begin(), propsList.end(), std::back_inserter(list));
return list;
}
} // namespace react
} // namespace facebook