Make OpaqueNativeObserverHandle really opaque (#52054)

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

Changelog: [internal]

This makes `OpaqueNativeObserverHandle` really opaque and fixes the problems in the observer implementation caused by it.

Reviewed By: yungsters

Differential Revision: D76744094

fbshipit-source-id: a8b6fa43ee8a5ee9d15f0171a83fe0badd46d9c3
This commit is contained in:
Rubén Norte authored and Facebook GitHub Bot committed 2025-06-17 02:24:22 -07:00
1 parent 98fdc965b9
commit 4556bdf4fc
3 files changed
+30 -25

No files matched your search

@@ -23,6 +23,7 @@ import {
} from './internals/RawPerformanceEntry';
import {warnNoNativePerformance} from './internals/Utilities';
import NativePerformance from './specs/NativePerformance';
import nullthrows from 'nullthrows';
export {PerformanceEntry} from './PerformanceEntry';
@@ -130,14 +131,16 @@ export class PerformanceObserver {
this.#nativeObserverHandle = this.#createNativeObserver();
}
const observerHandle = nullthrows(this.#nativeObserverHandle);
if (options.entryTypes) {
this.#type = 'multiple';
NativePerformance.observe?.(this.#nativeObserverHandle, {
NativePerformance.observe?.(observerHandle, {
entryTypes: options.entryTypes.map(performanceEntryTypeToRaw),
});
} else if (options.type) {
this.#type = 'single';
NativePerformance.observe?.(this.#nativeObserverHandle, {
NativePerformance.observe?.(observerHandle, {
type: performanceEntryTypeToRaw(options.type),
buffered: options.buffered,
durationThreshold: options.durationThreshold,
@@ -158,37 +161,38 @@ export class PerformanceObserver {
NativePerformance.disconnect(this.#nativeObserverHandle);
}
#createNativeObserver(): OpaqueNativeObserverHandle {
#createNativeObserver(): OpaqueNativeObserverHandle | null {
if (!NativePerformance || !NativePerformance.createObserver) {
warnNoNativePerformance();
return;
return null;
}
this.#calledAtLeastOnce = false;
return NativePerformance.createObserver(() => {
const rawEntries = NativePerformance.takeRecords?.(
this.#nativeObserverHandle,
true, // sort records
);
if (!rawEntries) {
return;
}
const observerHandle: OpaqueNativeObserverHandle =
NativePerformance.createObserver(() => {
const rawEntries = NativePerformance.takeRecords?.(
observerHandle,
true, // sort records
);
if (!rawEntries) {
return;
}
const entries = rawEntries.map(rawToPerformanceEntry);
const entryList = new PerformanceObserverEntryList(entries);
const entries = rawEntries.map(rawToPerformanceEntry);
const entryList = new PerformanceObserverEntryList(entries);
let droppedEntriesCount = 0;
if (!this.#calledAtLeastOnce) {
droppedEntriesCount =
NativePerformance.getDroppedEntriesCount?.(
this.#nativeObserverHandle,
) ?? 0;
this.#calledAtLeastOnce = true;
}
let droppedEntriesCount = 0;
if (!this.#calledAtLeastOnce) {
droppedEntriesCount =
NativePerformance.getDroppedEntriesCount?.(observerHandle) ?? 0;
this.#calledAtLeastOnce = true;
}
this.#callback(entryList, this, {droppedEntriesCount});
});
this.#callback(entryList, this, {droppedEntriesCount});
});
return observerHandle;
}
#validateObserveOptions(options: PerformanceObserverInit): void {
@@ -39,7 +39,7 @@ export type RawPerformanceEntry = {
responseStatus?: number,
};
export type OpaqueNativeObserverHandle = mixed;
export opaque type OpaqueNativeObserverHandle = mixed;
export type NativeBatchedObserverCallback = () => void;
export type NativePerformanceMarkResult = number;
@@ -179,6 +179,7 @@ const NativePerformanceMock = {
createObserver: (
callback: NativeBatchedObserverCallback,
): OpaqueNativeObserverHandle => {
// $FlowExpectedError[incompatible-return]
return createMockObserver(callback);
},