Files
react-native/ReactCommon/fabric/core/events/EventEmitter.cpp
Kevin Gozali 2ca4770011 fabric: send events via JS thread
Summary: Calling the event emitters on the main thread seems to be problematic, so let's dispatch it via the JS thread. This requires some changes to make "eventTarget" single-use because otherwise the binding would need to synchronize the actual JS call with the act of releasing the target.

Reviewed By: shergin

Differential Revision: D8375291

fbshipit-source-id: bd2b42731176ae209f4a19c232309c163fb1c01b
2018-06-15 11:02:17 -07:00

51 lines
1.3 KiB
C++

/**
* Copyright (c) 2015-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "EventEmitter.h"
#include <folly/dynamic.h>
namespace facebook {
namespace react {
EventEmitter::EventEmitter(const InstanceHandle &instanceHandle, const Tag &tag, const SharedEventDispatcher &eventDispatcher):
instanceHandle_(instanceHandle),
tag_(tag),
eventDispatcher_(eventDispatcher) {}
EventEmitter::~EventEmitter() {}
void EventEmitter::dispatchEvent(
const std::string &type,
const folly::dynamic &payload,
const EventPriority &priority
) const {
auto &&eventDispatcher = eventDispatcher_.lock();
if (!eventDispatcher) {
return;
}
EventTarget eventTarget = createEventTarget();
// Mixing `target` into `payload`.
assert(payload.isObject());
folly::dynamic extendedPayload = folly::dynamic::object("target", tag_);
extendedPayload.merge_patch(payload);
// TODO(T29610783): Reconsider using dynamic dispatch here.
eventDispatcher->dispatchEvent(eventTarget, type, extendedPayload, priority);
}
EventTarget EventEmitter::createEventTarget() const {
auto &&eventDispatcher = eventDispatcher_.lock();
assert(eventDispatcher);
return eventDispatcher->createEventTarget(instanceHandle_);
}
} // namespace react
} // namespace facebook