Fabric: Using types `EventTarget`, `EventHandler` & co. instead of `void *` everywhere

Summary:
Nothing actually changed besides type names... which actually helps me found an issue in FabricUIManager!
Now there is no a single `void *` in Fabric/C++ and JavaScript bindings. Yay!

Reviewed By: fkgozali

Differential Revision: D8191420

fbshipit-source-id: b1eb60b6bc34dd25ab200aab854ffbd7ccf5b15d
This commit is contained in:
Valentin Shergin 2018-05-29 14:54:52 -07:00 committed by Facebook Github Bot
parent 122b3791ed
commit b3b72bbdcf
4 changed files with 26 additions and 22 deletions

View File

@ -22,11 +22,15 @@ enum class EventPriority {
Deferred = AsynchronousBatched, Deferred = AsynchronousBatched,
}; };
// `InstanceHandler`, `EventTarget`, and `EventHandler` are all opaque /* `InstanceHandler`, `EventTarget`, and `EventHandler` are all opaque
// raw pointers. We use `struct {} *` trick to differentiate them in compiler's * raw pointers. We use `struct {} *` trick to differentiate them in compiler's
// eyes to ensure type safety. * eyes to ensure type safety.
using EventTarget = struct {} *; * These structs must have names (and the names must be exported)
using EventHandler = struct {} *; * to allow consistent template (e.g. `std::function`) instantiating
* across different modules.
*/
using EventTarget = struct EventTargetDummyStruct {} *;
using EventHandler = struct EventHandlerDummyStruct {} *;
} // namespace react } // namespace react
} // namespace facebook } // namespace facebook

View File

@ -19,7 +19,7 @@ namespace react {
* `Tag` and `InstanceHandle` are used to address React Native components. * `Tag` and `InstanceHandle` are used to address React Native components.
*/ */
using Tag = int32_t; using Tag = int32_t;
using InstanceHandle = void *; using InstanceHandle = struct InstanceHandleDummyStruct {} *;
/* /*
* `RawProps` represents untyped map with props comes from JavaScript side. * `RawProps` represents untyped map with props comes from JavaScript side.

View File

@ -78,7 +78,7 @@ FabricUIManager::FabricUIManager(SharedComponentDescriptorRegistry componentDesc
FabricUIManager::~FabricUIManager() { FabricUIManager::~FabricUIManager() {
if (eventHandler_) { if (eventHandler_) {
releaseEventTargetFunction_(eventHandler_); releaseEventHandlerFunction_(eventHandler_);
} }
} }
@ -106,11 +106,11 @@ void FabricUIManager::setReleaseEventHandlerFunction(std::function<ReleaseEventH
releaseEventHandlerFunction_ = releaseEventHandlerFunction; releaseEventHandlerFunction_ = releaseEventHandlerFunction;
} }
void *FabricUIManager::createEventTarget(void *instanceHandle) { EventTarget FabricUIManager::createEventTarget(const InstanceHandle &instanceHandle) const {
return createEventTargetFunction_(instanceHandle); return createEventTargetFunction_(instanceHandle);
} }
void FabricUIManager::dispatchEvent(void *eventTarget, const std::string &type, const folly::dynamic &payload) { void FabricUIManager::dispatchEvent(const EventTarget &eventTarget, const std::string &type, const folly::dynamic &payload) const {
dispatchEventFunction_( dispatchEventFunction_(
eventHandler_, eventHandler_,
eventTarget, eventTarget,
@ -119,12 +119,12 @@ void FabricUIManager::dispatchEvent(void *eventTarget, const std::string &type,
); );
} }
void FabricUIManager::releaseEventTarget(void *eventTarget) { void FabricUIManager::releaseEventTarget(const EventTarget &eventTarget) const {
releaseEventTargetFunction_(eventTarget); releaseEventTargetFunction_(eventTarget);
} }
SharedShadowNode FabricUIManager::createNode(int tag, std::string viewName, int rootTag, folly::dynamic props, InstanceHandle 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 << ")"; isLoggingEnabled && LOG(INFO) << "FabricUIManager::createNode(tag: " << tag << ", name: " << viewName << ", rootTag: " << rootTag << ", props: " << props << ")";
ComponentName componentName = componentNameByReactViewName(viewName); ComponentName componentName = componentNameByReactViewName(viewName);
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[componentName]; const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[componentName];
@ -251,8 +251,8 @@ void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedLi
} }
} }
void FabricUIManager::registerEventHandler(void *eventHandler) { void FabricUIManager::registerEventHandler(const EventHandler &eventHandler) {
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler)"; isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler: " << eventHandler << ")";
eventHandler_ = eventHandler; eventHandler_ = eventHandler;
} }

View File

@ -18,10 +18,10 @@
namespace facebook { namespace facebook {
namespace react { namespace react {
using CreateEventTargetFunction = void *(void *instanceHandle); using CreateEventTargetFunction = EventTarget (InstanceHandle instanceHandle);
using DispatchEventFunction = void (void *eventHandler, void *eventTarget, std::string type, folly::dynamic payload); using DispatchEventFunction = void (EventHandler eventHandler, EventTarget eventTarget, std::string type, folly::dynamic payload);
using ReleaseEventTargetFunction = void (void *eventTarget); using ReleaseEventTargetFunction = void (EventTarget eventTarget);
using ReleaseEventHandlerFunction = void (void *eventHandler); using ReleaseEventHandlerFunction = void (EventHandler eventHandler);
class FabricUIManager { class FabricUIManager {
public: public:
@ -51,9 +51,9 @@ public:
#pragma mark - Native-facing Interface #pragma mark - Native-facing Interface
void *createEventTarget(void *instanceHandle); EventTarget createEventTarget(const InstanceHandle &instanceHandle) const;
void dispatchEvent(void *eventTarget, const std::string &type, const folly::dynamic &payload); void dispatchEvent(const EventTarget &eventTarget, const std::string &type, const folly::dynamic &payload) const;
void releaseEventTarget(void *eventTarget); void releaseEventTarget(const EventTarget &eventTarget) const;
#pragma mark - JavaScript/React-facing Interface #pragma mark - JavaScript/React-facing Interface
@ -66,13 +66,13 @@ public:
SharedShadowNodeUnsharedList createChildSet(Tag rootTag); SharedShadowNodeUnsharedList createChildSet(Tag rootTag);
void appendChildToSet(const SharedShadowNodeUnsharedList &childSet, const SharedShadowNode &childNode); void appendChildToSet(const SharedShadowNodeUnsharedList &childSet, const SharedShadowNode &childNode);
void completeRoot(Tag rootTag, const SharedShadowNodeUnsharedList &childSet); void completeRoot(Tag rootTag, const SharedShadowNodeUnsharedList &childSet);
void registerEventHandler(void *eventHandler); void registerEventHandler(const EventHandler &eventHandler);
private: private:
SharedComponentDescriptorRegistry componentDescriptorRegistry_; SharedComponentDescriptorRegistry componentDescriptorRegistry_;
UIManagerDelegate *delegate_; UIManagerDelegate *delegate_;
void *eventHandler_; EventHandler eventHandler_;
std::function<CreateEventTargetFunction> createEventTargetFunction_; std::function<CreateEventTargetFunction> createEventTargetFunction_;
std::function<DispatchEventFunction> dispatchEventFunction_; std::function<DispatchEventFunction> dispatchEventFunction_;
std::function<ReleaseEventTargetFunction> releaseEventTargetFunction_; std::function<ReleaseEventTargetFunction> releaseEventTargetFunction_;