Fabric: Rethinking of EventEmitter's role

Summary:
@public
Now, one of the main purposes of `EventEmitter`s is to create RawEvent instances for all specific event invocations.

Reviewed By: mdvacca

Differential Revision: D8886226

fbshipit-source-id: 82a489174efcda097887e70650a2038dc986d149
This commit is contained in:
Valentin Shergin 2018-08-27 07:21:24 -07:00 committed by Facebook Github Bot
parent 0dae893eec
commit b49c8add15
2 changed files with 61 additions and 21 deletions

View File

@ -9,22 +9,33 @@
#include <folly/dynamic.h> #include <folly/dynamic.h>
#include "RawEvent.h"
namespace facebook { namespace facebook {
namespace react { namespace react {
EventEmitter::EventEmitter(const EventTarget &eventTarget, const Tag &tag, const SharedEventDispatcher &eventDispatcher): // TODO(T29874519): Get rid of "top" prefix once and for all.
eventTarget_(eventTarget), /*
tag_(tag), * Capitalizes the first letter of the event type and adds "top" prefix
eventDispatcher_(eventDispatcher) { * (e.g. "layout" becames "topLayout").
*/
static std::string normalizeEventType(const std::string &type) {
std::string prefixedType = type;
prefixedType[0] = toupper(prefixedType[0]);
prefixedType.insert(0, "top");
return prefixedType;
} }
EventEmitter::~EventEmitter() { std::recursive_mutex &EventEmitter::DispatchMutex() {
auto &&eventDispatcher = eventDispatcher_.lock(); static std::recursive_mutex mutex;
if (eventDispatcher && eventTarget_) { return mutex;
eventDispatcher->releaseEventTarget(eventTarget_);
}
} }
EventEmitter::EventEmitter(const EventTarget &eventTarget, const Tag &tag, const std::shared_ptr<const EventDispatcher> &eventDispatcher):
eventTarget_(eventTarget),
tag_(tag),
eventDispatcher_(eventDispatcher) {}
void EventEmitter::dispatchEvent( void EventEmitter::dispatchEvent(
const std::string &type, const std::string &type,
const folly::dynamic &payload, const folly::dynamic &payload,
@ -35,15 +46,37 @@ void EventEmitter::dispatchEvent(
return; return;
} }
assert(eventTarget_ && "Attempted to dispatch an event without an eventTarget.");
// Mixing `target` into `payload`. // Mixing `target` into `payload`.
assert(payload.isObject()); assert(payload.isObject());
folly::dynamic extendedPayload = folly::dynamic::object("target", tag_); folly::dynamic extendedPayload = folly::dynamic::object("target", tag_);
extendedPayload.merge_patch(payload); extendedPayload.merge_patch(payload);
// TODO(T29610783): Reconsider using dynamic dispatch here. std::weak_ptr<const EventEmitter> weakEventEmitter = shared_from_this();
eventDispatcher->dispatchEvent(eventTarget_, type, extendedPayload, priority);
eventDispatcher->dispatchEvent(
RawEvent(
normalizeEventType(type),
extendedPayload,
eventTarget_,
[weakEventEmitter]() {
auto eventEmitter = weakEventEmitter.lock();
if (!eventEmitter) {
return false;
}
return eventEmitter->getEnabled();
}
),
priority
);
}
void EventEmitter::setEnabled(bool enabled) const {
enabled_ = enabled;
}
bool EventEmitter::getEnabled() const {
return enabled_;
} }
} // namespace react } // namespace react

View File

@ -24,10 +24,9 @@ using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
* Base class for all particular typed event handlers. * Base class for all particular typed event handlers.
* Stores `InstanceHandle` identifying a particular component and the pointer * Stores `InstanceHandle` identifying a particular component and the pointer
* to `EventDispatcher` which is responsible for delivering the event. * to `EventDispatcher` which is responsible for delivering the event.
*
* TODO: Reconsider naming of all event-related things.
*/ */
class EventEmitter { class EventEmitter:
public std::enable_shared_from_this<EventEmitter> {
/* /*
* We have to repeat `Tag` type definition here because `events` module does * We have to repeat `Tag` type definition here because `events` module does
@ -36,11 +35,19 @@ class EventEmitter {
using Tag = int32_t; using Tag = int32_t;
public: public:
EventEmitter(const EventTarget &eventTarget, const Tag &tag, const SharedEventDispatcher &eventDispatcher); static std::recursive_mutex &DispatchMutex();
virtual ~EventEmitter();
EventEmitter(const EventTarget &eventTarget, const Tag &tag, const std::shared_ptr<const EventDispatcher> &eventDispatcher);
virtual ~EventEmitter() = default;
/*
* Indicates that an event can be delivered to `eventTarget`.
* Callsite must acquire `DispatchMutex` to access those methods.
*/
void setEnabled(bool enabled) const;
bool getEnabled() const;
protected: protected:
/* /*
* Initates an event delivery process. * Initates an event delivery process.
* Is used by particular subclasses only. * Is used by particular subclasses only.
@ -52,10 +59,10 @@ protected:
) const; ) const;
private: private:
EventTarget eventTarget_;
mutable EventTarget eventTarget_ {nullptr};
Tag tag_; Tag tag_;
std::weak_ptr<const EventDispatcher> eventDispatcher_; std::weak_ptr<const EventDispatcher> eventDispatcher_;
mutable bool enabled_; // Protected by `DispatchMutex`.
}; };
} // namespace react } // namespace react