API symmetry when passing PerformanceEntryType to/from native module

Summary:
[Changelog][Internal]

`NativePerformanceObserver` TurboModule API would get the type for performance entries as strings in one direction (`start/stopReporting`) and as integers in another direction (inside `RawPerformanceEntry`, for optimization on the native side).

This makes is symmetrical and consistent, all the conversions are now handled on the JS side.

Reviewed By: christophpurrer

Differential Revision: D43236466

fbshipit-source-id: 08e1b62df90e6d26a11577d6b6b1d91a6bce8339
This commit is contained in:
Ruslan Shestopalyuk 2023-02-13 10:44:06 -08:00 коммит произвёл Facebook GitHub Bot
Родитель e168db4c3b
Коммит aef7194996
4 изменённых файлов: 34 добавлений и 24 удалений

Просмотреть файл

@ -10,19 +10,6 @@
namespace facebook::react {
static PerformanceEntryType stringToPerformanceEntryType(
const std::string &entryType) {
if (entryType == "mark") {
return PerformanceEntryType::MARK;
} else if (entryType == "measure") {
return PerformanceEntryType::MEASURE;
} else if (entryType == "event") {
return PerformanceEntryType::EVENT;
} else {
return PerformanceEntryType::UNDEFINED;
}
}
NativePerformanceObserver::NativePerformanceObserver(
std::shared_ptr<CallInvoker> jsInvoker)
: NativePerformanceObserverCxxSpec(std::move(jsInvoker)) {
@ -35,16 +22,16 @@ NativePerformanceObserver::~NativePerformanceObserver() {
void NativePerformanceObserver::startReporting(
jsi::Runtime &rt,
std::string entryType) {
int32_t entryType) {
PerformanceEntryReporter::getInstance().startReporting(
stringToPerformanceEntryType(entryType));
static_cast<PerformanceEntryType>(entryType));
}
void NativePerformanceObserver::stopReporting(
jsi::Runtime &rt,
std::string entryType) {
int32_t entryType) {
PerformanceEntryReporter::getInstance().stopReporting(
stringToPerformanceEntryType(entryType));
static_cast<PerformanceEntryType>(entryType));
}
GetPendingEntriesResult NativePerformanceObserver::popPendingEntries(

Просмотреть файл

@ -59,9 +59,9 @@ class NativePerformanceObserver
NativePerformanceObserver(std::shared_ptr<CallInvoker> jsInvoker);
~NativePerformanceObserver();
void startReporting(jsi::Runtime &rt, std::string entryType);
void startReporting(jsi::Runtime &rt, int32_t entryType);
void stopReporting(jsi::Runtime &rt, std::string entryType);
void stopReporting(jsi::Runtime &rt, int32_t entryType);
GetPendingEntriesResult popPendingEntries(jsi::Runtime &rt);

Просмотреть файл

@ -38,8 +38,8 @@ export type GetPendingEntriesResult = {|
|};
export interface Spec extends TurboModule {
+startReporting: (entryType: string) => void;
+stopReporting: (entryType: string) => void;
+startReporting: (entryType: RawPerformanceEntryType) => void;
+stopReporting: (entryType: RawPerformanceEntryType) => void;
+popPendingEntries: () => GetPendingEntriesResult;
+setOnPerformanceEntryCallback: (callback?: () => void) => void;
}

Просмотреть файл

@ -33,7 +33,26 @@ function rawToPerformanceEntryType(
return 'event';
default:
throw new TypeError(
`unexpected performance entry type received: ${type}`,
`rawToPerformanceEntryType: unexpected performance entry type received: ${type}`,
);
}
}
function performanceEntryTypeToRaw(
type: PerformanceEntryType,
): RawPerformanceEntryType {
switch (type) {
case 'mark':
return RawPerformanceEntryTypeValues.MARK;
case 'measure':
return RawPerformanceEntryTypeValues.MEASURE;
case 'event':
return RawPerformanceEntryTypeValues.EVENT;
default:
// Verify exhaustive check with Flow
(type: empty);
throw new TypeError(
`performanceEntryTypeToRaw: unexpected performance entry type received: ${type}`,
);
}
}
@ -222,7 +241,9 @@ export default class PerformanceObserver {
: requestedEntryTypes;
for (const type of newEntryTypes) {
if (!observerCountPerEntryType.has(type)) {
NativePerformanceObserver.startReporting(type);
NativePerformanceObserver.startReporting(
performanceEntryTypeToRaw(type),
);
}
observerCountPerEntryType.set(
type,
@ -248,7 +269,9 @@ export default class PerformanceObserver {
observerCountPerEntryType.get(type) ?? 0;
if (numberOfObserversForThisType === 1) {
observerCountPerEntryType.delete(type);
NativePerformanceObserver.stopReporting(type);
NativePerformanceObserver.stopReporting(
performanceEntryTypeToRaw(type),
);
} else if (numberOfObserversForThisType !== 0) {
observerCountPerEntryType.set(type, numberOfObserversForThisType - 1);
}