2018-05-22 22:48:19 +00:00
|
|
|
/**
|
2018-09-11 22:27:47 +00:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-22 22:48:19 +00:00
|
|
|
*
|
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
|
|
|
*/
|
|
|
|
|
2018-06-09 20:02:55 +00:00
|
|
|
#include "EventEmitter.h"
|
2018-05-22 22:48:19 +00:00
|
|
|
|
|
|
|
#include <folly/dynamic.h>
|
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
#include "RawEvent.h"
|
|
|
|
|
2018-05-22 22:48:19 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
// TODO(T29874519): Get rid of "top" prefix once and for all.
|
|
|
|
/*
|
|
|
|
* Capitalizes the first letter of the event type and adds "top" prefix
|
|
|
|
* (e.g. "layout" becames "topLayout").
|
|
|
|
*/
|
|
|
|
static std::string normalizeEventType(const std::string &type) {
|
2018-09-10 23:33:48 +00:00
|
|
|
auto prefixedType = type;
|
2018-08-27 14:21:24 +00:00
|
|
|
prefixedType[0] = toupper(prefixedType[0]);
|
|
|
|
prefixedType.insert(0, "top");
|
|
|
|
return prefixedType;
|
2018-06-21 21:14:59 +00:00
|
|
|
}
|
2018-05-22 22:48:19 +00:00
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
std::recursive_mutex &EventEmitter::DispatchMutex() {
|
|
|
|
static std::recursive_mutex mutex;
|
|
|
|
return mutex;
|
2018-06-21 21:14:59 +00:00
|
|
|
}
|
2018-06-01 16:36:25 +00:00
|
|
|
|
2018-08-27 14:21:24 +00:00
|
|
|
EventEmitter::EventEmitter(const EventTarget &eventTarget, const Tag &tag, const std::shared_ptr<const EventDispatcher> &eventDispatcher):
|
|
|
|
eventTarget_(eventTarget),
|
|
|
|
tag_(tag),
|
|
|
|
eventDispatcher_(eventDispatcher) {}
|
|
|
|
|
2018-06-09 20:02:55 +00:00
|
|
|
void EventEmitter::dispatchEvent(
|
2018-06-01 16:36:19 +00:00
|
|
|
const std::string &type,
|
2018-05-22 22:48:19 +00:00
|
|
|
const folly::dynamic &payload,
|
|
|
|
const EventPriority &priority
|
|
|
|
) const {
|
2018-06-22 14:28:36 +00:00
|
|
|
const auto &eventDispatcher = eventDispatcher_.lock();
|
2018-05-22 22:48:19 +00:00
|
|
|
if (!eventDispatcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-06-01 16:36:19 +00:00
|
|
|
// Mixing `target` into `payload`.
|
|
|
|
assert(payload.isObject());
|
|
|
|
folly::dynamic extendedPayload = folly::dynamic::object("target", tag_);
|
|
|
|
extendedPayload.merge_patch(payload);
|
|
|
|
|
2018-09-10 23:33:48 +00:00
|
|
|
auto weakEventEmitter = std::weak_ptr<const EventEmitter> {shared_from_this()};
|
2018-08-27 14:21:24 +00:00
|
|
|
|
|
|
|
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_;
|
2018-06-01 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 22:48:19 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|