Files
react-native/ReactCommon/react/renderer/core/EventDispatcher.cpp
T
Ruslan Shestopalyuk 09ad0cc0c6 Implement reporting of events from native side to WebPerformance API (#35768)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/35768

Changelog: [Internal]

This implements native side mechanics for reporting user events timing to JS  (PerformanceObserver API).

See the standard for more details: https://www.w3.org/TR/event-timing/

The events are only logged when there are any active subscriptions (via `PerformanceObserver.observe`), also we only log "discrete events" (i.e. no likes of mouse move), so the overhead is non-existing.

There are two main metrics of interest for an event lifecycle:
* Time the event is spent in the queue, i.e. the time between it's created and dispatched
* Time that is spend in the event handler on the JS side (event dispatch), or processing time

Both of these are measured, and the corresponding fields are populated.

Reviewed By: sammy-SC

Differential Revision: D42294947

fbshipit-source-id: 4fd7938c04b942400befa4057d4929fb2763cee1
2023-01-03 11:11:37 -08:00

92 lines
3.0 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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 <cxxreact/JSExecutor.h>
#include <react/renderer/core/StateUpdate.h>
#include "EventLogger.h"
#include "BatchedEventQueue.h"
#include "RawEvent.h"
#include "UnbatchedEventQueue.h"
namespace facebook::react {
EventDispatcher::EventDispatcher(
EventQueueProcessor const &eventProcessor,
EventBeat::Factory const &synchonousEventBeatFactory,
EventBeat::Factory const &asynchonousEventBeatFactory,
EventBeat::SharedOwnerBox const &ownerBox)
: synchronousUnbatchedQueue_(std::make_unique<UnbatchedEventQueue>(
eventProcessor,
synchonousEventBeatFactory(ownerBox))),
synchronousBatchedQueue_(std::make_unique<BatchedEventQueue>(
eventProcessor,
synchonousEventBeatFactory(ownerBox))),
asynchronousUnbatchedQueue_(std::make_unique<UnbatchedEventQueue>(
eventProcessor,
asynchonousEventBeatFactory(ownerBox))),
asynchronousBatchedQueue_(std::make_unique<BatchedEventQueue>(
eventProcessor,
asynchonousEventBeatFactory(ownerBox))) {}
void EventDispatcher::dispatchEvent(RawEvent &&rawEvent, EventPriority priority)
const {
// Allows the event listener to interrupt default event dispatch
if (eventListeners_.willDispatchEvent(rawEvent)) {
return;
}
auto eventLogger = getEventLogger();
if (eventLogger != nullptr) {
rawEvent.loggingTag = eventLogger->onEventStart(rawEvent.type.c_str());
}
getEventQueue(priority).enqueueEvent(std::move(rawEvent));
}
void EventDispatcher::dispatchStateUpdate(
StateUpdate &&stateUpdate,
EventPriority priority) const {
getEventQueue(priority).enqueueStateUpdate(std::move(stateUpdate));
}
void EventDispatcher::dispatchUniqueEvent(RawEvent &&rawEvent) const {
// Allows the event listener to interrupt default event dispatch
if (eventListeners_.willDispatchEvent(rawEvent)) {
return;
}
asynchronousBatchedQueue_->enqueueUniqueEvent(std::move(rawEvent));
}
const EventQueue &EventDispatcher::getEventQueue(EventPriority priority) const {
switch (priority) {
case EventPriority::SynchronousUnbatched:
return *synchronousUnbatchedQueue_;
case EventPriority::SynchronousBatched:
return *synchronousBatchedQueue_;
case EventPriority::AsynchronousUnbatched:
return *asynchronousUnbatchedQueue_;
case EventPriority::AsynchronousBatched:
return *asynchronousBatchedQueue_;
}
}
void EventDispatcher::addListener(
const std::shared_ptr<EventListener const> &listener) const {
eventListeners_.addListener(listener);
}
/*
* Removes provided event listener to the event dispatcher.
*/
void EventDispatcher::removeListener(
const std::shared_ptr<EventListener const> &listener) const {
eventListeners_.removeListener(listener);
}
} // namespace facebook::react