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-09-14 05:56:00 +00:00
|
|
|
EventEmitter::EventEmitter(
|
|
|
|
SharedEventTarget eventTarget,
|
|
|
|
Tag tag,
|
|
|
|
WeakEventDispatcher eventDispatcher
|
|
|
|
):
|
|
|
|
eventTarget_(std::move(eventTarget)),
|
2018-08-27 14:21:24 +00:00
|
|
|
tag_(tag),
|
2018-09-14 05:56:00 +00:00
|
|
|
eventDispatcher_(std::move(eventDispatcher)) {}
|
2018-08-27 14:21:24 +00:00
|
|
|
|
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-09-14 05:56:06 +00:00
|
|
|
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-08-27 14:21:24 +00:00
|
|
|
eventDispatcher->dispatchEvent(
|
|
|
|
RawEvent(
|
|
|
|
normalizeEventType(type),
|
|
|
|
extendedPayload,
|
2018-09-14 05:56:06 +00:00
|
|
|
eventTarget_
|
2018-08-27 14:21:24 +00:00
|
|
|
),
|
|
|
|
priority
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventEmitter::setEnabled(bool enabled) const {
|
2018-09-14 05:56:02 +00:00
|
|
|
bool alreadyEnabled = eventTarget_ != nullptr;
|
|
|
|
if (enabled == alreadyEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-14 05:56:00 +00:00
|
|
|
if (!enabled) {
|
2018-09-14 05:56:02 +00:00
|
|
|
eventTarget_.reset();
|
2018-09-14 05:56:00 +00:00
|
|
|
}
|
2018-08-27 14:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EventEmitter::getEnabled() const {
|
2018-09-14 05:56:02 +00:00
|
|
|
return eventTarget_ != nullptr;
|
2018-06-01 16:36:25 +00:00
|
|
|
}
|
|
|
|
|
2018-05-22 22:48:19 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|