Fabric: JSIReleaseFabricEventHandler was replaced with shared_ptr approach

Summary:
Previously, we used special JSI bindings method to release an event handler (`JSIReleaseFabricEventHandler`). Now we expose this ownership model as a regular `std::shared_ptr`, so when the owner got deallocated, the event handler will be released automatically.

Why not use `unique_ptr`? `unique_ptr` is faster (and simpler) indeed, but it does not provide `type erasure` functionality that we need; to use `unique_ptr` we would have to make JSI an explicit Fabric dependency (we will probably end up with it eventually, but I this particular case is not a good reason for that).
All interactions with `eventHandler_` are done in a non-owning manner, so it's as performant as unique_ptr anyway.

(Please ignore all changes in JSCFabricUIManager.h/cpp files, we will delete them soon.)

Reviewed By: mdvacca

Differential Revision: D9756732

fbshipit-source-id: bffdee0c724dc95855ced7c35e7c13cf1554796e
This commit is contained in:
Valentin Shergin 2018-09-13 22:55:58 -07:00 коммит произвёл Facebook Github Bot
Родитель ba608a2db7
Коммит a089df3f8b
3 изменённых файлов: 14 добавлений и 24 удалений

Просмотреть файл

@ -32,7 +32,12 @@ enum class EventPriority: int {
* across different modules.
*/
using EventTarget = struct EventTargetDummyStruct {} *;
using EventHandler = struct EventHandlerDummyStruct {} *;
/*
* We need this types only to ensure type-safety when we deal with them. Conceptually,
* they are opaque pointers to some types that derived from those classes.
*/
class EventHandler {};
/*
* EmptyEventTarget is used when some event cannot be dispatched to an original

Просмотреть файл

@ -85,12 +85,6 @@ static const std::string componentNameByReactViewName(std::string viewName) {
return viewName;
}
FabricUIManager::~FabricUIManager() {
if (eventHandler_) {
releaseEventHandlerFunction_(eventHandler_);
}
}
void FabricUIManager::setComponentDescriptorRegistry(const SharedComponentDescriptorRegistry &componentDescriptorRegistry) {
componentDescriptorRegistry_ = componentDescriptorRegistry;
}
@ -111,10 +105,6 @@ void FabricUIManager::setDispatchEventToTargetFunction(std::function<DispatchEve
dispatchEventToTargetFunction_ = dispatchEventFunction;
}
void FabricUIManager::setReleaseEventHandlerFunction(std::function<ReleaseEventHandlerFunction> releaseEventHandlerFunction) {
releaseEventHandlerFunction_ = releaseEventHandlerFunction;
}
void FabricUIManager::setReleaseEventTargetFunction(std::function<ReleaseEventTargetFunction> releaseEventTargetFunction) {
releaseEventTargetFunction_ = releaseEventTargetFunction;
}
@ -122,7 +112,7 @@ void FabricUIManager::setReleaseEventTargetFunction(std::function<ReleaseEventTa
void FabricUIManager::dispatchEventToTarget(const EventTarget &eventTarget, const std::string &type, const folly::dynamic &payload) const {
if (eventTarget != EmptyEventTarget) {
dispatchEventToTargetFunction_(
eventHandler_,
*eventHandler_,
eventTarget,
const_cast<std::string &>(type),
const_cast<folly::dynamic &>(payload)
@ -130,7 +120,7 @@ void FabricUIManager::dispatchEventToTarget(const EventTarget &eventTarget, cons
}
else {
dispatchEventToEmptyTargetFunction_(
eventHandler_,
*eventHandler_,
const_cast<std::string &>(type),
const_cast<folly::dynamic &>(payload)
);
@ -254,8 +244,8 @@ void FabricUIManager::completeRoot(int rootTag, const SharedShadowNodeUnsharedLi
}
}
void FabricUIManager::registerEventHandler(const EventHandler &eventHandler) {
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler: " << eventHandler << ")";
void FabricUIManager::registerEventHandler(std::shared_ptr<EventHandler> eventHandler) {
isLoggingEnabled && LOG(INFO) << "FabricUIManager::registerEventHandler(eventHandler: " << eventHandler.get() << ")";
eventHandler_ = eventHandler;
}

Просмотреть файл

@ -18,9 +18,8 @@
namespace facebook {
namespace react {
using DispatchEventToEmptyTargetFunction = void (EventHandler eventHandler, std::string type, folly::dynamic payload);
using DispatchEventToTargetFunction = void (EventHandler eventHandler, EventTarget eventTarget, std::string type, folly::dynamic payload);
using ReleaseEventHandlerFunction = void (EventHandler eventHandler);
using DispatchEventToEmptyTargetFunction = void (const EventHandler &eventHandler, std::string type, folly::dynamic payload);
using DispatchEventToTargetFunction = void (const EventHandler &eventHandler, EventTarget eventTarget, std::string type, folly::dynamic payload);
using ReleaseEventTargetFunction = void (EventTarget eventTarget);
class FabricUIManager {
@ -28,8 +27,6 @@ public:
#pragma mark - Native-facing Interface
~FabricUIManager();
void setComponentDescriptorRegistry(const SharedComponentDescriptorRegistry &componentDescriptorRegistry);
/*
@ -47,7 +44,6 @@ public:
*/
void setDispatchEventToEmptyTargetFunction(std::function<DispatchEventToEmptyTargetFunction> dispatchEventFunction);
void setDispatchEventToTargetFunction(std::function<DispatchEventToTargetFunction> dispatchEventFunction);
void setReleaseEventHandlerFunction(std::function<ReleaseEventHandlerFunction> releaseEventHandlerFunction);
void setReleaseEventTargetFunction(std::function<ReleaseEventTargetFunction> releaseEventTargetFunction);
#pragma mark - Native-facing Interface
@ -66,16 +62,15 @@ public:
SharedShadowNodeUnsharedList createChildSet(Tag rootTag);
void appendChildToSet(const SharedShadowNodeUnsharedList &childSet, const SharedShadowNode &childNode);
void completeRoot(Tag rootTag, const SharedShadowNodeUnsharedList &childSet);
void registerEventHandler(const EventHandler &eventHandler);
void registerEventHandler(std::shared_ptr<EventHandler> eventHandler);
private:
SharedComponentDescriptorRegistry componentDescriptorRegistry_;
UIManagerDelegate *delegate_;
EventHandler eventHandler_;
std::shared_ptr<EventHandler> eventHandler_;
std::function<DispatchEventToEmptyTargetFunction> dispatchEventToEmptyTargetFunction_;
std::function<DispatchEventToTargetFunction> dispatchEventToTargetFunction_;
std::function<ReleaseEventHandlerFunction> releaseEventHandlerFunction_;
std::function<ReleaseEventTargetFunction> releaseEventTargetFunction_;
};