2018-05-22 15:48:19 -07:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-22 15:48:19 -07: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 13:02:55 -07:00
|
|
|
#include "EventEmitter.h"
|
2018-05-22 15:48:19 -07:00
|
|
|
|
|
|
|
#include <folly/dynamic.h>
|
2018-11-10 14:19:08 -08:00
|
|
|
#include <react/debug/SystraceSection.h>
|
2018-05-22 15:48:19 -07:00
|
|
|
|
2018-08-27 07:21:24 -07:00
|
|
|
#include "RawEvent.h"
|
|
|
|
|
2018-05-22 15:48:19 -07:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2018-08-27 07:21:24 -07:00
|
|
|
// TODO(T29874519): Get rid of "top" prefix once and for all.
|
|
|
|
/*
|
2018-10-02 15:02:07 -07:00
|
|
|
* Capitalizes the first letter of the event type and adds "top" prefix if
|
|
|
|
* necessary (e.g. "layout" becames "topLayout").
|
2018-08-27 07:21:24 -07:00
|
|
|
*/
|
|
|
|
static std::string normalizeEventType(const std::string &type) {
|
2018-09-10 16:33:48 -07:00
|
|
|
auto prefixedType = type;
|
2018-10-02 15:02:07 -07:00
|
|
|
if (type.find("top", 0) != 0) {
|
|
|
|
prefixedType.insert(0, "top");
|
|
|
|
prefixedType[3] = toupper(prefixedType[3]);
|
|
|
|
}
|
2018-08-27 07:21:24 -07:00
|
|
|
return prefixedType;
|
2018-06-21 14:14:59 -07:00
|
|
|
}
|
2018-05-22 15:48:19 -07:00
|
|
|
|
2018-08-27 07:21:24 -07:00
|
|
|
std::recursive_mutex &EventEmitter::DispatchMutex() {
|
|
|
|
static std::recursive_mutex mutex;
|
|
|
|
return mutex;
|
2018-06-21 14:14:59 -07:00
|
|
|
}
|
2018-06-01 09:36:25 -07:00
|
|
|
|
2018-09-13 22:56:00 -07:00
|
|
|
EventEmitter::EventEmitter(
|
2018-10-09 16:25:13 -07:00
|
|
|
SharedEventTarget eventTarget,
|
|
|
|
Tag tag,
|
|
|
|
WeakEventDispatcher eventDispatcher)
|
|
|
|
: eventTarget_(std::move(eventTarget)),
|
2018-10-16 12:26:48 -07:00
|
|
|
weakEventTarget_({}),
|
2018-10-09 16:25:13 -07:00
|
|
|
eventDispatcher_(std::move(eventDispatcher)) {}
|
2018-08-27 07:21:24 -07:00
|
|
|
|
2018-06-09 13:02:55 -07:00
|
|
|
void EventEmitter::dispatchEvent(
|
2018-10-09 16:25:13 -07:00
|
|
|
const std::string &type,
|
|
|
|
const folly::dynamic &payload,
|
|
|
|
const EventPriority &priority) const {
|
2018-11-08 16:55:12 -08:00
|
|
|
SystraceSection s("EventEmitter::dispatchEvent");
|
2018-11-27 20:58:28 -08:00
|
|
|
|
2018-09-13 22:56:06 -07:00
|
|
|
auto eventDispatcher = eventDispatcher_.lock();
|
2018-05-22 15:48:19 -07:00
|
|
|
if (!eventDispatcher) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-27 07:21:24 -07:00
|
|
|
eventDispatcher->dispatchEvent(
|
2018-11-27 20:58:28 -08:00
|
|
|
RawEvent(normalizeEventType(type), payload, eventTarget_), priority);
|
2018-08-27 07:21:24 -07:00
|
|
|
}
|
|
|
|
|
2018-11-09 11:01:33 -08:00
|
|
|
void EventEmitter::enable() const {
|
|
|
|
enableCounter_++;
|
|
|
|
toggleEventTargetOwnership_();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventEmitter::disable() const {
|
|
|
|
enableCounter_--;
|
|
|
|
toggleEventTargetOwnership_();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EventEmitter::toggleEventTargetOwnership_() const {
|
|
|
|
bool shouldBeRetained = enableCounter_ > 0;
|
|
|
|
bool alreadyBeRetained = eventTarget_ != nullptr;
|
|
|
|
if (shouldBeRetained == alreadyBeRetained) {
|
2018-09-13 22:56:02 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-11-09 11:01:33 -08:00
|
|
|
if (shouldBeRetained) {
|
2018-10-16 12:26:48 -07:00
|
|
|
eventTarget_ = weakEventTarget_.lock();
|
|
|
|
weakEventTarget_.reset();
|
|
|
|
} else {
|
|
|
|
weakEventTarget_ = eventTarget_;
|
2018-09-13 22:56:02 -07:00
|
|
|
eventTarget_.reset();
|
2018-09-13 22:56:00 -07:00
|
|
|
}
|
2018-08-27 07:21:24 -07:00
|
|
|
}
|
|
|
|
|
2018-05-22 15:48:19 -07:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|