Skip to content

Commit

Permalink
Make PerformanceObserver report correctly supported performance entry…
Browse files Browse the repository at this point in the history
… types (facebook#39951)

Summary:

## 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.

Differential Revision: D50010982
  • Loading branch information
rshest authored and facebook-github-bot committed Oct 6, 2023
1 parent 9252099 commit de52c84
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -32,7 +34,7 @@ template <>
struct Bridging<RawPerformanceEntry>
: NativePerformanceObserverCxxBaseRawPerformanceEntryBridging<
std::string,
int32_t,
RawPerformanceEntryType,
double,
double,
std::optional<double>,
Expand Down Expand Up @@ -94,6 +96,9 @@ class NativePerformanceObserver
std::optional<int32_t> entryType,
std::optional<std::string> entryName);

std::vector<RawPerformanceEntryType> getSupportedPerformanceEntryTypes(
jsi::Runtime& rt);

private:
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export interface Spec extends TurboModule {
entryType?: RawPerformanceEntryType,
entryName?: string,
) => $ReadOnlyArray<RawPerformanceEntry>;
+getSupportedPerformanceEntryTypes: () => $ReadOnlyArray<RawPerformanceEntryType>;
}

export default (TurboModuleRegistry.get<Spec>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {PerformanceEntry} from './PerformanceEntry';
import {
performanceEntryTypeToRaw,
rawToPerformanceEntry,
rawToPerformanceEntryType,
} from './RawPerformanceEntry';

export type PerformanceEntryList = $ReadOnlyArray<PerformanceEntry>;
Expand Down Expand Up @@ -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/
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit de52c84

Please sign in to comment.