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,
};
// `InstanceHandler`, `EventTarget`, and `EventHandler` are all opaque
// raw pointers. We use `struct {} *` trick to differentiate them in compiler's
// eyes to ensure type safety.
using EventTarget = struct {} *;
using EventHandler = struct {} *;
/* `InstanceHandler`, `EventTarget`, and `EventHandler` are all opaque
* raw pointers. We use `struct {} *` trick to differentiate them in compiler's
* eyes to ensure type safety.
* These structs must have names (and the names must be exported)
* to allow consistent template (e.g. `std::function`) instantiating
* across different modules.
*/
using EventTarget = struct EventTargetDummyStruct {} *;
using EventHandler = struct EventHandlerDummyStruct {} *;
} // namespace react
} // namespace facebook

View File

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

View File

@ -78,7 +78,7 @@ FabricUIManager::FabricUIManager(SharedComponentDescriptorRegistry componentDesc
FabricUIManager::~FabricUIManager() {
if (eventHandler_) {
releaseEventTargetFunction_(eventHandler_);
releaseEventHandlerFunction_(eventHandler_);
}
}
@ -106,11 +106,11 @@ void FabricUIManager::setReleaseEventHandlerFunction(std::function<ReleaseEventH
releaseEventHandlerFunction_ = releaseEventHandlerFunction;
}
void *FabricUIManager::createEventTarget(void *instanceHandle) {
EventTarget FabricUIManager::createEventTarget(const InstanceHandle &instanceHandle) const {
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_(
eventHandler_,
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);
}
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);
const SharedComponentDescriptor &componentDescriptor = (*componentDescriptorRegistry_)[componentName];
@ -251,8 +251,8 @@ void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedLi
}
}
void FabricUIManager::registerEventHandler(void *eventHandler) {
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler)";
void FabricUIManager::registerEventHandler(const EventHandler &eventHandler) {
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler: " << eventHandler << ")";
eventHandler_ = eventHandler;
}

View File

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