Skip to content

Commit

Permalink
Add an event start timestamp field into RawEvent
Browse files Browse the repository at this point in the history
Summary:
# Changelog:

[Internal] - 

This adds an (optional) event start time field into RawEvent, so that it can be threaded by the platform-specific code to tell where is the earliest point of the event start (depending on the platform).

For example, on Android platform it can be the point of time when the original MotionEvent was received.

Differential Revision: D64175467
  • Loading branch information
rshest authored and facebook-github-bot committed Oct 10, 2024
1 parent 0d6908f commit b98b55c
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ void EventDispatcher::dispatchEvent(RawEvent&& rawEvent) const {

auto eventLogger = eventLogger_.lock();
if (eventLogger != nullptr) {
rawEvent.loggingTag =
eventLogger->onEventStart(rawEvent.type, rawEvent.eventTarget);
rawEvent.loggingTag = eventLogger->onEventStart(
rawEvent.type, rawEvent.eventTarget, rawEvent.eventStartTimeStamp);
}
eventQueue_.enqueueEvent(std::move(rawEvent));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#pragma once

#include <react/renderer/core/EventTarget.h>
#include <react/timing/primitives.h>
#include <string_view>

namespace facebook::react {
Expand All @@ -30,7 +31,9 @@ class EventLogger {
*/
virtual EventTag onEventStart(
std::string_view name,
SharedEventTarget target) = 0;
SharedEventTarget target,
DOMHighResTimeStamp eventStartTimeStamp =
DOM_HIGH_RES_TIME_STAMP_UNSET) = 0;

/*
* Called when event starts getting dispatched (processed by the handlers, if
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <react/renderer/core/EventLogger.h>
#include <react/renderer/core/EventPayload.h>
#include <react/renderer/core/EventTarget.h>
#include <react/timing/primitives.h>

namespace facebook::react {

Expand Down Expand Up @@ -69,6 +70,11 @@ struct RawEvent {
SharedEventTarget eventTarget;
Category category;
EventTag loggingTag{0};

// The client may specify a platform-specific timestamp for the event start
// time, for example when MotionEvent was triggered on the Android native
// side.
DOMHighResTimeStamp eventStartTimeStamp{DOM_HIGH_RES_TIME_STAMP_UNSET};
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ EventPerformanceLogger::EventPerformanceLogger(

EventTag EventPerformanceLogger::onEventStart(
std::string_view name,
SharedEventTarget target) {
SharedEventTarget target,
DOMHighResTimeStamp eventStartTimeStamp) {
auto performanceEntryReporter = performanceEntryReporter_.lock();
if (performanceEntryReporter == nullptr) {
return EMPTY_EVENT_TAG;
Expand All @@ -119,7 +120,11 @@ EventTag EventPerformanceLogger::onEventStart(

auto eventTag = createEventTag();

auto timeStamp = performanceEntryReporter->getCurrentTimeStamp();
// The event start timestamp may be provided by the caller in order to
// specify the platform specific event start time.
auto timeStamp = eventStartTimeStamp == DOM_HIGH_RES_TIME_STAMP_UNSET
? performanceEntryReporter->getCurrentTimeStamp()
: eventStartTimeStamp;
{
std::lock_guard lock(eventsInFlightMutex_);
eventsInFlight_.emplace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ class EventPerformanceLogger : public EventLogger,

#pragma mark - EventLogger

EventTag onEventStart(std::string_view name, SharedEventTarget target)
override;
EventTag onEventStart(
std::string_view name,
SharedEventTarget target,
DOMHighResTimeStamp eventStartTimeStamp =
DOM_HIGH_RES_TIME_STAMP_UNSET) override;
void onEventProcessingStart(EventTag tag) override;
void onEventProcessingEnd(EventTag tag) override;

Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/ReactCommon/react/timing/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace facebook::react {
// not necessary in React Native.
using DOMHighResTimeStamp = double;

constexpr DOMHighResTimeStamp DOM_HIGH_RES_TIME_STAMP_UNSET = -1.0;

inline DOMHighResTimeStamp chronoToDOMHighResTimeStamp(
std::chrono::steady_clock::duration duration) {
return static_cast<std::chrono::duration<double, std::milli>>(duration)
Expand Down

0 comments on commit b98b55c

Please sign in to comment.