Skip to content

Commit

Permalink
Declare some missing globals
Browse files Browse the repository at this point in the history
Summary:
This declares a few globals that were missing in our `global.js` Flow declaration file:
* `process`
* `performance` with its current definition. We'll replace it with the new API when we replace `setupPerformance` with `setupWebPerformance`.
* `navigator`
* `setImmediate`
* `clearImmediate`

Eventually we should stop including all DOM definitions that Flow provides out of the box and define only what we provide (which is pretty much this file).

Changelog: [Internal]

Reviewed By: christophpurrer

Differential Revision: D42964772

fbshipit-source-id: 6156968e8a9d193e7068d8a5043aa682ad45bba1
  • Loading branch information
rubennorte authored and facebook-github-bot committed Feb 9, 2023
1 parent c8c6abe commit 3337553
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
7 changes: 5 additions & 2 deletions Libraries/Core/setUpGlobals.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,21 @@
* You can use this module directly, or just require InitializeCore.
*/
if (global.window === undefined) {
// $FlowFixMe[cannot-write]
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.window = global;
}

if (global.self === undefined) {
// $FlowFixMe[cannot-write]
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.self = global;
}

// Set up process
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.process = global.process || {};
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.process.env = global.process.env || {};
if (!global.process.env.NODE_ENV) {
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
}
11 changes: 6 additions & 5 deletions Libraries/Core/setUpNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@

const {polyfillObjectProperty} = require('../Utilities/PolyfillFunctions');

let navigator = global.navigator;
const navigator = global.navigator;
if (navigator === undefined) {
global.navigator = navigator = {};
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
global.navigator = {product: 'ReactNative'};
} else {
// see https://github.com/facebook/react-native/issues/10881
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
}

// see https://github.com/facebook/react-native/issues/10881
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
1 change: 1 addition & 0 deletions Libraries/Core/setUpPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (!global.performance) {
* 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();
Expand Down
1 change: 1 addition & 0 deletions Libraries/Core/setUpWebPerformance.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ 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();
}
29 changes: 25 additions & 4 deletions flow/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,19 @@ declare var global: {
// setUpGlobals
+window: typeof global,
+self: typeof global,
+process: {
+env: {
+NODE_ENV: 'development' | 'production',
},
+argv?: $ReadOnlyArray<string>,
},

// setXHR
// setUpPerformance
+performance: {
+now: () => number,
},

// setUpXHR
+XMLHttpRequest: typeof XMLHttpRequest,
+FormData: typeof FormData,
+fetch: typeof fetch,
Expand All @@ -39,17 +50,27 @@ declare var global: {
// setUpAlert
+alert: typeof alert,

// setUpNavigator
+navigator: {
+product: 'ReactNative',
+appName?: ?string,
...
},

// setUpTimers
+clearInterval: typeof clearInterval,
+clearTimeout: typeof clearTimeout,
+setInterval: typeof setInterval,
+clearInterval: typeof clearInterval,
+setTimeout: typeof setTimeout,
+clearTimeout: typeof clearTimeout,
+requestAnimationFrame: typeof requestAnimationFrame,
+cancelAnimationFrame: typeof cancelAnimationFrame,
+requestIdleCallback: typeof requestIdleCallback,
+cancelIdleCallback: typeof cancelIdleCallback,
+setTimeout: typeof setTimeout,
+queueMicrotask: typeof queueMicrotask,
+setImmediate: typeof setImmediate,
+clearImmediate: typeof clearImmediate,

// Polyfills
+console: typeof console,

// JavaScript environments specific
Expand Down

0 comments on commit 3337553

Please sign in to comment.