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); },