From 4556bdf4fc30fa2ad63909dd01df587b2f3b69a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Tue, 17 Jun 2025 02:24:22 -0700 Subject: [PATCH] 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 --- .../performance/PerformanceObserver.js | 52 ++++++++++--------- .../performance/specs/NativePerformance.js | 2 +- .../specs/__mocks__/NativePerformanceMock.js | 1 + 3 files changed, 30 insertions(+), 25 deletions(-) diff --git a/packages/react-native/src/private/webapis/performance/PerformanceObserver.js b/packages/react-native/src/private/webapis/performance/PerformanceObserver.js index 3dbfc349d24..6870d4a9f2a 100644 --- a/packages/react-native/src/private/webapis/performance/PerformanceObserver.js +++ b/packages/react-native/src/private/webapis/performance/PerformanceObserver.js @@ -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 { diff --git a/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js b/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js index 809037e8a73..18714050b15 100644 --- a/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js +++ b/packages/react-native/src/private/webapis/performance/specs/NativePerformance.js @@ -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; diff --git a/packages/react-native/src/private/webapis/performance/specs/__mocks__/NativePerformanceMock.js b/packages/react-native/src/private/webapis/performance/specs/__mocks__/NativePerformanceMock.js index c43a5c3a049..edab30a38d4 100644 --- a/packages/react-native/src/private/webapis/performance/specs/__mocks__/NativePerformanceMock.js +++ b/packages/react-native/src/private/webapis/performance/specs/__mocks__/NativePerformanceMock.js @@ -179,6 +179,7 @@ const NativePerformanceMock = { createObserver: ( callback: NativeBatchedObserverCallback, ): OpaqueNativeObserverHandle => { + // $FlowExpectedError[incompatible-return] return createMockObserver(callback); },