Fabric: Using `ShadowNodeFragment` in `ComponentDescriptor`

Summary:
@public
Now we use same data structure to specify a shape of shadow node as we use in ShadowNode (sub)clases.

Reviewed By: mdvacca

Differential Revision: D8988387

fbshipit-source-id: 475298b2c71ee7ee2b197db009f7b8313b54f5df
This commit is contained in:
Valentin Shergin 2018-08-04 09:30:30 -07:00 committed by Facebook Github Bot
parent 52ed882332
commit 06e62440d3
4 changed files with 40 additions and 66 deletions

View File

@ -45,20 +45,15 @@ public:
* Creates a new `ShadowNode` of a particular type.
*/
virtual SharedShadowNode createShadowNode(
const Tag &tag,
const Tag &rootTag,
const SharedEventEmitter &eventEmitter,
const SharedProps &props
const ShadowNodeFragment &fragment
) const = 0;
/*
* Clones a `ShadowNode` with optionally new `props` and/or `children`.
*/
virtual UnsharedShadowNode cloneShadowNode(
const SharedShadowNode &shadowNode,
const SharedProps &props = nullptr,
const SharedEventEmitter &eventEmitter = nullptr,
const SharedShadowNodeSharedList &children = nullptr
const ShadowNode &sourceShadowNode,
const ShadowNodeFragment &fragment
) const = 0;
/*

View File

@ -45,22 +45,13 @@ public:
}
SharedShadowNode createShadowNode(
const Tag &tag,
const Tag &rootTag,
const SharedEventEmitter &eventEmitter,
const SharedProps &props
const ShadowNodeFragment &fragment
) const override {
assert(std::dynamic_pointer_cast<const ConcreteProps>(props));
assert(std::dynamic_pointer_cast<const ConcreteEventEmitter>(eventEmitter));
assert(std::dynamic_pointer_cast<const ConcreteProps>(fragment.props));
assert(std::dynamic_pointer_cast<const ConcreteEventEmitter>(fragment.eventEmitter));
const auto &shadowNode = std::make_shared<ShadowNodeT>(
ShadowNodeFragment {
.tag = tag,
.rootTag = rootTag,
.props = props,
.eventEmitter = eventEmitter,
.children = ShadowNode::emptySharedShadowNodeSharedList()
},
auto shadowNode = std::make_shared<ShadowNodeT>(
fragment,
getCloneFunction()
);
@ -70,20 +61,12 @@ public:
}
UnsharedShadowNode cloneShadowNode(
const SharedShadowNode &sourceShadowNode,
const SharedProps &props = nullptr,
const SharedEventEmitter &eventEmitter = nullptr,
const SharedShadowNodeSharedList &children = nullptr
const ShadowNode &sourceShadowNode,
const ShadowNodeFragment &fragment
) const override {
assert(std::dynamic_pointer_cast<const ShadowNodeT>(sourceShadowNode));
const auto &shadowNode = std::make_shared<ShadowNodeT>(
*std::static_pointer_cast<const ShadowNodeT>(sourceShadowNode),
ShadowNodeFragment {
.props = props,
.eventEmitter = eventEmitter,
.children = children
}
auto shadowNode = std::make_shared<ShadowNodeT>(
sourceShadowNode,
fragment
);
adopt(shadowNode);
@ -130,7 +113,7 @@ private:
if (!cloneFunction_) {
cloneFunction_ = [this](const SharedShadowNode &shadowNode, const ShadowNodeFragment &fragment) {
assert(std::dynamic_pointer_cast<const ShadowNodeT>(shadowNode));
return this->cloneShadowNode(shadowNode, fragment.props, fragment.eventEmitter, fragment.children);
return this->cloneShadowNode(*shadowNode, fragment);
};
}

View File

@ -21,7 +21,7 @@ TEST(ComponentDescriptorTest, createShadowNode) {
RawProps raw;
raw["nativeID"] = "abc";
SharedProps props = descriptor->cloneProps(nullptr, raw);
SharedShadowNode node = descriptor->createShadowNode(9, 1, descriptor->createEventEmitter(0, 9), props);
SharedShadowNode node = descriptor->createShadowNode(ShadowNodeFragment {.tag = 9, .rootTag = 1, .props = props, .eventEmitter = descriptor->createEventEmitter(0, 9)});
ASSERT_EQ(node->getComponentHandle(), TestShadowNode::Handle());
ASSERT_STREQ(node->getComponentName().c_str(), TestShadowNode::Name().c_str());
@ -37,8 +37,8 @@ TEST(ComponentDescriptorTest, cloneShadowNode) {
RawProps raw;
raw["nativeID"] = "abc";
SharedProps props = descriptor->cloneProps(nullptr, raw);
SharedShadowNode node = descriptor->createShadowNode(9, 1, descriptor->createEventEmitter(0, 9), props);
SharedShadowNode cloned = descriptor->cloneShadowNode(node);
SharedShadowNode node = descriptor->createShadowNode(ShadowNodeFragment {.tag = 9, .rootTag = 1, .props = props, .eventEmitter = descriptor->createEventEmitter(0, 9)});
SharedShadowNode cloned = descriptor->cloneShadowNode(*node, {});
ASSERT_STREQ(cloned->getComponentName().c_str(), "Test");
ASSERT_EQ(cloned->getTag(), 9);
@ -52,9 +52,9 @@ TEST(ComponentDescriptorTest, appendChild) {
RawProps raw;
raw["nativeID"] = "abc";
SharedProps props = descriptor->cloneProps(nullptr, raw);
SharedShadowNode node1 = descriptor->createShadowNode(1, 1, descriptor->createEventEmitter(0, 1), props);
SharedShadowNode node2 = descriptor->createShadowNode(2, 1, descriptor->createEventEmitter(0, 2), props);
SharedShadowNode node3 = descriptor->createShadowNode(3, 1, descriptor->createEventEmitter(0, 3), props);
SharedShadowNode node1 = descriptor->createShadowNode(ShadowNodeFragment {.tag = 1, .rootTag = 1, .props = props, .eventEmitter = descriptor->createEventEmitter(0, 1)});
SharedShadowNode node2 = descriptor->createShadowNode(ShadowNodeFragment {.tag = 2, .rootTag = 1, .props = props, .eventEmitter = descriptor->createEventEmitter(0, 2)});
SharedShadowNode node3 = descriptor->createShadowNode(ShadowNodeFragment {.tag = 3, .rootTag = 1, .props = props, .eventEmitter = descriptor->createEventEmitter(0, 3)});
descriptor->appendChild(node1, node2);
descriptor->appendChild(node1, node3);

View File

@ -142,12 +142,12 @@ SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int
RawProps rawProps = rawPropsFromDynamic(props);
SharedShadowNode shadowNode =
componentDescriptor->createShadowNode(
tag,
rootTag,
componentDescriptor->createEventEmitter(eventTarget, tag),
componentDescriptor->cloneProps(nullptr, rawProps)
);
componentDescriptor->createShadowNode({
.tag = tag,
.rootTag = rootTag,
.eventEmitter = componentDescriptor->createEventEmitter(eventTarget, tag),
.props = componentDescriptor->cloneProps(nullptr, rawProps)
});
isLoggingEnabled && LOG(INFO) << "FabricUIManager::createNode() -> " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
@ -163,12 +163,7 @@ SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode)
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode];
SharedShadowNode clonedShadowNode =
componentDescriptor->cloneShadowNode(
shadowNode,
nullptr,
shadowNode->getEventEmitter(),
nullptr
);
componentDescriptor->cloneShadowNode(*shadowNode, {});
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNode() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
return clonedShadowNode;
@ -181,10 +176,10 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNod
SharedShadowNode clonedShadowNode =
componentDescriptor->cloneShadowNode(
shadowNode,
nullptr,
shadowNode->getEventEmitter(),
ShadowNode::emptySharedShadowNodeSharedList()
*shadowNode,
{
.children = ShadowNode::emptySharedShadowNodeSharedList()
}
);
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
@ -199,10 +194,10 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &
SharedShadowNode clonedShadowNode =
componentDescriptor->cloneShadowNode(
shadowNode,
componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
shadowNode->getEventEmitter(),
nullptr
*shadowNode,
{
.props = componentDescriptor->cloneProps(shadowNode->getProps(), rawProps)
}
);
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});
@ -217,10 +212,11 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedS
SharedShadowNode clonedShadowNode =
componentDescriptor->cloneShadowNode(
shadowNode,
componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
shadowNode->getEventEmitter(),
ShadowNode::emptySharedShadowNodeSharedList()
*shadowNode,
{
.props = componentDescriptor->cloneProps(shadowNode->getProps(), rawProps),
.children = ShadowNode::emptySharedShadowNodeSharedList()
}
);
isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false});