diff --git a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp index 93d09bc97ee932..206da95d6c269e 100644 --- a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp +++ b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.cpp @@ -107,4 +107,13 @@ std::vector NativePerformanceObserver::getEntries( entryName ? entryName->c_str() : nullptr); } +std::vector +NativePerformanceObserver::getSupportedPerformanceEntryTypes(jsi::Runtime& rt) { + return { + static_cast(PerformanceEntryType::MARK), + static_cast(PerformanceEntryType::MEASURE), + static_cast(PerformanceEntryType::EVENT), + }; +} + } // namespace facebook::react diff --git a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.h b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.h index 257b0513e1c5ca..bc55f2476d2132 100644 --- a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.h +++ b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.h @@ -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 : NativePerformanceObserverCxxBaseRawPerformanceEntryBridging< std::string, - int32_t, + RawPerformanceEntryType, double, double, std::optional, @@ -94,6 +96,9 @@ class NativePerformanceObserver std::optional entryType, std::optional entryName); + std::vector getSupportedPerformanceEntryTypes( + jsi::Runtime& rt); + private: }; diff --git a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.js b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.js index e6e106e8ccea17..6db3be05a911a9 100644 --- a/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.js +++ b/packages/react-native/Libraries/WebPerformance/NativePerformanceObserver.js @@ -53,6 +53,7 @@ export interface Spec extends TurboModule { entryType?: RawPerformanceEntryType, entryName?: string, ) => $ReadOnlyArray; + +getSupportedPerformanceEntryTypes: () => $ReadOnlyArray; } export default (TurboModuleRegistry.get( diff --git a/packages/react-native/Libraries/WebPerformance/PerformanceObserver.js b/packages/react-native/Libraries/WebPerformance/PerformanceObserver.js index 776c9f0ced1e60..89d6e483d91da4 100644 --- a/packages/react-native/Libraries/WebPerformance/PerformanceObserver.js +++ b/packages/react-native/Libraries/WebPerformance/PerformanceObserver.js @@ -16,6 +16,7 @@ import {PerformanceEntry} from './PerformanceEntry'; import { performanceEntryTypeToRaw, rawToPerformanceEntry, + rawToPerformanceEntryType, } from './RawPerformanceEntry'; export type PerformanceEntryList = $ReadOnlyArray; @@ -129,6 +130,21 @@ function applyDurationThresholds() { } } +function getSupportedPerformanceEntryTypes(): $ReadOnlyArray { + 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 = - Object.freeze(['mark', 'measure', 'event']); + getSupportedPerformanceEntryTypes(); } // As a Set union, except if value exists in both, we take minimum diff --git a/packages/react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js b/packages/react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js index dbf6eeebc12b15..dd0fd1acb7f537 100644 --- a/packages/react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js +++ b/packages/react-native/Libraries/WebPerformance/__mocks__/NativePerformanceObserver.js @@ -114,6 +114,15 @@ const NativePerformanceObserverMock: NativePerformanceObserver = { (entryName == null || e.name === entryName), ); }, + + getSupportedPerformanceEntryTypes: + (): $ReadOnlyArray => { + return [ + RawPerformanceEntryTypeValues.MARK, + RawPerformanceEntryTypeValues.MEASURE, + RawPerformanceEntryTypeValues.EVENT, + ]; + }, }; export default NativePerformanceObserverMock;