Skip to content

Commit 9693a2b

Browse files
huntiemeta-codesync[bot]
authored andcommitted
Support parsing of detail arg from console.timeStamp (#54262)
Summary: Pull Request resolved: #54262 Similar to `performance.measure()`, Chrome is adding an optional `detail` arg to `console.timeStamp`. Here we implement this for React Native, by direct passing to the `"TimeStamp"` trace event args. [`console.timeStamp()`](https://developer.mozilla.org/en-US/docs/Web/API/console/timeStamp_static) remains an experimental, non-standard API. Changelog: [Internal] Reviewed By: hoxyq Differential Revision: D85437162 fbshipit-source-id: 36f5f6207cf205df5a216bde95013ea9540fc082
1 parent e8fcde5 commit 9693a2b

File tree

7 files changed

+53
-4
lines changed

7 files changed

+53
-4
lines changed

packages/polyfills/__tests__/consoleTimeStamp-itest.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe('console.timeStamp()', () => {
2626

2727
it("doesn't throw when additional arguments are specified", () => {
2828
expect(() =>
29-
console.timeStamp('label', 100, 500, 'Track', 'Group', 'error'),
29+
console.timeStamp('label', 100, 500, 'Track', 'Group', 'error', {
30+
tooltipText: 'Image processing failed',
31+
}),
3032
).not.toThrow();
3133
});
3234

packages/react-native/ReactCommon/jsinspector-modern/RuntimeTargetConsole.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,18 @@ void consoleTimeStamp(
467467
}
468468
}
469469

470+
std::optional<folly::dynamic> detail;
471+
if (performanceTracer.isTracing() && argumentsCount >= 7) {
472+
const jsi::Value& detailArgument = arguments[6];
473+
if (detailArgument.isObject()) {
474+
detail =
475+
tracing::getConsoleTimeStampDetailFromObject(runtime, detailArgument);
476+
}
477+
}
478+
470479
if (performanceTracer.isTracing()) {
471480
performanceTracer.reportTimeStamp(
472-
label, start, end, trackName, trackGroup, color);
481+
label, start, end, trackName, trackGroup, color, std::move(detail));
473482
}
474483

475484
if (ReactPerfettoLogger::isTracing()) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#include "ConsoleTimeStamp.h"
9+
10+
namespace facebook::react::jsinspector_modern::tracing {
11+
12+
std::optional<folly::dynamic> getConsoleTimeStampDetailFromObject(
13+
jsi::Runtime& runtime,
14+
const jsi::Value& detailValue) {
15+
try {
16+
return jsi::dynamicFromValue(runtime, detailValue);
17+
} catch (jsi::JSIException&) {
18+
return std::nullopt;
19+
}
20+
}
21+
22+
} // namespace facebook::react::jsinspector_modern::tracing

packages/react-native/ReactCommon/jsinspector-modern/tracing/ConsoleTimeStamp.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <jsi/JSIDynamic.h>
11+
#include <jsi/jsi.h>
1012
#include <react/timing/primitives.h>
1113

1214
#include <cassert>
@@ -96,4 +98,6 @@ inline std::optional<ConsoleTimeStampColor> getConsoleTimeStampColorFromString(c
9698
}
9799
};
98100

101+
std::optional<folly::dynamic> getConsoleTimeStampDetailFromObject(jsi::Runtime &runtime, const jsi::Value &detailValue);
102+
99103
}; // namespace facebook::react::jsinspector_modern::tracing

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ void PerformanceTracer::reportTimeStamp(
213213
std::optional<ConsoleTimeStampEntry> end,
214214
std::optional<std::string> trackName,
215215
std::optional<std::string> trackGroup,
216-
std::optional<ConsoleTimeStampColor> color) {
216+
std::optional<ConsoleTimeStampColor> color,
217+
std::optional<folly::dynamic> detail) {
217218
if (!tracingAtomic_) {
218219
return;
219220
}
@@ -231,6 +232,7 @@ void PerformanceTracer::reportTimeStamp(
231232
.trackName = std::move(trackName),
232233
.trackGroup = std::move(trackGroup),
233234
.color = std::move(color),
235+
.detail = std::move(detail),
234236
.threadId = getCurrentThreadId(),
235237
});
236238
}
@@ -651,6 +653,13 @@ void PerformanceTracer::enqueueTraceEventsFromPerformanceTracerEvent(
651653
if (event.color) {
652654
data["color"] = consoleTimeStampColorToString(*event.color);
653655
}
656+
if (event.detail) {
657+
folly::dynamic devtoolsDetail = folly::dynamic::object();
658+
for (const auto& [key, value] : event.detail->items()) {
659+
devtoolsDetail[key] = value;
660+
}
661+
data["devtools"] = folly::toJson(devtoolsDetail);
662+
}
654663

655664
events.emplace_back(
656665
TraceEvent{

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ class PerformanceTracer {
9696
std::optional<ConsoleTimeStampEntry> end = std::nullopt,
9797
std::optional<std::string> trackName = std::nullopt,
9898
std::optional<std::string> trackGroup = std::nullopt,
99-
std::optional<ConsoleTimeStampColor> color = std::nullopt);
99+
std::optional<ConsoleTimeStampColor> color = std::nullopt,
100+
std::optional<folly::dynamic> detail = std::nullopt);
100101

101102
/**
102103
* Record an Event Loop tick, which will be represented as an Event Loop task
@@ -252,6 +253,7 @@ class PerformanceTracer {
252253
std::optional<std::string> trackName;
253254
std::optional<std::string> trackGroup;
254255
std::optional<ConsoleTimeStampColor> color;
256+
std::optional<folly::dynamic> detail;
255257
ThreadId threadId;
256258
HighResTimeStamp createdAt = HighResTimeStamp::now();
257259
};

packages/react-native/flow/bom.js.flow

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ declare var console: {
7272
trackName?: string,
7373
trackGroup?: string,
7474
color?: DevToolsColor,
75+
detail?: {[string]: mixed},
7576
): void,
7677

7778
...

0 commit comments

Comments
 (0)