Fabric: Simplified relationship between RawEvent and EventEmitter

Summary: Instead of relying on explicit `RawEventDispatchable` function, we simply check the existence of the `weak_ptr` to `EventTarget`. This is efficient and sufficient because only an EventEmitter retains an associated EventTarget.

Reviewed By: mdvacca

Differential Revision: D9764858

fbshipit-source-id: 4ac25d925f189d0f8b9002e52388fd51629934a8
This commit is contained in:
Valentin Shergin 2018-09-13 22:56:06 -07:00 committed by Facebook Github Bot
parent b6e42517e5
commit 93dd790cad
5 changed files with 9 additions and 39 deletions

View File

@ -45,7 +45,7 @@ void EventEmitter::dispatchEvent(
const folly::dynamic &payload,
const EventPriority &priority
) const {
const auto &eventDispatcher = eventDispatcher_.lock();
auto eventDispatcher = eventDispatcher_.lock();
if (!eventDispatcher) {
return;
}
@ -55,21 +55,11 @@ void EventEmitter::dispatchEvent(
folly::dynamic extendedPayload = folly::dynamic::object("target", tag_);
extendedPayload.merge_patch(payload);
auto weakEventEmitter = std::weak_ptr<const EventEmitter> {shared_from_this()};
eventDispatcher->dispatchEvent(
RawEvent(
normalizeEventType(type),
extendedPayload,
eventTarget_,
[weakEventEmitter]() {
auto eventEmitter = weakEventEmitter.lock();
if (!eventEmitter) {
return false;
}
return eventEmitter->getEnabled();
}
eventTarget_
),
priority
);

View File

@ -33,8 +33,7 @@ using SharedEventEmitter = std::shared_ptr<const EventEmitter>;
* JSI maintains `WeakObject` object as long as we retain the `EventTarget`.
* All `EventTarget` instances must be deallocated before stopping JavaScript machine.
*/
class EventEmitter:
public std::enable_shared_from_this<EventEmitter> {
class EventEmitter {
/*
* We have to repeat `Tag` type definition here because `events` module does

View File

@ -39,10 +39,10 @@ void EventQueue::onBeat() const {
{
std::lock_guard<std::recursive_mutex> lock(EventEmitter::DispatchMutex());
for (const auto &event : queue) {
auto eventTarget = event.eventTarget.lock();
eventPipe_(
eventTarget && event.isDispatchable() ? eventTarget.get() : nullptr,
event.eventTarget.lock().get(),
event.type,
event.payload
);

View File

@ -13,17 +13,11 @@ namespace react {
RawEvent::RawEvent(
std::string type,
folly::dynamic payload,
WeakEventTarget eventTarget,
RawEventDispatchable isDispatchable
WeakEventTarget eventTarget
):
type(std::move(type)),
payload(std::move(payload)),
eventTarget(std::move(eventTarget)),
isDispatchable_(std::move(isDispatchable)) {}
bool RawEvent::isDispatchable() const {
return isDispatchable_();
}
eventTarget(std::move(eventTarget)) {}
} // namespace react
} // namespace facebook

View File

@ -15,33 +15,20 @@ namespace facebook {
namespace react {
/*
* Represents ready-to-dispatch event data.
* Represents ready-to-dispatch event object.
*/
class RawEvent {
public:
using RawEventDispatchable = std::function<bool()>;
RawEvent(
std::string type,
folly::dynamic payload,
WeakEventTarget eventTarget,
RawEventDispatchable isDispatchable
WeakEventTarget eventTarget
);
const std::string type;
const folly::dynamic payload;
const WeakEventTarget eventTarget;
/*
* Returns `true` if event can be dispatched to `eventTarget`.
* Events that associated with unmounted or deallocated `ShadowNode`s
* must not be dispatched.
*/
bool isDispatchable() const;
private:
const RawEventDispatchable isDispatchable_;
};
} // namespace react