From b89358263a8576af58b3f8e702df0dfd25e58e70 Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Wed, 15 Mar 2023 05:36:24 -0700 Subject: [PATCH] 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 --- Libraries/Core/setUpPerformance.js | 37 +++++++++++++++++---------- Libraries/Core/setUpWebPerformance.js | 17 ------------ 2 files changed, 24 insertions(+), 30 deletions(-) delete mode 100644 Libraries/Core/setUpWebPerformance.js diff --git a/Libraries/Core/setUpPerformance.js b/Libraries/Core/setUpPerformance.js index ef12f6c9ae..c5f25890cd 100644 --- a/Libraries/Core/setUpPerformance.js +++ b/Libraries/Core/setUpPerformance.js @@ -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(); + }; + } } diff --git a/Libraries/Core/setUpWebPerformance.js b/Libraries/Core/setUpWebPerformance.js deleted file mode 100644 index 333c5c57c1..0000000000 --- a/Libraries/Core/setUpWebPerformance.js +++ /dev/null @@ -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(); -}