Make PerformanceObserver report correctly supported performance entry types (#39951)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39951

## Changelog:
[Internal] -

This makes PerformanceObserver API more robust in regards of telling which exactly types of the performance entries are supported.

At this point, we either tell that we support mark/measure/event ones on the New Architecture, or none otherwise. The source of truth of this information should be on the native side.

Reviewed By: rubennorte

Differential Revision: D50010982

fbshipit-source-id: ad7bce279a58eac232b7c26f8d8c2bd1cde51e3c
This commit is contained in:
Ruslan Shestopalyuk
2023-10-06 09:45:42 -07:00
committed by Facebook GitHub Bot
parent 239079e457
commit cc2791eb86
5 changed files with 43 additions and 3 deletions
@@ -107,4 +107,13 @@ std::vector<RawPerformanceEntry> NativePerformanceObserver::getEntries(
entryName ? entryName->c_str() : nullptr);
}
std::vector<RawPerformanceEntryType>
NativePerformanceObserver::getSupportedPerformanceEntryTypes(jsi::Runtime& rt) {
return {
static_cast<RawPerformanceEntryType>(PerformanceEntryType::MARK),
static_cast<RawPerformanceEntryType>(PerformanceEntryType::MEASURE),
static_cast<RawPerformanceEntryType>(PerformanceEntryType::EVENT),
};
}
} // namespace facebook::react
@@ -18,9 +18,11 @@ class PerformanceEntryReporter;
#pragma mark - Structs
using RawPerformanceEntryType = int32_t;
using RawPerformanceEntry = NativePerformanceObserverCxxBaseRawPerformanceEntry<
std::string,
int32_t,
RawPerformanceEntryType,
double,
double,
// For "event" entries only:
@@ -32,7 +34,7 @@ template <>
struct Bridging<RawPerformanceEntry>
: NativePerformanceObserverCxxBaseRawPerformanceEntryBridging<
std::string,
int32_t,
RawPerformanceEntryType,
double,
double,
std::optional<double>,
@@ -94,6 +96,9 @@ class NativePerformanceObserver
std::optional<int32_t> entryType,
std::optional<std::string> entryName);
std::vector<RawPerformanceEntryType> getSupportedPerformanceEntryTypes(
jsi::Runtime& rt);
private:
};
@@ -53,6 +53,7 @@ export interface Spec extends TurboModule {
entryType?: RawPerformanceEntryType,
entryName?: string,
) => $ReadOnlyArray<RawPerformanceEntry>;
+getSupportedPerformanceEntryTypes: () => $ReadOnlyArray<RawPerformanceEntryType>;
}
export default (TurboModuleRegistry.get<Spec>(
@@ -16,6 +16,7 @@ import {PerformanceEntry} from './PerformanceEntry';
import {
performanceEntryTypeToRaw,
rawToPerformanceEntry,
rawToPerformanceEntryType,
} from './RawPerformanceEntry';
export type PerformanceEntryList = $ReadOnlyArray<PerformanceEntry>;
@@ -129,6 +130,21 @@ function applyDurationThresholds() {
}
}
function getSupportedPerformanceEntryTypes(): $ReadOnlyArray<PerformanceEntryType> {
if (!NativePerformanceObserver) {
return Object.freeze([]);
}
if (!NativePerformanceObserver.getSupportedPerformanceEntryTypes) {
// fallback if getSupportedPerformanceEntryTypes is not defined on native side
return Object.freeze(['mark', 'measure', 'event']);
}
return Object.freeze(
NativePerformanceObserver.getSupportedPerformanceEntryTypes().map(
rawToPerformanceEntryType,
),
);
}
/**
* Implementation of the PerformanceObserver interface for RN,
* corresponding to the standard in https://www.w3.org/TR/performance-timeline/
@@ -294,7 +310,7 @@ export default class PerformanceObserver {
}
static supportedEntryTypes: $ReadOnlyArray<PerformanceEntryType> =
Object.freeze(['mark', 'measure', 'event']);
getSupportedPerformanceEntryTypes();
}
// As a Set union, except if value exists in both, we take minimum
@@ -114,6 +114,15 @@ const NativePerformanceObserverMock: NativePerformanceObserver = {
(entryName == null || e.name === entryName),
);
},
getSupportedPerformanceEntryTypes:
(): $ReadOnlyArray<RawPerformanceEntryType> => {
return [
RawPerformanceEntryTypeValues.MARK,
RawPerformanceEntryTypeValues.MEASURE,
RawPerformanceEntryTypeValues.EVENT,
];
},
};
export default NativePerformanceObserverMock;