Skip to content

Commit a09ab53

Browse files
sherginfacebook-github-bot
authored andcommitted
Fabric: Use ComponentDescriptorParameters as an actual parameter of ComponentDescriptor constructor
Summary: This diff changes the signature of ComponentDescriptor constructor to make it simpler and easier to support: now all arguments are passed via struct that contains all these arguments as fields. Now the ComponentDescriptor constructor accepts three arguments one of which is optional. This causes some confusion and the possibility of bugs in all subclasses that needs to implement a custom constructor. Mostly because in every case we need to ensure that the constructor: * Accepts and pass down all parameters/arguments; * Accepts the right types of those parameters (shared vs weak pointers, references vs values). * Accepts all thee arguments and pass them (including flavor!). We failed this point several times. Overal that makes the code simpler and allows changing the set of parameters relatively easy. (There is no plan for it!) Look at the LOC balance: less code! Changelog: [INTERNAL] Reviewed By: sammy-SC Differential Revision: D18548173 fbshipit-source-id: 5d038b135e004f6c054026b3235ed57db99c086d
1 parent a8fbbe2 commit a09ab53

15 files changed

+67
-103
lines changed

ReactCommon/fabric/components/image/ImageComponentDescriptor.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ namespace react {
2121
class ImageComponentDescriptor final
2222
: public ConcreteComponentDescriptor<ImageShadowNode> {
2323
public:
24-
ImageComponentDescriptor(
25-
EventDispatcher::Weak eventDispatcher,
26-
ContextContainer::Shared const &contextContainer,
27-
ComponentDescriptor::Flavor const &flavor = {})
28-
: ConcreteComponentDescriptor(eventDispatcher, contextContainer, flavor),
29-
imageManager_(std::make_shared<ImageManager>(contextContainer)){};
24+
ImageComponentDescriptor(ComponentDescriptorParameters const &parameters)
25+
: ConcreteComponentDescriptor(parameters),
26+
imageManager_(std::make_shared<ImageManager>(contextContainer_)){};
3027

3128
void adopt(UnsharedShadowNode shadowNode) const override {
3229
ConcreteComponentDescriptor::adopt(shadowNode);

ReactCommon/fabric/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class LegacyViewManagerInteropComponentDescriptor final
1919
using ConcreteComponentDescriptor::ConcreteComponentDescriptor;
2020

2121
LegacyViewManagerInteropComponentDescriptor(
22-
EventDispatcher::Weak const &eventDispatcher,
23-
ContextContainer::Shared const &contextContainer = {},
24-
ComponentDescriptor::Flavor const &flavor = {});
22+
ComponentDescriptorParameters const &parameters);
2523
/*
2624
* Returns `name` and `handle` based on a `flavor`, not on static data from
2725
* `LegacyViewManagerInteropShadowNode`.

ReactCommon/fabric/components/legacyviewmanagerinterop/LegacyViewManagerInteropComponentDescriptor.mm

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,27 @@
5252
}
5353

5454
LegacyViewManagerInteropComponentDescriptor::LegacyViewManagerInteropComponentDescriptor(
55-
EventDispatcher::Weak const &eventDispatcher,
56-
ContextContainer::Shared const &contextContainer,
57-
ComponentDescriptor::Flavor const &flavor)
58-
: ConcreteComponentDescriptor(eventDispatcher, contextContainer, flavor),
59-
_coordinator(constructCoordinator(contextContainer, flavor))
55+
ComponentDescriptorParameters const &parameters)
56+
: ConcreteComponentDescriptor(parameters), _coordinator(constructCoordinator(contextContainer_, flavor_))
6057
{
6158
}
6259

63-
ComponentHandle
64-
LegacyViewManagerInteropComponentDescriptor::getComponentHandle() const {
60+
ComponentHandle LegacyViewManagerInteropComponentDescriptor::getComponentHandle() const
61+
{
6562
return reinterpret_cast<ComponentHandle>(getComponentName());
6663
}
6764

68-
ComponentName LegacyViewManagerInteropComponentDescriptor::getComponentName()
69-
const {
65+
ComponentName LegacyViewManagerInteropComponentDescriptor::getComponentName() const
66+
{
7067
return std::static_pointer_cast<std::string const>(this->flavor_)->c_str();
7168
}
7269

73-
void LegacyViewManagerInteropComponentDescriptor::adopt(
74-
ShadowNode::Unshared shadowNode) const {
70+
void LegacyViewManagerInteropComponentDescriptor::adopt(ShadowNode::Unshared shadowNode) const
71+
{
7572
ConcreteComponentDescriptor::adopt(shadowNode);
7673

77-
assert(std::dynamic_pointer_cast<LegacyViewManagerInteropShadowNode>(
78-
shadowNode));
79-
auto legacyViewManagerInteropShadowNode =
80-
std::static_pointer_cast<LegacyViewManagerInteropShadowNode>(shadowNode);
74+
assert(std::dynamic_pointer_cast<LegacyViewManagerInteropShadowNode>(shadowNode));
75+
auto legacyViewManagerInteropShadowNode = std::static_pointer_cast<LegacyViewManagerInteropShadowNode>(shadowNode);
8176

8277
auto state = LegacyViewManagerInteropState{};
8378
state.coordinator = _coordinator;

ReactCommon/fabric/components/modal/ModalHostViewComponentDescriptor.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,7 @@ namespace react {
2121
class ModalHostViewComponentDescriptor final
2222
: public ConcreteComponentDescriptor<ModalHostViewShadowNode> {
2323
public:
24-
#ifdef ANDROID
25-
ModalHostViewComponentDescriptor(
26-
EventDispatcher::Weak eventDispatcher,
27-
ContextContainer::Shared const &contextContainer,
28-
ComponentDescriptor::Flavor const &flavor = {})
29-
: ConcreteComponentDescriptor(eventDispatcher, contextContainer, flavor) {
30-
}
31-
#else
3224
using ConcreteComponentDescriptor::ConcreteComponentDescriptor;
33-
#endif
3425

3526
void adopt(UnsharedShadowNode shadowNode) const override {
3627
assert(std::dynamic_pointer_cast<ModalHostViewShadowNode>(shadowNode));

ReactCommon/fabric/components/slider/SliderComponentDescriptor.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,12 @@ namespace react {
2020
class SliderComponentDescriptor final
2121
: public ConcreteComponentDescriptor<SliderShadowNode> {
2222
public:
23-
SliderComponentDescriptor(
24-
EventDispatcher::Weak eventDispatcher,
25-
ContextContainer::Shared const &contextContainer,
26-
ComponentDescriptor::Flavor const &flavor = {})
27-
: ConcreteComponentDescriptor(eventDispatcher, contextContainer, flavor),
28-
imageManager_(std::make_shared<ImageManager>(contextContainer)),
23+
SliderComponentDescriptor(ComponentDescriptorParameters const &parameters)
24+
: ConcreteComponentDescriptor(parameters),
25+
imageManager_(std::make_shared<ImageManager>(contextContainer_)),
2926
measurementsManager_(
3027
SliderMeasurementsManager::shouldMeasureSlider()
31-
? std::make_shared<SliderMeasurementsManager>(contextContainer)
28+
? std::make_shared<SliderMeasurementsManager>(contextContainer_)
3229
: nullptr) {}
3330

3431
void adopt(UnsharedShadowNode shadowNode) const override {

ReactCommon/fabric/components/switch/androidswitch/AndroidSwitchComponentDescriptor.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ class AndroidSwitchComponentDescriptor final
2222
: public ConcreteComponentDescriptor<AndroidSwitchShadowNode> {
2323
public:
2424
AndroidSwitchComponentDescriptor(
25-
EventDispatcher::Weak eventDispatcher,
26-
ContextContainer::Shared const &contextContainer,
27-
ComponentDescriptor::Flavor const &flavor = {})
28-
: ConcreteComponentDescriptor(eventDispatcher, contextContainer, flavor),
25+
ComponentDescriptorParameters const &parameters)
26+
: ConcreteComponentDescriptor(parameters),
2927
measurementsManager_(std::make_shared<AndroidSwitchMeasurementsManager>(
30-
contextContainer)) {}
28+
contextContainer_)) {}
3129

3230
void adopt(UnsharedShadowNode shadowNode) const override {
3331
ConcreteComponentDescriptor::adopt(shadowNode);

ReactCommon/fabric/components/text/paragraph/ParagraphComponentDescriptor.h

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@ namespace react {
2323
class ParagraphComponentDescriptor final
2424
: public ConcreteComponentDescriptor<ParagraphShadowNode> {
2525
public:
26-
ParagraphComponentDescriptor(
27-
EventDispatcher::Weak eventDispatcher,
28-
ContextContainer::Shared const &contextContainer,
29-
ComponentDescriptor::Flavor const &flavor = {})
30-
: ConcreteComponentDescriptor<ParagraphShadowNode>(
31-
eventDispatcher,
32-
contextContainer,
33-
flavor) {
26+
ParagraphComponentDescriptor(ComponentDescriptorParameters const &parameters)
27+
: ConcreteComponentDescriptor<ParagraphShadowNode>(parameters) {
3428
// Every single `ParagraphShadowNode` will have a reference to
3529
// a shared `TextLayoutManager`.
36-
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer);
30+
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer_);
3731
}
3832

3933
protected:

ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputComponentDescriptor.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@ class AndroidTextInputComponentDescriptor final
2020
: public ConcreteComponentDescriptor<AndroidTextInputShadowNode> {
2121
public:
2222
AndroidTextInputComponentDescriptor(
23-
EventDispatcher::Weak eventDispatcher,
24-
const ContextContainer::Shared &contextContainer,
25-
ComponentDescriptor::Flavor const &flavor = {})
26-
: ConcreteComponentDescriptor<AndroidTextInputShadowNode>(
27-
eventDispatcher,
28-
contextContainer,
29-
flavor) {
23+
ComponentDescriptorParameters const &parameters)
24+
: ConcreteComponentDescriptor<AndroidTextInputShadowNode>(parameters) {
3025
// Every single `AndroidTextInputShadowNode` will have a reference to
3126
// a shared `TextLayoutManager`.
32-
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer);
27+
textLayoutManager_ = std::make_shared<TextLayoutManager>(contextContainer_);
3328
}
3429

3530
protected:

ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@ namespace facebook {
1111
namespace react {
1212

1313
ComponentDescriptor::ComponentDescriptor(
14-
EventDispatcher::Weak const &eventDispatcher,
15-
ContextContainer::Shared const &contextContainer,
16-
ComponentDescriptor::Flavor const &flavor)
17-
: eventDispatcher_(eventDispatcher),
18-
contextContainer_(contextContainer),
19-
flavor_(flavor) {}
14+
ComponentDescriptorParameters const &parameters)
15+
: eventDispatcher_(parameters.eventDispatcher),
16+
contextContainer_(parameters.contextContainer),
17+
flavor_(parameters.flavor) {}
2018

2119
ContextContainer::Shared const &ComponentDescriptor::getContextContainer()
2220
const {

ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace facebook {
1919
namespace react {
2020

21+
class ComponentDescriptorParameters;
2122
class ComponentDescriptor;
2223

2324
using SharedComponentDescriptor = std::shared_ptr<ComponentDescriptor const>;
@@ -44,10 +45,7 @@ class ComponentDescriptor {
4445
*/
4546
using Flavor = std::shared_ptr<void const>;
4647

47-
ComponentDescriptor(
48-
EventDispatcher::Weak const &eventDispatcher,
49-
ContextContainer::Shared const &contextContainer,
50-
ComponentDescriptor::Flavor const &flavor);
48+
ComponentDescriptor(ComponentDescriptorParameters const &parameters);
5149

5250
virtual ~ComponentDescriptor() = default;
5351

@@ -136,5 +134,16 @@ class ComponentDescriptor {
136134
Flavor flavor_;
137135
};
138136

137+
/*
138+
* Represents a collection of arguments that sufficient to construct a
139+
* `ComponentDescriptor`.
140+
*/
141+
class ComponentDescriptorParameters {
142+
public:
143+
EventDispatcher::Weak eventDispatcher;
144+
ContextContainer::Shared contextContainer;
145+
ComponentDescriptor::Flavor flavor;
146+
};
147+
139148
} // namespace react
140149
} // namespace facebook

0 commit comments

Comments
 (0)