Skip to content

Commit 727caca

Browse files
rubennortefacebook-github-bot
authored andcommitted
Set up modern performance APIs if the native module is available (#53431)
Summary: Pull Request resolved: #53431 Changelog: [internal] This renames `setUpPerformanceObserver` as `setUpPerformanceModern` and removes the need to call it manually. If the native module is defined, we define the whole new API. Reviewed By: javache Differential Revision: D80803626 fbshipit-source-id: ef41cb9aa959ee898d32724c102d7597e6bee84e
1 parent 1716b3c commit 727caca

File tree

7 files changed

+11
-22
lines changed

7 files changed

+11
-22
lines changed

packages/react-native/Libraries/Core/setUpPerformance.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,17 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict
7+
* @flow strict-local
88
* @format
99
*/
1010

11+
import setUpPerformanceModern from '../../src/private/setup/setUpPerformanceModern';
1112
import NativePerformance from '../../src/private/webapis/performance/specs/NativePerformance';
1213

1314
// In case if the native implementation of the Performance API is available, use it,
1415
// otherwise fall back to the legacy/default one, which only defines 'Performance.now()'
1516
if (NativePerformance) {
16-
const Performance =
17-
require('../../src/private/webapis/performance/Performance').default;
18-
// $FlowExpectedError[cannot-write]
19-
global.performance = new Performance();
17+
setUpPerformanceModern();
2018
} else {
2119
if (!global.performance) {
2220
// $FlowExpectedError[cannot-write]

packages/react-native/src/private/setup/setUpPerformanceObserver.js renamed to packages/react-native/src/private/setup/setUpPerformanceModern.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
1212

1313
let initialized = false;
1414

15-
export default function setUpPerformanceObserver() {
15+
export default function setUpPerformanceModern() {
1616
if (initialized) {
1717
return;
1818
}
1919

2020
initialized = true;
2121

22+
const Performance = require('../webapis/performance/Performance').default;
23+
24+
// We don't use `polyfillGlobal` to define this lazily because the
25+
// `performance` object is always accessed.
26+
// $FlowExpectedError[cannot-write]
27+
global.performance = new Performance();
28+
2229
polyfillGlobal(
2330
'EventCounts',
2431
() => require('../webapis/performance/EventTiming').EventCounts_public,

packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,9 @@ import * as Fantom from '@react-native/fantom';
1515
import nullthrows from 'nullthrows';
1616
import {useState} from 'react';
1717
import {Text, View} from 'react-native';
18-
import setUpPerformanceObserver from 'react-native/src/private/setup/setUpPerformanceObserver';
1918

2019
const NativePerformance = nullthrows(MaybeNativePerformance);
2120

22-
setUpPerformanceObserver();
23-
2421
function sleep(ms: number) {
2522
const end = performance.now() + ms;
2623
while (performance.now() < end) {}

packages/react-native/src/private/webapis/performance/__tests__/LongTasksAPI-itest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1313
import type {PerformanceObserverCallbackOptions} from '../PerformanceObserver';
1414

1515
import * as Fantom from '@react-native/fantom';
16-
import setUpPerformanceObserver from 'react-native/src/private/setup/setUpPerformanceObserver';
17-
18-
setUpPerformanceObserver();
1916

2017
function ensurePerformanceLongTaskTiming(
2118
value: mixed,

packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13-
import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver';
14-
15-
setUpPerformanceObserver();
16-
1713
describe('Performance', () => {
1814
it('does NOT allow creating instances of Performance directly', () => {
1915
expect(() => {

packages/react-native/src/private/webapis/performance/__tests__/PerformanceObserver-itest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,9 @@
1010

1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

13-
import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver';
1413
import {PerformanceObserverEntryList_public} from '../PerformanceObserver';
1514
import * as Fantom from '@react-native/fantom';
1615

17-
setUpPerformanceObserver();
18-
1916
describe('PerformanceObserver', () => {
2017
it('receives notifications for marks and measures', () => {
2118
const callback = jest.fn();

packages/react-native/src/private/webapis/performance/__tests__/UserTiming-itest.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,9 @@
1111
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1212

1313
import ensureInstance from '../../../__tests__/utilities/ensureInstance';
14-
import setUpPerformanceObserver from '../../../setup/setUpPerformanceObserver';
1514
import DOMException from '../../errors/DOMException';
1615
import * as Fantom from '@react-native/fantom';
1716

18-
setUpPerformanceObserver();
19-
2017
function getThrownError(fn: () => mixed): mixed {
2118
try {
2219
fn();

0 commit comments

Comments
 (0)