Create ShadowNodeFamily inside ComponentDescriptor

Summary:
Changelog: [internal]

1. Creates `ShadowNodeFamily` inside `ComponentDescriptor`.
2. As a side effect of this, we no longer need `ComponentDescriptor::createEventEmitter` so it is removed.

This is a step in order to merge `StateCoordinator` into `ShadowNodeFamily` and use it as target for state updates.

Reviewed By: shergin

Differential Revision: D19514906

fbshipit-source-id: 04ad3c621886be56925acd76f9b35a09d8c5e15a
This commit is contained in:
Samuel Susla
2020-01-28 09:32:53 -08:00
committed by Facebook Github Bot
parent 7f79b46bad
commit 142c66f341
7 changed files with 65 additions and 50 deletions
@@ -103,14 +103,6 @@ class ComponentDescriptor {
const SharedProps &props,
const RawProps &rawProps) const = 0;
/*
* Creates a new `EventEmitter` object compatible with particular type of
* shadow nodes.
*/
virtual SharedEventEmitter createEventEmitter(
SharedEventTarget eventTarget,
const Tag &tag) const = 0;
/*
* Create an initial State object that represents (and contains) an initial
* State's data which can be constructed based on initial Props.
@@ -127,6 +119,13 @@ class ComponentDescriptor {
const State::Shared &previousState,
const StateData::Shared &data) const = 0;
/*
* Creates a shadow node family for particular node.
*/
virtual ShadowNodeFamily::Shared createFamily(
ShadowNodeFamilyFragment const &fragment,
SharedEventTarget eventTarget) const = 0;
protected:
EventDispatcher::Weak eventDispatcher_;
ContextContainer::Shared contextContainer_;
@@ -118,13 +118,6 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
return ShadowNodeT::Props(rawProps, props);
};
virtual SharedEventEmitter createEventEmitter(
SharedEventTarget eventTarget,
const Tag &tag) const override {
return std::make_shared<ConcreteEventEmitter>(
std::move(eventTarget), tag, eventDispatcher_);
}
virtual State::Shared createInitialState(
ShadowNodeFragment const &fragment,
SurfaceId const surfaceId) const override {
@@ -157,6 +150,17 @@ class ConcreteComponentDescriptor : public ComponentDescriptor {
*std::static_pointer_cast<const ConcreteState>(previousState));
}
virtual ShadowNodeFamily::Shared createFamily(
ShadowNodeFamilyFragment const &fragment,
SharedEventTarget eventTarget) const override {
auto eventEmitter = std::make_shared<ConcreteEventEmitter const>(
std::move(eventTarget), fragment.tag, eventDispatcher_);
return std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{
fragment.tag, fragment.surfaceId, eventEmitter},
*this);
}
protected:
virtual void adopt(UnsharedShadowNode shadowNode) const {
// Default implementation does nothing.
@@ -23,9 +23,14 @@ TEST(ComponentDescriptorTest, createShadowNode) {
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
SharedProps props = descriptor->cloneProps(nullptr, raw);
auto family = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{9, 1, descriptor->createEventEmitter(0, 9)},
*descriptor);
auto family = descriptor->createFamily(
ShadowNodeFamilyFragment{
/* .tag = */ 9,
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
nullptr);
SharedShadowNode node = descriptor->createShadowNode(
ShadowNodeFragment{
@@ -49,9 +54,13 @@ TEST(ComponentDescriptorTest, cloneShadowNode) {
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
SharedProps props = descriptor->cloneProps(nullptr, raw);
auto family = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{9, 1, descriptor->createEventEmitter(0, 9)},
*descriptor);
auto family = descriptor->createFamily(
ShadowNodeFamilyFragment{
/* .tag = */ 9,
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
nullptr);
SharedShadowNode node = descriptor->createShadowNode(
ShadowNodeFragment{
/* .props = */ props,
@@ -73,25 +82,37 @@ TEST(ComponentDescriptorTest, appendChild) {
const auto &raw = RawProps(folly::dynamic::object("nativeID", "abc"));
SharedProps props = descriptor->cloneProps(nullptr, raw);
auto family1 = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{1, 1, descriptor->createEventEmitter(0, 9)},
*descriptor);
auto family1 = descriptor->createFamily(
ShadowNodeFamilyFragment{
/* .tag = */ 1,
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
nullptr);
SharedShadowNode node1 = descriptor->createShadowNode(
ShadowNodeFragment{
/* .props = */ props,
},
family1);
auto family2 = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{2, 1, descriptor->createEventEmitter(0, 2)},
*descriptor);
auto family2 = descriptor->createFamily(
ShadowNodeFamilyFragment{
/* .tag = */ 2,
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
nullptr);
SharedShadowNode node2 = descriptor->createShadowNode(
ShadowNodeFragment{
/* .props = */ props,
},
family2);
auto family3 = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{3, 1, descriptor->createEventEmitter(0, 3)},
*descriptor);
auto family3 = descriptor->createFamily(
ShadowNodeFamilyFragment{
/* .tag = */ 3,
/* .surfaceId = */ 1,
/* .eventEmitter = */ nullptr,
},
nullptr);
SharedShadowNode node3 = descriptor->createShadowNode(
ShadowNodeFragment{
/* .props = */ props,
@@ -25,13 +25,10 @@ ShadowNode::Shared ComponentBuilder::build(
children.push_back(build(childFragment));
}
auto eventEmitter =
componentDescriptor.createEventEmitter(nullptr, elementFragment.tag);
auto family = std::make_shared<ShadowNodeFamily>(
auto family = componentDescriptor.createFamily(
ShadowNodeFamilyFragment{
elementFragment.tag, elementFragment.surfaceId, eventEmitter},
componentDescriptor);
elementFragment.tag, elementFragment.surfaceId, nullptr},
nullptr);
auto shadowNode = componentDescriptor.createShadowNode(
ShadowNodeFragment{
+2 -2
View File
@@ -102,9 +102,9 @@ ShadowTree::ShadowTree(
const auto props = std::make_shared<const RootProps>(
*RootShadowNode::defaultSharedProps(), layoutConstraints, layoutContext);
auto family = std::make_shared<ShadowNodeFamily>(
auto family = rootComponentDescriptor.createFamily(
ShadowNodeFamilyFragment{surfaceId, surfaceId, noopEventEmitter},
rootComponentDescriptor);
nullptr);
rootShadowNode_ = std::static_pointer_cast<const RootShadowNode>(
rootComponentDescriptor.createShadowNode(
ShadowNodeFragment{
@@ -172,12 +172,9 @@ SharedShadowNode ComponentDescriptorRegistry::createNode(
auto unifiedComponentName = componentNameByReactViewName(viewName);
auto const &componentDescriptor = this->at(unifiedComponentName);
auto family = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{
tag,
surfaceId,
componentDescriptor.createEventEmitter(std::move(eventTarget), tag)},
componentDescriptor);
auto family = componentDescriptor.createFamily(
ShadowNodeFamilyFragment{tag, surfaceId, nullptr},
std::move(eventTarget));
auto const props =
componentDescriptor.cloneProps(nullptr, RawProps(propsDynamic));
auto const state = componentDescriptor.createInitialState(
+3 -6
View File
@@ -33,12 +33,9 @@ SharedShadowNode UIManager::createNode(
auto fallbackDescriptor =
componentDescriptorRegistry_->getFallbackComponentDescriptor();
auto family = std::make_shared<ShadowNodeFamily>(
ShadowNodeFamilyFragment{
tag,
surfaceId,
componentDescriptor.createEventEmitter(std::move(eventTarget), tag)},
componentDescriptor);
auto family = componentDescriptor.createFamily(
ShadowNodeFamilyFragment{tag, surfaceId, nullptr},
std::move(eventTarget));
auto const props = componentDescriptor.cloneProps(nullptr, rawProps);
auto const state = componentDescriptor.createInitialState(
ShadowNodeFragment{props}, surfaceId);