Use by default new Performance API implementation if available

Summary:
## Changelog:
[Internal] -

Now that Performance API implementation is getting close to the final stage, it should be safe to replace the `global.performance` instance with the corresponding new implementation. This is done *only* if the actual native implementation is present, however (otherwise the behaviour will be exactly the same as before). Currently this is the case for all VR apps, Catalyst and very soon will be the case for fb4a/ios.

Reviewed By: cipolleschi

Differential Revision: D44057951

fbshipit-source-id: 7ccff4a4930175def69ef4d8a44335fbbd4a5df4
This commit is contained in:
Ruslan Shestopalyuk 2023-03-15 05:36:24 -07:00 коммит произвёл Facebook GitHub Bot
Родитель 102e40a88d
Коммит b89358263a
2 изменённых файлов: 24 добавлений и 30 удалений

Просмотреть файл

@ -8,18 +8,29 @@
* @format
*/
if (!global.performance) {
global.performance = ({}: {now?: () => number});
}
import NativePerformance from '../WebPerformance/NativePerformance';
import Performance from '../WebPerformance/Performance';
/**
* Returns a double, measured in milliseconds.
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
*/
if (typeof global.performance.now !== 'function') {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.performance.now = function () {
const performanceNow = global.nativePerformanceNow || Date.now;
return performanceNow();
};
// In case if the native implementation of the Performance API is available, use it,
// otherwise fall back to the legacy/default one, which only defines 'Performance.now()'
if (NativePerformance) {
// $FlowExpectedError[cannot-write]
global.performance = new Performance();
} else {
if (!global.performance) {
// $FlowExpectedError[cannot-write]
global.performance = ({}: {now?: () => number});
}
/**
* Returns a double, measured in milliseconds.
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
*/
if (typeof global.performance.now !== 'function') {
// $FlowExpectedError[cannot-write]
global.performance.now = function () {
const performanceNow = global.nativePerformanceNow || Date.now;
return performanceNow();
};
}
}

Просмотреть файл

@ -1,17 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @format
*/
import Performance from '../WebPerformance/Performance';
// TODO: Replace setUpPerformance with this once the WebPerformance API is stable (T143070419)
export default function setUpPerformance() {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.performance = new Performance();
}