From 0fc5a91889292eaf4fc2f9b7ba971478040c441e Mon Sep 17 00:00:00 2001 From: Valentin Shergin Date: Fri, 1 Jun 2018 09:36:19 -0700 Subject: [PATCH] Fabric: Integrating `tag` into `EventHandlers` Summary: In order to dispatch event, `EventHandlers` must also know react tag. So we have to store it inside. We plan to illuminate this requirement (and `tag` from `EventHandlers`) eventually. Reviewed By: fkgozali Differential Revision: D8211685 fbshipit-source-id: 2064c0f4a7869cbf4d2c92d0349f4ee3998cb8f5 --- .../core/componentdescriptor/ComponentDescriptor.h | 3 ++- .../ConcreteComponentDescriptor.h | 5 +++-- ReactCommon/fabric/core/events/EventHandlers.cpp | 12 +++++++++--- ReactCommon/fabric/core/events/EventHandlers.h | 7 ++++--- ReactCommon/fabric/uimanager/FabricUIManager.cpp | 10 +++++----- ReactCommon/fabric/uimanager/ShadowTree.cpp | 2 +- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h index 4804e5ad70c..669f8475fa4 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ComponentDescriptor.h @@ -85,7 +85,8 @@ public: * shadow nodes. */ virtual SharedEventHandlers createEventHandlers( - const InstanceHandle &instanceHandle + const InstanceHandle &instanceHandle, + const Tag &tag ) const = 0; }; diff --git a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h index 6ae4ea67321..8b5ab2ce7ce 100644 --- a/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h +++ b/ReactCommon/fabric/core/componentdescriptor/ConcreteComponentDescriptor.h @@ -113,9 +113,10 @@ public: }; virtual SharedEventHandlers createEventHandlers( - const InstanceHandle &instanceHandle + const InstanceHandle &instanceHandle, + const Tag &tag ) const override { - return std::make_shared(instanceHandle, eventDispatcher_); + return std::make_shared(instanceHandle, tag, eventDispatcher_); } protected: diff --git a/ReactCommon/fabric/core/events/EventHandlers.cpp b/ReactCommon/fabric/core/events/EventHandlers.cpp index 25bd7dfec90..6980b3e7c10 100644 --- a/ReactCommon/fabric/core/events/EventHandlers.cpp +++ b/ReactCommon/fabric/core/events/EventHandlers.cpp @@ -12,12 +12,13 @@ namespace facebook { namespace react { -EventHandlers::EventHandlers(InstanceHandle instanceHandle, SharedEventDispatcher eventDispatcher): +EventHandlers::EventHandlers(const InstanceHandle &instanceHandle, const Tag &tag, const SharedEventDispatcher &eventDispatcher): instanceHandle_(instanceHandle), + tag_(tag), eventDispatcher_(eventDispatcher) {} void EventHandlers::dispatchEvent( - const std::string &name, + const std::string &type, const folly::dynamic &payload, const EventPriority &priority ) const { @@ -26,8 +27,13 @@ void EventHandlers::dispatchEvent( return; } + // Mixing `target` into `payload`. + assert(payload.isObject()); + folly::dynamic extendedPayload = folly::dynamic::object("target", tag_); + extendedPayload.merge_patch(payload); + // TODO(T29610783): Reconsider using dynamic dispatch here. - eventDispatcher->dispatchEvent(instanceHandle_, name, payload, priority); + eventDispatcher->dispatchEvent(instanceHandle_, type, extendedPayload, priority); } } // namespace react diff --git a/ReactCommon/fabric/core/events/EventHandlers.h b/ReactCommon/fabric/core/events/EventHandlers.h index 596e8871a90..f2ae16fd6f0 100644 --- a/ReactCommon/fabric/core/events/EventHandlers.h +++ b/ReactCommon/fabric/core/events/EventHandlers.h @@ -32,8 +32,8 @@ using SharedEventHandlers = std::shared_ptr; class EventHandlers { public: - EventHandlers(InstanceHandle instanceHandle, SharedEventDispatcher eventDispatcher); virtual ~EventHandlers() = default; + EventHandlers(const InstanceHandle &instanceHandle, const Tag &tag, const SharedEventDispatcher &eventDispatcher); protected: @@ -42,14 +42,15 @@ protected: * Is used by particular subclasses only. */ void dispatchEvent( - const std::string &name, - const folly::dynamic &payload = {}, + const std::string &type, + const folly::dynamic &payload = folly::dynamic::object(), const EventPriority &priority = EventPriority::AsynchronousBatched ) const; private: InstanceHandle instanceHandle_; + Tag tag_; std::weak_ptr eventDispatcher_; }; diff --git a/ReactCommon/fabric/uimanager/FabricUIManager.cpp b/ReactCommon/fabric/uimanager/FabricUIManager.cpp index 7bedbfc8e8e..41115a191ee 100644 --- a/ReactCommon/fabric/uimanager/FabricUIManager.cpp +++ b/ReactCommon/fabric/uimanager/FabricUIManager.cpp @@ -134,7 +134,7 @@ SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int componentDescriptor->createShadowNode( tag, rootTag, - componentDescriptor->createEventHandlers(instanceHandle), + componentDescriptor->createEventHandlers(instanceHandle, tag), componentDescriptor->cloneProps(nullptr, rawProps) ); @@ -155,7 +155,7 @@ SharedShadowNode FabricUIManager::cloneNode(const SharedShadowNode &shadowNode, componentDescriptor->cloneShadowNode( shadowNode, nullptr, - componentDescriptor->createEventHandlers(instanceHandle), + componentDescriptor->createEventHandlers(instanceHandle, shadowNode->getTag()), nullptr ); @@ -172,7 +172,7 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewChildren(const SharedShadowNod componentDescriptor->cloneShadowNode( shadowNode, nullptr, - componentDescriptor->createEventHandlers(instanceHandle), + componentDescriptor->createEventHandlers(instanceHandle, shadowNode->getTag()), ShadowNode::emptySharedShadowNodeSharedList() ); @@ -190,7 +190,7 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewProps(const SharedShadowNode & componentDescriptor->cloneShadowNode( shadowNode, componentDescriptor->cloneProps(shadowNode->getProps(), rawProps), - componentDescriptor->createEventHandlers(instanceHandle), + componentDescriptor->createEventHandlers(instanceHandle, shadowNode->getTag()), nullptr ); @@ -208,7 +208,7 @@ SharedShadowNode FabricUIManager::cloneNodeWithNewChildrenAndProps(const SharedS componentDescriptor->cloneShadowNode( shadowNode, componentDescriptor->cloneProps(shadowNode->getProps(), rawProps), - componentDescriptor->createEventHandlers(instanceHandle), + componentDescriptor->createEventHandlers(instanceHandle, shadowNode->getTag()), ShadowNode::emptySharedShadowNodeSharedList() ); diff --git a/ReactCommon/fabric/uimanager/ShadowTree.cpp b/ReactCommon/fabric/uimanager/ShadowTree.cpp index 06e259f86d0..140ff8071e1 100644 --- a/ReactCommon/fabric/uimanager/ShadowTree.cpp +++ b/ReactCommon/fabric/uimanager/ShadowTree.cpp @@ -18,7 +18,7 @@ namespace react { ShadowTree::ShadowTree(Tag rootTag): rootTag_(rootTag) { - auto &&noopEventHandlers = std::make_shared(nullptr, nullptr); + auto &&noopEventHandlers = std::make_shared(nullptr, rootTag, nullptr); rootShadowNode_ = std::make_shared( rootTag, rootTag,