Valentin Shergin beb3fcda34 Fabric: The first version of event dispatching pipeline
Summary:
This is the first attempt to implement some base part of event dispatching pipeline from end-to-end.
Even when it is working, all this is still incomplete and generally up in the air. We are still messing proper implementation of event queue, priority, and synchronization of react reconciliation process with event scheduling.

Reviewed By: fkgozali

Differential Revision: D8212271

fbshipit-source-id: 92f9427d14726441c70ffff294ac95eeb004152a
2018-06-01 09:37:46 -07:00

71 lines
1.7 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 "EventHandlers.h"
#include <folly/dynamic.h>
namespace facebook {
namespace react {
EventHandlers::EventHandlers(const InstanceHandle &instanceHandle, const Tag &tag, const SharedEventDispatcher &eventDispatcher):
instanceHandle_(instanceHandle),
tag_(tag),
eventDispatcher_(eventDispatcher) {}
EventHandlers::~EventHandlers() {
releaseEventTargetIfNeeded();
}
void EventHandlers::dispatchEvent(
const std::string &type,
const folly::dynamic &payload,
const EventPriority &priority
) const {
auto &&eventDispatcher = eventDispatcher_.lock();
if (!eventDispatcher) {
return;
}
createEventTargetIfNeeded();
// 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);
}
void EventHandlers::createEventTargetIfNeeded() const {
std::lock_guard<std::mutex> lock(mutex_);
if (eventTarget_) {
return;
}
auto &&eventDispatcher = eventDispatcher_.lock();
assert(eventDispatcher);
eventTarget_ = eventDispatcher->createEventTarget(instanceHandle_);
}
void EventHandlers::releaseEventTargetIfNeeded() const {
std::lock_guard<std::mutex> lock(mutex_);
if (!eventTarget_) {
return;
}
auto &&eventDispatcher = eventDispatcher_.lock();
assert(eventDispatcher);
eventDispatcher->releaseEventTarget(eventTarget_);
}
} // namespace react
} // namespace facebook