From cc09d21e6047df8378ef3a7a209b68f8d63967a1 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Thu, 17 May 2018 20:03:46 -0700 Subject: [PATCH] Fabric: Simplifying usage of `ConcreteComponentDescriptor` Summary: Now ConcreteComponentDescriptor can infer `ComponentName` from `ShadowNodeT` automatically, so in the most cases we even don't need to create a subclass of that. Reviewed By: fkgozali Differential Revision: D8016965 fbshipit-source-id: d910597093c9f4c9f32ca06cb3ef1b12538b9543 --- .../ConcreteComponentDescriptor.h | 15 +++++++++++++++ .../scrollview/ScrollViewComponentDescriptor.h | 7 +------ .../text/paragraph/ParagraphComponentDescriptor.h | 4 ---- .../text/rawtext/RawTextComponentDescriptor.h | 10 +--------- .../fabric/text/text/TextComponentDescriptor.h | 8 +------- ReactCommon/fabric/view/ViewComponentDescriptor.h | 7 +------ 6 files changed, 19 insertions(+), 32 deletions(-) diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index f690cb652..3d2721f81 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -35,6 +35,21 @@ public: return typeid(ShadowNodeT).hash_code(); } + ComponentName getComponentName() const override { + // Even if this looks suboptimal, it is the only way to call + // a virtual non-static method of `ShadowNodeT`. + // Because it is not a hot path (it is executed once per an app run), + // it's fine. + return std::make_shared( + 0, + 0, + nullptr, + std::make_shared(), + ShadowNode::emptySharedShadowNodeSharedList(), + nullptr + )->ShadowNodeT::getComponentName(); + } + SharedShadowNode createShadowNode( const Tag &tag, const Tag &rootTag, diff --git a/ReactCommon/fabric/scrollview/ScrollViewComponentDescriptor.h b/ReactCommon/fabric/scrollview/ScrollViewComponentDescriptor.h index 44d61202e..b1c075980 100644 --- a/ReactCommon/fabric/scrollview/ScrollViewComponentDescriptor.h +++ b/ReactCommon/fabric/scrollview/ScrollViewComponentDescriptor.h @@ -13,12 +13,7 @@ namespace facebook { namespace react { -class ScrollViewComponentDescriptor final: public ConcreteComponentDescriptor { -public: - ComponentName getComponentName() const override { - return "ScrollView"; - } -}; +using ScrollViewComponentDescriptor = ConcreteComponentDescriptor; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h b/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h index d77676783..6648ddb5e 100644 --- a/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h +++ b/ReactCommon/fabric/text/paragraph/ParagraphComponentDescriptor.h @@ -27,10 +27,6 @@ public: textLayoutManager_ = std::make_shared(); } - ComponentName getComponentName() const override { - return "Paragraph"; - } - void adopt(UnsharedShadowNode shadowNode) const override { ConcreteComponentDescriptor::adopt(shadowNode); diff --git a/ReactCommon/fabric/text/rawtext/RawTextComponentDescriptor.h b/ReactCommon/fabric/text/rawtext/RawTextComponentDescriptor.h index 6f615fa3c..ca2ccdbe1 100644 --- a/ReactCommon/fabric/text/rawtext/RawTextComponentDescriptor.h +++ b/ReactCommon/fabric/text/rawtext/RawTextComponentDescriptor.h @@ -13,15 +13,7 @@ namespace facebook { namespace react { -/* - * Descriptor for component. - */ -class RawTextComponentDescriptor: public ConcreteComponentDescriptor { -public: - ComponentName getComponentName() const override { - return "RawText"; - } -}; +using RawTextComponentDescriptor = ConcreteComponentDescriptor; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/text/text/TextComponentDescriptor.h b/ReactCommon/fabric/text/text/TextComponentDescriptor.h index 043492f21..5abb428e3 100644 --- a/ReactCommon/fabric/text/text/TextComponentDescriptor.h +++ b/ReactCommon/fabric/text/text/TextComponentDescriptor.h @@ -8,18 +8,12 @@ #pragma once #include -#include #include namespace facebook { namespace react { -class TextComponentDescriptor: public ConcreteComponentDescriptor { -public: - ComponentName getComponentName() const override { - return "Text"; - } -}; +using TextComponentDescriptor = ConcreteComponentDescriptor; } // namespace react } // namespace facebook diff --git a/ReactCommon/fabric/view/ViewComponentDescriptor.h b/ReactCommon/fabric/view/ViewComponentDescriptor.h index 1db26dc3b..4c25d8b94 100644 --- a/ReactCommon/fabric/view/ViewComponentDescriptor.h +++ b/ReactCommon/fabric/view/ViewComponentDescriptor.h @@ -13,12 +13,7 @@ namespace facebook { namespace react { -class ViewComponentDescriptor: public ConcreteComponentDescriptor { -public: - ComponentName getComponentName() const override { - return "View"; - } -}; +using ViewComponentDescriptor = ConcreteComponentDescriptor; } // namespace react } // namespace facebook