diff --git a/ReactCommon/fabric/events/EventDispatcher.cpp b/ReactCommon/fabric/events/EventDispatcher.cpp new file mode 100644 index 000000000..68df47079 --- /dev/null +++ b/ReactCommon/fabric/events/EventDispatcher.cpp @@ -0,0 +1,60 @@ +/** + * 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 "EventDispatcher.h" + +#include "UnbatchedEventQueue.h" +#include "BatchedEventQueue.h" + +#define REACT_FABRIC_SYNC_EVENT_DISPATCHING_DISABLED + +namespace facebook { +namespace react { + +EventDispatcher::EventDispatcher( + const EventPipe &eventPipe, + const EventBeatFactory &synchonousEventBeatFactory, + const EventBeatFactory &asynchonousEventBeatFactory +) { + // Synchronous/Unbatched + eventQueues_[(int)EventPriority::SynchronousUnbatched] = + std::make_unique(eventPipe, synchonousEventBeatFactory()); + + // Synchronous/Batched + eventQueues_[(int)EventPriority::SynchronousBatched] = + std::make_unique(eventPipe, synchonousEventBeatFactory()); + + // Asynchronous/Unbatched + eventQueues_[(int)EventPriority::AsynchronousUnbatched] = + std::make_unique(eventPipe, asynchonousEventBeatFactory()); + + // Asynchronous/Batched + eventQueues_[(int)EventPriority::AsynchronousBatched] = + std::make_unique(eventPipe, asynchonousEventBeatFactory()); +} + +void EventDispatcher::dispatchEvent( + const RawEvent &rawEvent, + EventPriority priority +) const { +#ifdef REACT_FABRIC_SYNC_EVENT_DISPATCHING_DISABLED + // Synchronous dispatch works, but JavaScript interop layer does not have + // proper synchonization yet and it crashes. + if (priority == EventPriority::SynchronousUnbatched) { + priority = EventPriority::AsynchronousUnbatched; + } + + if (priority == EventPriority::SynchronousBatched) { + priority = EventPriority::AsynchronousBatched; + } +#endif + + eventQueues_[(int)priority]->enqueueEvent(rawEvent); +} + +} // namespace react +} // namespace facebook diff --git a/ReactCommon/fabric/events/EventDispatcher.h b/ReactCommon/fabric/events/EventDispatcher.h index d317e65a4..8a052c547 100644 --- a/ReactCommon/fabric/events/EventDispatcher.h +++ b/ReactCommon/fabric/events/EventDispatcher.h @@ -8,37 +8,40 @@ #include +#include +#include #include -#include +#include namespace facebook { namespace react { class EventDispatcher; - using SharedEventDispatcher = std::shared_ptr; /* - * Abstract class that represent event-delivery infrastructure. - * Particular `EventEmitter` clases use an object of this class to invoke - * events. + * Represents event-delivery infrastructure. + * Particular `EventEmitter` clases use this for sending events. */ class EventDispatcher { public: + EventDispatcher( + const EventPipe &eventPipe, + const EventBeatFactory &synchonousEventBeatFactory, + const EventBeatFactory &asynchonousEventBeatFactory + ); /* - * Dispatches "raw" event using some event-delivery infrastructure. + * Dispatches a raw event with given priority using event-delivery pipe. */ - virtual void dispatchEvent( - const EventTarget &eventTarget, - const std::string &type, - const folly::dynamic &payload, - const EventPriority &priority - ) const = 0; - - virtual void releaseEventTarget(const EventTarget &eventTarget) const = 0; + void dispatchEvent( + const RawEvent &rawEvent, + EventPriority priority + ) const; +private: + std::array, 4> eventQueues_; }; } // namespace react diff --git a/ReactCommon/fabric/uimanager/Scheduler.h b/ReactCommon/fabric/uimanager/Scheduler.h index f603b5778..ce7ccfa41 100644 --- a/ReactCommon/fabric/uimanager/Scheduler.h +++ b/ReactCommon/fabric/uimanager/Scheduler.h @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include diff --git a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp deleted file mode 100644 index f26149932..000000000 --- a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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 "SchedulerEventDispatcher.h" - -namespace facebook { -namespace react { - -// 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) { - std::string prefixedType = type; - prefixedType[0] = toupper(prefixedType[0]); - prefixedType.insert(0, "top"); - return prefixedType; -} - -void SchedulerEventDispatcher::setUIManager(std::shared_ptr uiManager) const { - uiManager_ = uiManager; -} - -void SchedulerEventDispatcher::dispatchEvent( - const EventTarget &eventTarget, - const std::string &type, - const folly::dynamic &payload, - const EventPriority &priority -) const { - if (!uiManager_) { - return; - } - // TODO: Schedule the event based on priority. - uiManager_->dispatchEventToTarget(eventTarget, normalizeEventType(type), payload); -} - -void SchedulerEventDispatcher::releaseEventTarget(const EventTarget &eventTarget) const { - if (!uiManager_) { - return; - } - // TODO(shergin): This needs to move to the destructor of EventEmitter. For now we'll leak. - // uiManager_->releaseEventTarget(eventTarget); -} - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h b/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h deleted file mode 100644 index 4ce85faef..000000000 --- a/ReactCommon/fabric/uimanager/SchedulerEventDispatcher.h +++ /dev/null @@ -1,51 +0,0 @@ -/** - * 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. - */ - -#pragma once - -#include -#include -#include -#include - -namespace facebook { -namespace react { - -class SchedulerEventDispatcher; - -using SharedSchedulerEventDispatcher = std::shared_ptr; - -/* - * Concrete EventDispatcher. - */ -class SchedulerEventDispatcher final: - public EventDispatcher { - -public: - - void setUIManager(std::shared_ptr uiManager) const; - -#pragma mark - EventDispatcher - - void dispatchEvent( - const EventTarget &eventTarget, - const std::string &type, - const folly::dynamic &payload, - const EventPriority &priority - ) const override; - - - void releaseEventTarget(const EventTarget &eventTarget) const override; - -private: - - // TODO: consider using std::weak_ptr<> instead for better memory management. - mutable std::shared_ptr uiManager_; -}; - -} // namespace react -} // namespace facebook