From 2a3025da979e4d19792494db1f6d13a13b9b38a4 Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Tue, 22 May 2018 15:48:19 -0700 Subject: [PATCH] Fabric: Application layer of events infrastructure Summary: This implements `EventHandlers` abstract class (aka "Events Guy") which encapsulates `eventDispatcher` and `instanceHandle` (and ownership of future `eventTarget`), all of this as part of existing {ShadowNode + Props + LayoutMetrics + LocalData + Descriptor + (and now) EventHandlers} infra. (We don't plan to add anything else to this model. Ever.) Reviewed By: fkgozali Differential Revision: D8053351 fbshipit-source-id: 1dd9ccbcbe5a2eb284b59ea351dc8beca645e8bf --- ReactCommon/fabric/core/BUCK | 1 + .../componentdescriptor/ComponentDescriptor.h | 11 +++- .../ConcreteComponentDescriptor.h | 39 ++++++++++--- .../fabric/core/events/EventDispatcher.h | 43 ++++++++++++++ .../fabric/core/events/EventHandlers.cpp | 34 +++++++++++ .../fabric/core/events/EventHandlers.h | 57 +++++++++++++++++++ .../fabric/core/events/EventPrimitives.h | 26 +++++++++ .../core/shadownode/ConcreteShadowNode.h | 10 +++- .../fabric/core/shadownode/ShadowNode.cpp | 22 ++++--- .../fabric/core/shadownode/ShadowNode.h | 15 +++-- .../core/tests/ComponentDescriptorTest.cpp | 16 +++--- .../fabric/core/tests/ShadowNodeTest.cpp | 41 ++++++------- ReactCommon/fabric/core/tests/TestComponent.h | 2 + .../SampleComponentDescriptorFactor.cpp | 2 +- .../uimanager/ComponentDescriptorFactory.h | 3 +- .../fabric/uimanager/FabricUIManager.cpp | 26 +++++---- .../fabric/uimanager/FabricUIManager.h | 10 ++-- ReactCommon/fabric/uimanager/Scheduler.cpp | 3 +- ReactCommon/fabric/uimanager/Scheduler.h | 2 + .../uimanager/SchedulerEventDispatcher.cpp | 23 ++++++++ .../uimanager/SchedulerEventDispatcher.h | 34 +++++++++++ ReactCommon/fabric/uimanager/ShadowTree.cpp | 7 ++- 22 files changed, 350 insertions(+), 77 deletions(-) create mode 100644 ReactCommon/fabric/core/events/EventDispatcher.h create mode 100644 ReactCommon/fabric/core/events/EventHandlers.cpp create mode 100644 ReactCommon/fabric/core/events/EventHandlers.h create mode 100644 ReactCommon/fabric/core/events/EventPrimitives.h create mode 100644 ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp create mode 100644 ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h diff --git a/ReactCommon/fabric/core/BUCK b/ReactCommon/fabric/core/BUCK index e63e79ad94d..a444f072680 100644 --- a/ReactCommon/fabric/core/BUCK +++ b/ReactCommon/fabric/core/BUCK @@ -22,6 +22,7 @@ rn_xplat_cxx_library( exported_headers = subdir_glob( [ ("", "*.h"), + ("events", "*.h"), ("primitives", "*.h"), ("componentdescriptor", "*.h"), ("layout", "*.h"), diff --git a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h index 1b92620d9fd..4804e5ad70c 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h @@ -47,7 +47,7 @@ public: virtual SharedShadowNode createShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, + const SharedEventHandlers &eventHandlers, const SharedProps &props ) const = 0; @@ -57,6 +57,7 @@ public: virtual SharedShadowNode cloneShadowNode( const SharedShadowNode &shadowNode, const SharedProps &props = nullptr, + const SharedEventHandlers &eventHandlers = nullptr, const SharedShadowNodeSharedList &children = nullptr ) const = 0; @@ -78,6 +79,14 @@ public: const SharedProps &props, const RawProps &rawProps ) const = 0; + + /* + * Creates a new `EventHandlers` object compatible with particular type of + * shadow nodes. + */ + virtual SharedEventHandlers createEventHandlers( + const InstanceHandle &instanceHandle + ) const = 0; }; } // namespace react diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index 3d2721f8156..6ae4ea67321 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -29,8 +29,13 @@ class ConcreteComponentDescriptor: public ComponentDescriptor { using SharedShadowNodeT = std::shared_ptr; using ConcreteProps = typename ShadowNodeT::ConcreteProps; using SharedConcreteProps = typename ShadowNodeT::SharedConcreteProps; + using ConcreteEventHandlers = typename ShadowNodeT::ConcreteEventHandlers; + using SharedConcreteEventHandlers = typename ShadowNodeT::SharedConcreteEventHandlers; public: + ConcreteComponentDescriptor(SharedEventDispatcher eventDispatcher): + eventDispatcher_(eventDispatcher) {} + ComponentHandle getComponentHandle() const override { return typeid(ShadowNodeT).hash_code(); } @@ -43,8 +48,8 @@ public: return std::make_shared( 0, 0, - nullptr, std::make_shared(), + nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr )->ShadowNodeT::getComponentName(); @@ -53,17 +58,21 @@ public: SharedShadowNode createShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, + const SharedEventHandlers &eventHandlers, const SharedProps &props ) const override { - UnsharedShadowNode shadowNode = std::make_shared( + assert(std::dynamic_pointer_cast(props)); + assert(std::dynamic_pointer_cast(eventHandlers)); + + auto &&shadowNode = std::make_shared( tag, rootTag, - instanceHandle, std::static_pointer_cast(props), + std::static_pointer_cast(eventHandlers), ShadowNode::emptySharedShadowNodeSharedList(), getCloneFunction() ); + adopt(shadowNode); return shadowNode; } @@ -71,10 +80,18 @@ public: SharedShadowNode cloneShadowNode( const SharedShadowNode &sourceShadowNode, const SharedProps &props = nullptr, + const SharedEventHandlers &eventHandlers = nullptr, const SharedShadowNodeSharedList &children = nullptr ) const override { assert(std::dynamic_pointer_cast(sourceShadowNode)); - UnsharedShadowNode shadowNode = std::make_shared(std::static_pointer_cast(sourceShadowNode), std::static_pointer_cast(props), children); + + auto &&shadowNode = std::make_shared( + std::static_pointer_cast(sourceShadowNode), + std::static_pointer_cast(props), + std::static_pointer_cast(eventHandlers), + children + ); + adopt(shadowNode); return shadowNode; } @@ -95,6 +112,12 @@ public: return ShadowNodeT::Props(rawProps, props); }; + virtual SharedEventHandlers createEventHandlers( + const InstanceHandle &instanceHandle + ) const override { + return std::make_shared(instanceHandle, eventDispatcher_); + } + protected: virtual void adopt(UnsharedShadowNode shadowNode) const { @@ -103,13 +126,15 @@ protected: private: + mutable SharedEventDispatcher eventDispatcher_ {nullptr}; + mutable ShadowNodeCloneFunction cloneFunction_; ShadowNodeCloneFunction getCloneFunction() const { if (!cloneFunction_) { - cloneFunction_ = [this](const SharedShadowNode &shadowNode, const SharedProps &props, const SharedShadowNodeSharedList &children) { + cloneFunction_ = [this](const SharedShadowNode &shadowNode, const SharedProps &props, const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children) { assert(std::dynamic_pointer_cast(shadowNode)); - return this->cloneShadowNode(shadowNode, props, children); + return this->cloneShadowNode(shadowNode, props, eventHandlers, children); }; } diff --git a/ReactCommon/fabric/core/events/EventDispatcher.h b/ReactCommon/fabric/core/events/EventDispatcher.h new file mode 100644 index 00000000000..354e35ed477 --- /dev/null +++ b/ReactCommon/fabric/core/events/EventDispatcher.h @@ -0,0 +1,43 @@ +/** + * 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. + */ +#pragma once + +#include + +#include +#include +#include + +namespace facebook { +namespace react { + +class EventDispatcher; + +using SharedEventDispatcher = std::shared_ptr; + +/* + * Abstract class that represent event-delivery infrastructure. + * Particular `EventHandlers` clases use an object of this class to invoke + * events. + */ +class EventDispatcher { + +public: + + /* + * Dispatches "raw" event using some event-delivery infrastructure. + */ + virtual void dispatchEvent( + const InstanceHandle &instanceHandle, + const std::string &name, + const folly::dynamic &payload, + const EventPriority &priority + ) const = 0; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/core/events/EventHandlers.cpp b/ReactCommon/fabric/core/events/EventHandlers.cpp new file mode 100644 index 00000000000..25bd7dfec90 --- /dev/null +++ b/ReactCommon/fabric/core/events/EventHandlers.cpp @@ -0,0 +1,34 @@ +/** + * 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 "EventHandlers.h" + +#include + +namespace facebook { +namespace react { + +EventHandlers::EventHandlers(InstanceHandle instanceHandle, SharedEventDispatcher eventDispatcher): + instanceHandle_(instanceHandle), + eventDispatcher_(eventDispatcher) {} + +void EventHandlers::dispatchEvent( + const std::string &name, + const folly::dynamic &payload, + const EventPriority &priority +) const { + auto &&eventDispatcher = eventDispatcher_.lock(); + if (!eventDispatcher) { + return; + } + + // TODO(T29610783): Reconsider using dynamic dispatch here. + eventDispatcher->dispatchEvent(instanceHandle_, name, payload, priority); +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/core/events/EventHandlers.h b/ReactCommon/fabric/core/events/EventHandlers.h new file mode 100644 index 00000000000..596e8871a90 --- /dev/null +++ b/ReactCommon/fabric/core/events/EventHandlers.h @@ -0,0 +1,57 @@ +/** + * 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. + */ +#pragma once + +#include + +#include +#include +#include +#include + +namespace facebook { +namespace react { + +class EventHandlers; + +using SharedEventHandlers = std::shared_ptr; + + + +/* + * Base class for all particular typed event handlers. + * Stores `InstanceHandle` identifying a particular component and the pointer + * to `EventDispatcher` which is responsible for delivering the event. + * + * TODO: Reconsider naming of all event-related things. + */ +class EventHandlers { + +public: + EventHandlers(InstanceHandle instanceHandle, SharedEventDispatcher eventDispatcher); + virtual ~EventHandlers() = default; + +protected: + + /* + * Initates an event delivery process. + * Is used by particular subclasses only. + */ + void dispatchEvent( + const std::string &name, + const folly::dynamic &payload = {}, + const EventPriority &priority = EventPriority::AsynchronousBatched + ) const; + +private: + + InstanceHandle instanceHandle_; + std::weak_ptr eventDispatcher_; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/core/events/EventPrimitives.h b/ReactCommon/fabric/core/events/EventPrimitives.h new file mode 100644 index 00000000000..ba12b23b1be --- /dev/null +++ b/ReactCommon/fabric/core/events/EventPrimitives.h @@ -0,0 +1,26 @@ +/** + * 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. + */ + +#pragma once + +namespace facebook { +namespace react { + +enum class EventPriority { + SynchronousUnbatched, + SynchronousBatched, + AsynchronousUnbatched, + AsynchronousBatched, + + Sync = SynchronousUnbatched, + Work = SynchronousBatched, + Interactive = AsynchronousUnbatched, + Deferred = AsynchronousBatched, +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h b/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h index 6cf911ea132..86c0976b8ce 100644 --- a/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h +++ b/ReactCommon/fabric/core/shadownode/ConcreteShadowNode.h @@ -19,13 +19,15 @@ namespace react { * `ConcreteShadowNode` is a default implementation of `ShadowNode` interface * with many handy features. */ -template +template class ConcreteShadowNode: public ShadowNode { static_assert(std::is_base_of::value, "PropsT must be a descendant of Props"); public: using ConcreteProps = PropsT; using SharedConcreteProps = std::shared_ptr; + using ConcreteEventHandlers = EventHandlersT; + using SharedConcreteEventHandlers = std::shared_ptr; using SharedConcreteShadowNode = std::shared_ptr; static SharedConcreteProps Props(const RawProps &rawProps, const SharedProps &baseProps = nullptr) { @@ -40,16 +42,16 @@ public: ConcreteShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, const SharedConcreteProps &props, + const SharedConcreteEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children, const ShadowNodeCloneFunction &cloneFunction ): ShadowNode( tag, rootTag, - instanceHandle, (SharedProps)props, + eventHandlers, children, cloneFunction ) {}; @@ -57,11 +59,13 @@ public: ConcreteShadowNode( const SharedConcreteShadowNode &shadowNode, const SharedProps &props, + const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children ): ShadowNode( shadowNode, (SharedProps)props, + eventHandlers, children ) {} diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp index b4883c22f4d..e18ae2e387d 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.cpp +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.cpp @@ -23,15 +23,15 @@ SharedShadowNodeSharedList ShadowNode::emptySharedShadowNodeSharedList() { ShadowNode::ShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, const SharedProps &props, + const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children, const ShadowNodeCloneFunction &cloneFunction ): tag_(tag), rootTag_(rootTag), - instanceHandle_(instanceHandle), props_(props), + eventHandlers_(eventHandlers), children_(std::make_shared(*children)), cloneFunction_(cloneFunction), revision_(1) {} @@ -39,12 +39,13 @@ ShadowNode::ShadowNode( ShadowNode::ShadowNode( const SharedShadowNode &shadowNode, const SharedProps &props, + const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children ): tag_(shadowNode->tag_), rootTag_(shadowNode->rootTag_), - instanceHandle_(shadowNode->instanceHandle_), props_(props ? props : shadowNode->props_), + eventHandlers_(eventHandlers ? eventHandlers : shadowNode->eventHandlers_), children_(std::make_shared(*(children ? children : shadowNode->children_))), sourceNode_(shadowNode), localData_(shadowNode->localData_), @@ -56,7 +57,7 @@ SharedShadowNode ShadowNode::clone( const SharedShadowNodeSharedList &children ) const { assert(cloneFunction_); - return cloneFunction_(shared_from_this(), props_, children_); + return cloneFunction_(shared_from_this(), props_, eventHandlers_, children_); } #pragma mark - Getters @@ -69,6 +70,10 @@ SharedProps ShadowNode::getProps() const { return props_; } +SharedEventHandlers ShadowNode::getEventHandlers() const { + return eventHandlers_; +} + Tag ShadowNode::getTag() const { return tag_; } @@ -77,10 +82,6 @@ Tag ShadowNode::getRootTag() const { return rootTag_; } -InstanceHandle ShadowNode::getInstanceHandle() const { - return instanceHandle_; -} - SharedShadowNode ShadowNode::getSourceNode() const { return sourceNode_.lock(); } @@ -146,6 +147,7 @@ bool ShadowNode::operator==(const ShadowNode& rhs) const { tag_ == rhs.tag_ && rootTag_ == rhs.rootTag_ && props_ == rhs.props_ && + eventHandlers_ == rhs.eventHandlers_ && localData_ == rhs.localData_; } @@ -181,10 +183,6 @@ SharedDebugStringConvertibleList ShadowNode::getDebugProps() const { list.push_back(std::make_shared("tag", std::to_string(tag_))); - if (instanceHandle_) { - list.push_back(std::make_shared("handle", std::to_string((size_t)instanceHandle_))); - } - SharedShadowNode sourceNode = getSourceNode(); if (sourceNode) { list.push_back(std::make_shared( diff --git a/ReactCommon/fabric/core/shadownode/ShadowNode.h b/ReactCommon/fabric/core/shadownode/ShadowNode.h index 927590b868e..9fa1b9341d9 100644 --- a/ReactCommon/fabric/core/shadownode/ShadowNode.h +++ b/ReactCommon/fabric/core/shadownode/ShadowNode.h @@ -11,6 +11,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,12 @@ using SharedShadowNodeSharedList = std::shared_ptr; using SharedShadowNodeUnsharedList = std::shared_ptr; using WeakShadowNode = std::weak_ptr; -using ShadowNodeCloneFunction = std::function; +using ShadowNodeCloneFunction = std::function; class ShadowNode: public virtual Sealable, @@ -43,8 +49,8 @@ public: ShadowNode( const Tag &tag, const Tag &rootTag, - const InstanceHandle &instanceHandle, const SharedProps &props, + const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children, const ShadowNodeCloneFunction &cloneFunction ); @@ -52,6 +58,7 @@ public: ShadowNode( const SharedShadowNode &shadowNode, const SharedProps &props, + const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children ); @@ -70,9 +77,9 @@ public: SharedShadowNodeSharedList getChildren() const; SharedProps getProps() const; + SharedEventHandlers getEventHandlers() const; Tag getTag() const; Tag getRootTag() const; - InstanceHandle getInstanceHandle() const; /* * Returns the node which was used as a prototype in clone constructor. @@ -135,8 +142,8 @@ public: protected: Tag tag_; Tag rootTag_; - InstanceHandle instanceHandle_; SharedProps props_; + SharedEventHandlers eventHandlers_; SharedShadowNodeSharedList children_; WeakShadowNode sourceNode_; SharedLocalData localData_; diff --git a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp index f297d3800a8..99fb54201f0 100644 --- a/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp +++ b/ReactCommon/fabric/core/tests/ComponentDescriptorTest.cpp @@ -12,7 +12,7 @@ using namespace facebook::react; TEST(ComponentDescriptorTest, createShadowNode) { - SharedComponentDescriptor descriptor = std::make_shared(); + SharedComponentDescriptor descriptor = std::make_shared(nullptr); ASSERT_EQ(descriptor->getComponentHandle(), typeid(TestShadowNode).hash_code()); ASSERT_STREQ(descriptor->getComponentName().c_str(), "Test"); @@ -20,7 +20,7 @@ TEST(ComponentDescriptorTest, createShadowNode) { RawProps raw; raw["nativeID"] = "abc"; SharedProps props = descriptor->cloneProps(nullptr, raw); - SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, props); + SharedShadowNode node = descriptor->createShadowNode(9, 1, nullptr, props); ASSERT_EQ(node->getComponentHandle(), typeid(TestShadowNode).hash_code()); ASSERT_STREQ(node->getComponentName().c_str(), "Test"); @@ -30,12 +30,12 @@ TEST(ComponentDescriptorTest, createShadowNode) { } TEST(ComponentDescriptorTest, cloneShadowNode) { - SharedComponentDescriptor descriptor = std::make_shared(); + SharedComponentDescriptor descriptor = std::make_shared(nullptr); RawProps raw; raw["nativeID"] = "abc"; SharedProps props = descriptor->cloneProps(nullptr, raw); - SharedShadowNode node = descriptor->createShadowNode(9, 1, (void *)NULL, props); + SharedShadowNode node = descriptor->createShadowNode(9, 1, nullptr, props); SharedShadowNode cloned = descriptor->cloneShadowNode(node); ASSERT_EQ(cloned->getComponentHandle(), typeid(TestShadowNode).hash_code()); @@ -46,14 +46,14 @@ TEST(ComponentDescriptorTest, cloneShadowNode) { } TEST(ComponentDescriptorTest, appendChild) { - SharedComponentDescriptor descriptor = std::make_shared(); + SharedComponentDescriptor descriptor = std::make_shared(nullptr); RawProps raw; raw["nativeID"] = "abc"; SharedProps props = descriptor->cloneProps(nullptr, raw); - SharedShadowNode node1 = descriptor->createShadowNode(1, 1, (void *)NULL, props); - SharedShadowNode node2 = descriptor->createShadowNode(2, 1, (void *)NULL, props); - SharedShadowNode node3 = descriptor->createShadowNode(3, 1, (void *)NULL, props); + SharedShadowNode node1 = descriptor->createShadowNode(1, 1, nullptr, props); + SharedShadowNode node2 = descriptor->createShadowNode(2, 1, nullptr, props); + SharedShadowNode node3 = descriptor->createShadowNode(3, 1, nullptr, props); descriptor->appendChild(node1, node2); descriptor->appendChild(node1, node3); diff --git a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp index dfdf0dec4ae..7afbafbb5f8 100644 --- a/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp +++ b/ReactCommon/fabric/core/tests/ShadowNodeTest.cpp @@ -28,13 +28,13 @@ TEST(ShadowNodeTest, handleProps) { } TEST(ShadowNodeTest, handleShadowNodeCreation) { - auto node = std::make_shared(9, 1, (void *)NULL, std::make_shared(), ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto node = std::make_shared(9, 1, std::make_shared(), nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); ASSERT_FALSE(node->getSealed()); ASSERT_STREQ(node->getComponentName().c_str(), "Test"); ASSERT_EQ(node->getTag(), 9); ASSERT_EQ(node->getRootTag(), 1); - ASSERT_EQ(node->getInstanceHandle(), (void *)NULL); + ASSERT_EQ(node->getEventHandlers(), nullptr); TestShadowNode *nodePtr = node.get(); ASSERT_EQ(node->getComponentHandle(), typeid(*nodePtr).hash_code()); ASSERT_EQ(node->getSourceNode(), nullptr); @@ -48,21 +48,21 @@ TEST(ShadowNodeTest, handleShadowNodeCreation) { } TEST(ShadowNodeTest, handleShadowNodeSimpleCloning) { - auto node = std::make_shared(9, 1, (void *)NULL, std::make_shared(), ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto node2 = std::make_shared(node, nullptr, nullptr); + auto node = std::make_shared(9, 1, std::make_shared(), nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto node2 = std::make_shared(node, nullptr, nullptr, nullptr); ASSERT_STREQ(node->getComponentName().c_str(), "Test"); ASSERT_EQ(node->getTag(), 9); ASSERT_EQ(node->getRootTag(), 1); - ASSERT_EQ(node->getInstanceHandle(), (void *)NULL); + ASSERT_EQ(node->getEventHandlers(), nullptr); ASSERT_EQ(node2->getSourceNode(), node); } TEST(ShadowNodeTest, handleShadowNodeMutation) { auto props = std::make_shared(); - auto node1 = std::make_shared(1, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto node2 = std::make_shared(2, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto node3 = std::make_shared(3, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto node1 = std::make_shared(1, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto node2 = std::make_shared(2, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto node3 = std::make_shared(3, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); node1->appendChild(node2); node1->appendChild(node3); @@ -71,7 +71,7 @@ TEST(ShadowNodeTest, handleShadowNodeMutation) { ASSERT_EQ(node1Children->at(0), node2); ASSERT_EQ(node1Children->at(1), node3); - auto node4 = std::make_shared(node2, nullptr, nullptr); + auto node4 = std::make_shared(node2, nullptr, nullptr, nullptr); node1->replaceChild(node2, node4); node1Children = node1->getChildren(); ASSERT_EQ(node1Children->size(), 2); @@ -87,16 +87,16 @@ TEST(ShadowNodeTest, handleShadowNodeMutation) { // No more mutation after sealing. EXPECT_THROW(node4->clearSourceNode(), std::runtime_error); - auto node5 = std::make_shared(node4, nullptr, nullptr); + auto node5 = std::make_shared(node4, nullptr, nullptr, nullptr); node5->clearSourceNode(); ASSERT_EQ(node5->getSourceNode(), nullptr); } TEST(ShadowNodeTest, handleSourceNode) { - auto nodeFirstGeneration = std::make_shared(9, 1, (void *)NULL, std::make_shared(), ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto nodeSecondGeneration = std::make_shared(nodeFirstGeneration, nullptr, nullptr); - auto nodeThirdGeneration = std::make_shared(nodeSecondGeneration, nullptr, nullptr); - auto nodeForthGeneration = std::make_shared(nodeThirdGeneration, nullptr, nullptr); + auto nodeFirstGeneration = std::make_shared(9, 1, std::make_shared(), nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto nodeSecondGeneration = std::make_shared(nodeFirstGeneration, nullptr, nullptr, nullptr); + auto nodeThirdGeneration = std::make_shared(nodeSecondGeneration, nullptr, nullptr, nullptr); + auto nodeForthGeneration = std::make_shared(nodeThirdGeneration, nullptr, nullptr, nullptr); // Ensure established shource nodes structure. ASSERT_EQ(nodeForthGeneration->getSourceNode(), nodeThirdGeneration); @@ -117,7 +117,7 @@ TEST(ShadowNodeTest, handleSourceNode) { } TEST(ShadowNodeTest, handleCloneFunction) { - auto firstNode = std::make_shared(9, 1, (void *)NULL, std::make_shared(), ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto firstNode = std::make_shared(9, 1, std::make_shared(), nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); // The shadow node is not clonable if `cloneFunction` is not provided, ASSERT_DEATH_IF_SUPPORTED(firstNode->clone(), "cloneFunction_"); @@ -125,13 +125,14 @@ TEST(ShadowNodeTest, handleCloneFunction) { auto secondNode = std::make_shared( 9, 1, - (void *)NULL, std::make_shared(), + nullptr, ShadowNode::emptySharedShadowNodeSharedList(), - [](const SharedShadowNode &shadowNode, const SharedProps &props, const SharedShadowNodeSharedList &children) { + [](const SharedShadowNode &shadowNode, const SharedProps &props, const SharedEventHandlers &eventHandlers, const SharedShadowNodeSharedList &children) { return std::make_shared( std::static_pointer_cast(shadowNode), props, + nullptr, children ); } @@ -161,9 +162,9 @@ TEST(ShadowNodeTest, handleLocalData) { auto localDataOver9000 = std::make_shared(); localDataOver9000->setNumber(9001); auto props = std::make_shared(); - auto firstNode = std::make_shared(9, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto secondNode = std::make_shared(9, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); - auto thirdNode = std::make_shared(9, 1, (void *)NULL, props, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto firstNode = std::make_shared(9, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto secondNode = std::make_shared(9, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); + auto thirdNode = std::make_shared(9, 1, props, nullptr, ShadowNode::emptySharedShadowNodeSharedList(), nullptr); firstNode->setLocalData(localData42); secondNode->setLocalData(localData42); diff --git a/ReactCommon/fabric/core/tests/TestComponent.h b/ReactCommon/fabric/core/tests/TestComponent.h index b7257caabc0..8659b960ca3 100644 --- a/ReactCommon/fabric/core/tests/TestComponent.h +++ b/ReactCommon/fabric/core/tests/TestComponent.h @@ -57,6 +57,8 @@ public: class TestComponentDescriptor: public ConcreteComponentDescriptor { public: + using ConcreteComponentDescriptor::ConcreteComponentDescriptor; + // TODO (shergin): Why does this gets repeated here and the shadow node class? ComponentName getComponentName() const override { return "Test"; diff --git a/ReactCommon/fabric/sample/SampleComponentDescriptorFactor.cpp b/ReactCommon/fabric/sample/SampleComponentDescriptorFactor.cpp index d45a37351fa..28a8a8b2979 100644 --- a/ReactCommon/fabric/sample/SampleComponentDescriptorFactor.cpp +++ b/ReactCommon/fabric/sample/SampleComponentDescriptorFactor.cpp @@ -14,7 +14,7 @@ namespace react { /** * This is a sample implementation. Each app should provide its own. */ -SharedComponentDescriptorRegistry ComponentDescriptorFactory::buildRegistry() { +SharedComponentDescriptorRegistry ComponentDescriptorFactory::buildRegistry(const SharedEventDispatcher &eventDispatcher) { auto registry = std::make_shared(); return registry; } diff --git a/ReactCommon/fabric/uimanager/ComponentDescriptorFactory.h b/ReactCommon/fabric/uimanager/ComponentDescriptorFactory.h index c729c45f4b1..ea6ef34ac6c 100644 --- a/ReactCommon/fabric/uimanager/ComponentDescriptorFactory.h +++ b/ReactCommon/fabric/uimanager/ComponentDescriptorFactory.h @@ -10,6 +10,7 @@ #include #include +#include #include "ComponentDescriptorRegistry.h" @@ -24,7 +25,7 @@ namespace react { class ComponentDescriptorFactory { public: - static SharedComponentDescriptorRegistry buildRegistry(); + static SharedComponentDescriptorRegistry buildRegistry(const SharedEventDispatcher &eventDispatcher); }; } // namespace react diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.cpp b/ReactCommon/fabric/uimanager/FabricUIManager.cpp index b725b9739ca..c41eddf0da3 100644 --- a/ReactCommon/fabric/uimanager/FabricUIManager.cpp +++ b/ReactCommon/fabric/uimanager/FabricUIManager.cpp @@ -91,7 +91,7 @@ UIManagerDelegate *FabricUIManager::getDelegate() { return delegate_; } -SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, void *instanceHandle) { +SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, InstanceHandle instanceHandle) { isLoggingEnabled && LOG(INFO) << "FabricUIManager::createNode(tag: " << tag << ", name: " << viewName << ", rootTag" << rootTag << ", props: " << props << ")"; ComponentName componentName = componentNameByReactViewName(viewName); @@ -102,7 +102,7 @@ SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int componentDescriptor->createShadowNode( tag, rootTag, - instanceHandle, + componentDescriptor->createEventHandlers(instanceHandle), componentDescriptor->cloneProps(nullptr, rawProps) ); @@ -115,28 +115,32 @@ SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int return shadowNode; } -SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode, void *instanceHandle) { +SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode, InstanceHandle instanceHandle) { isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNode(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")"; const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode]; - // TODO: Retain new instanceHandle SharedShadowNode clonedShadowNode = - componentDescriptor->cloneShadowNode(shadowNode); + componentDescriptor->cloneShadowNode( + shadowNode, + nullptr, + componentDescriptor->createEventHandlers(instanceHandle), + nullptr + ); isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNode() -> " << clonedShadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}); return clonedShadowNode; } -SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &shadowNode, void *instanceHandle) { +SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNode &shadowNode, InstanceHandle instanceHandle) { isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildren(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ")"; // Assuming semantic: Cloning with same props but empty children. const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode]; - // TODO: Retain new instanceHandle SharedShadowNode clonedShadowNode = componentDescriptor->cloneShadowNode( shadowNode, nullptr, + componentDescriptor->createEventHandlers(instanceHandle), ShadowNode::emptySharedShadowNodeSharedList() ); @@ -144,17 +148,17 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNod return clonedShadowNode; } -SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &shadowNode, folly::dynamic props, void *instanceHandle) { +SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode &shadowNode, folly::dynamic props, InstanceHandle instanceHandle) { isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")"; // Assuming semantic: Cloning with same children and specified props. const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode]; RawProps rawProps = rawPropsFromDynamic(props); - // TODO: Retain new instanceHandle SharedShadowNode clonedShadowNode = componentDescriptor->cloneShadowNode( shadowNode, componentDescriptor->cloneProps(shadowNode->getProps(), rawProps), + componentDescriptor->createEventHandlers(instanceHandle), nullptr ); @@ -162,17 +166,17 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode & return clonedShadowNode; } -SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &shadowNode, folly::dynamic props, void *instanceHandle) { +SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedShadowNode &shadowNode, folly::dynamic props, InstanceHandle instanceHandle) { isLoggingEnabled && LOG(INFO) << "FabricUIManager::cloneNodeWithNewChildrenAndProps(shadowNode: " << shadowNode->getDebugDescription(DebugStringConvertibleOptions {.format = false}) << ", props: " << props << ")"; // Assuming semantic: Cloning with empty children and specified props. const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[shadowNode]; RawProps rawProps = rawPropsFromDynamic(props); - // TODO: Retain new instanceHandle SharedShadowNode clonedShadowNode = componentDescriptor->cloneShadowNode( shadowNode, componentDescriptor->cloneProps(shadowNode->getProps(), rawProps), + componentDescriptor->createEventHandlers(instanceHandle), ShadowNode::emptySharedShadowNodeSharedList() ); diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.h b/ReactCommon/fabric/uimanager/FabricUIManager.h index 4fb64c780f0..edf887ebab1 100644 --- a/ReactCommon/fabric/uimanager/FabricUIManager.h +++ b/ReactCommon/fabric/uimanager/FabricUIManager.h @@ -35,11 +35,11 @@ public: #pragma mark - JavaScript/React-facing Interface - SharedShadowNode createNode(Tag reactTag, std::string viewName, Tag rootTag, folly::dynamic props, void *instanceHandle); - SharedShadowNode cloneNode(const SharedShadowNode &node, void *instanceHandle); - SharedShadowNode cloneNodeWithNewChildren(const SharedShadowNode &node, void *instanceHandle); - SharedShadowNode cloneNodeWithNewProps(const SharedShadowNode &node, folly::dynamic props, void *instanceHandle); - SharedShadowNode cloneNodeWithNewChildrenAndProps(const SharedShadowNode &node, folly::dynamic newProps, void *instanceHandle); + SharedShadowNode createNode(Tag reactTag, std::string viewName, Tag rootTag, folly::dynamic props, InstanceHandle instanceHandle); + SharedShadowNode cloneNode(const SharedShadowNode &node, InstanceHandle instanceHandle); + SharedShadowNode cloneNodeWithNewChildren(const SharedShadowNode &node, InstanceHandle instanceHandle); + SharedShadowNode cloneNodeWithNewProps(const SharedShadowNode &node, folly::dynamic props, InstanceHandle instanceHandle); + SharedShadowNode cloneNodeWithNewChildrenAndProps(const SharedShadowNode &node, folly::dynamic newProps, InstanceHandle instanceHandle); void appendChild(const SharedShadowNode &parentNode, const SharedShadowNode &childNode); SharedShadowNodeUnsharedList createChildSet(Tag rootTag); void appendChildToSet(const SharedShadowNodeUnsharedList &childSet, const SharedShadowNode &childNode); diff --git a/ReactCommon/fabric/uimanager/Scheduler.cpp b/ReactCommon/fabric/uimanager/Scheduler.cpp index 235206dbdc5..0183c3a5987 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.cpp +++ b/ReactCommon/fabric/uimanager/Scheduler.cpp @@ -20,7 +20,8 @@ namespace facebook { namespace react { Scheduler::Scheduler() { - auto componentDescriptorRegistry = ComponentDescriptorFactory::buildRegistry(); + eventDispatcher_ = std::make_shared(); + auto componentDescriptorRegistry = ComponentDescriptorFactory::buildRegistry(eventDispatcher_); uiManager_ = std::make_shared(componentDescriptorRegistry); uiManager_->setDelegate(this); } diff --git a/ReactCommon/fabric/uimanager/Scheduler.h b/ReactCommon/fabric/uimanager/Scheduler.h index c3730a03346..5ccd8cbb3a3 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.h +++ b/ReactCommon/fabric/uimanager/Scheduler.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -69,6 +70,7 @@ private: SchedulerDelegate *delegate_; std::shared_ptr uiManager_; std::unordered_map shadowTreeRegistry_; + std::shared_ptr eventDispatcher_; }; } // namespace react diff --git a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp new file mode 100644 index 00000000000..ad9f4413636 --- /dev/null +++ b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp @@ -0,0 +1,23 @@ +/** + * 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 "SchedulerEventDispatcher.h" + +namespace facebook { +namespace react { + +void SchedulerEventDispatcher::dispatchEvent( + const InstanceHandle &instanceHandle, + const std::string &name, + const folly::dynamic &payload, + const EventPriority &priority +) const { + // Some future magic here. +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h new file mode 100644 index 00000000000..17cd7610951 --- /dev/null +++ b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#pragma once + +#include +#include +#include + +namespace facebook { +namespace react { + +/* + * Concrete EventDispatcher. + */ +class SchedulerEventDispatcher final: + public EventDispatcher { + +public: + + void dispatchEvent( + const InstanceHandle &instanceHandle, + const std::string &name, + const folly::dynamic &payload, + const EventPriority &priority + ) const override; +}; + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/ShadowTree.cpp b/ReactCommon/fabric/uimanager/ShadowTree.cpp index c274d2e6852..439369b9e55 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.cpp +++ b/ReactCommon/fabric/uimanager/ShadowTree.cpp @@ -15,11 +15,12 @@ namespace react { ShadowTree::ShadowTree(Tag rootTag): rootTag_(rootTag) { + auto &&noopEventHandlers = std::make_shared(nullptr, nullptr); rootShadowNode_ = std::make_shared( rootTag, rootTag, - nullptr, RootShadowNode::defaultSharedProps(), + noopEventHandlers, ShadowNode::emptySharedShadowNodeSharedList(), nullptr ); @@ -47,14 +48,14 @@ void ShadowTree::constraintLayout(const LayoutConstraints &layoutConstraints, co UnsharedRootShadowNode ShadowTree::cloneRootShadowNode(const LayoutConstraints &layoutConstraints, const LayoutContext &layoutContext) const { auto oldRootShadowNode = rootShadowNode_; auto &&props = std::make_shared(*oldRootShadowNode->getProps(), layoutConstraints, layoutContext); - auto newRootShadowNode = std::make_shared(oldRootShadowNode, props, nullptr); + auto newRootShadowNode = std::make_shared(oldRootShadowNode, props, nullptr, nullptr); return newRootShadowNode; } void ShadowTree::complete(const SharedShadowNodeUnsharedList &rootChildNodes) { auto oldRootShadowNode = rootShadowNode_; auto newRootShadowNode = - std::make_shared(oldRootShadowNode, nullptr, SharedShadowNodeSharedList(rootChildNodes)); + std::make_shared(oldRootShadowNode, nullptr, nullptr, SharedShadowNodeSharedList(rootChildNodes)); complete(newRootShadowNode); }