Skip to content

Commit 3337553

Browse files
rubennortefacebook-github-bot
authored andcommitted
Declare some missing globals
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
1 parent c8c6abe commit 3337553

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

Libraries/Core/setUpGlobals.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,21 @@
1515
* You can use this module directly, or just require InitializeCore.
1616
*/
1717
if (global.window === undefined) {
18-
// $FlowFixMe[cannot-write]
18+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
1919
global.window = global;
2020
}
2121

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

2727
// Set up process
28+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
2829
global.process = global.process || {};
30+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
2931
global.process.env = global.process.env || {};
3032
if (!global.process.env.NODE_ENV) {
33+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
3134
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
3235
}

Libraries/Core/setUpNavigator.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212

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

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

Libraries/Core/setUpPerformance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (!global.performance) {
1717
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
1818
*/
1919
if (typeof global.performance.now !== 'function') {
20+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
2021
global.performance.now = function () {
2122
const performanceNow = global.nativePerformanceNow || Date.now;
2223
return performanceNow();

Libraries/Core/setUpWebPerformance.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ import Performance from '../WebPerformance/Performance';
1212

1313
// TODO: Replace setUpPerformance with this once the WebPerformance API is stable (T143070419)
1414
export default function setUpPerformance() {
15+
// $FlowExpectedError[cannot-write] The global isn't writable anywhere but here, where we define it.
1516
global.performance = new Performance();
1617
}

flow/global.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,19 @@ declare var global: {
1919
// setUpGlobals
2020
+window: typeof global,
2121
+self: typeof global,
22+
+process: {
23+
+env: {
24+
+NODE_ENV: 'development' | 'production',
25+
},
26+
+argv?: $ReadOnlyArray<string>,
27+
},
2228

23-
// setXHR
29+
// setUpPerformance
30+
+performance: {
31+
+now: () => number,
32+
},
33+
34+
// setUpXHR
2435
+XMLHttpRequest: typeof XMLHttpRequest,
2536
+FormData: typeof FormData,
2637
+fetch: typeof fetch,
@@ -39,17 +50,27 @@ declare var global: {
3950
// setUpAlert
4051
+alert: typeof alert,
4152

53+
// setUpNavigator
54+
+navigator: {
55+
+product: 'ReactNative',
56+
+appName?: ?string,
57+
...
58+
},
59+
4260
// setUpTimers
43-
+clearInterval: typeof clearInterval,
44-
+clearTimeout: typeof clearTimeout,
4561
+setInterval: typeof setInterval,
62+
+clearInterval: typeof clearInterval,
4663
+setTimeout: typeof setTimeout,
64+
+clearTimeout: typeof clearTimeout,
4765
+requestAnimationFrame: typeof requestAnimationFrame,
4866
+cancelAnimationFrame: typeof cancelAnimationFrame,
4967
+requestIdleCallback: typeof requestIdleCallback,
5068
+cancelIdleCallback: typeof cancelIdleCallback,
51-
+setTimeout: typeof setTimeout,
5269
+queueMicrotask: typeof queueMicrotask,
70+
+setImmediate: typeof setImmediate,
71+
+clearImmediate: typeof clearImmediate,
72+
73+
// Polyfills
5374
+console: typeof console,
5475

5576
// JavaScript environments specific

0 commit comments

Comments
 (0)