2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
2016-11-21 01:49:21 +00:00
|
|
|
* @providesModule InitializeCore
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* globals window: true */
|
|
|
|
|
|
|
|
/**
|
2015-02-20 04:10:52 +00:00
|
|
|
* Sets up global variables typical in most JavaScript environments.
|
|
|
|
*
|
2016-11-21 01:49:21 +00:00
|
|
|
* 1. Global timers (via `setTimeout` etc).
|
|
|
|
* 2. Global console object.
|
|
|
|
* 3. Hooks for printing stack traces with source maps.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* Leaves enough room in the environment for implementing your own:
|
|
|
|
*
|
2016-11-21 01:49:21 +00:00
|
|
|
* 1. Require system.
|
|
|
|
* 2. Bridged modules.
|
|
|
|
*
|
2015-02-20 04:10:52 +00:00
|
|
|
*/
|
2016-11-22 15:15:10 +00:00
|
|
|
'use strict';
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2018-02-15 11:52:52 +00:00
|
|
|
const {polyfillObjectProperty, polyfillGlobal} = require('PolyfillFunctions');
|
|
|
|
|
2016-06-23 22:04:39 +00:00
|
|
|
if (global.GLOBAL === undefined) {
|
2016-04-29 23:14:55 +00:00
|
|
|
global.GLOBAL = global;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2016-06-23 22:04:39 +00:00
|
|
|
if (global.window === undefined) {
|
2016-04-29 23:14:55 +00:00
|
|
|
global.window = global;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-30 18:49:22 +00:00
|
|
|
// Set up collections
|
|
|
|
const _shouldPolyfillCollection = require('_shouldPolyfillES6Collection');
|
|
|
|
if (_shouldPolyfillCollection('Map')) {
|
|
|
|
polyfillGlobal('Map', () => require('Map'));
|
|
|
|
}
|
|
|
|
if (_shouldPolyfillCollection('Set')) {
|
|
|
|
polyfillGlobal('Set', () => require('Set'));
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up process
|
|
|
|
global.process = global.process || {};
|
|
|
|
global.process.env = global.process.env || {};
|
|
|
|
if (!global.process.env.NODE_ENV) {
|
|
|
|
global.process.env.NODE_ENV = __DEV__ ? 'development' : 'production';
|
|
|
|
}
|
2016-06-29 23:34:09 +00:00
|
|
|
|
2017-06-23 17:19:17 +00:00
|
|
|
// Setup the Systrace profiling hooks if necessary
|
|
|
|
if (global.__RCTProfileIsProfiling) {
|
|
|
|
const Systrace = require('Systrace');
|
2018-02-20 06:30:36 +00:00
|
|
|
Systrace.installReactHook();
|
2017-06-23 17:19:17 +00:00
|
|
|
Systrace.setEnabled(true);
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up console
|
|
|
|
const ExceptionsManager = require('ExceptionsManager');
|
|
|
|
ExceptionsManager.installConsoleErrorReporter();
|
2016-04-27 16:56:48 +00:00
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up error handler
|
|
|
|
if (!global.__fbDisableExceptionsManager) {
|
2016-12-11 08:28:29 +00:00
|
|
|
const handleError = (e, isFatal) => {
|
2015-11-05 20:20:15 +00:00
|
|
|
try {
|
2016-10-11 13:51:48 +00:00
|
|
|
ExceptionsManager.handleException(e, isFatal);
|
2016-01-21 15:22:20 +00:00
|
|
|
} catch (ee) {
|
2015-11-05 20:20:15 +00:00
|
|
|
console.log('Failed to print error: ', ee.message);
|
2016-06-23 00:54:33 +00:00
|
|
|
throw e;
|
2015-11-05 20:20:15 +00:00
|
|
|
}
|
2016-12-11 08:28:29 +00:00
|
|
|
};
|
2015-11-05 20:20:15 +00:00
|
|
|
|
2016-05-04 17:49:29 +00:00
|
|
|
const ErrorUtils = require('ErrorUtils');
|
2015-06-03 00:51:36 +00:00
|
|
|
ErrorUtils.setGlobalHandler(handleError);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 21:20:02 +00:00
|
|
|
// Check for compatibility between the JS and native code
|
2017-10-31 21:18:00 +00:00
|
|
|
const ReactNativeVersionCheck = require('ReactNativeVersionCheck');
|
|
|
|
ReactNativeVersionCheck.checkVersions();
|
2017-09-28 01:19:44 +00:00
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up Promise
|
|
|
|
// The native Promise implementation throws the following error:
|
|
|
|
// ERROR: Event loop not supported.
|
2017-07-24 14:09:39 +00:00
|
|
|
polyfillGlobal('Promise', () => require('Promise'));
|
2016-10-11 13:51:48 +00:00
|
|
|
|
2017-07-19 18:54:16 +00:00
|
|
|
// Set up regenerator.
|
2017-07-24 14:09:39 +00:00
|
|
|
polyfillGlobal('regeneratorRuntime', () => {
|
2017-07-19 18:54:16 +00:00
|
|
|
// The require just sets up the global, so make sure when we first
|
|
|
|
// invoke it the global does not exist
|
|
|
|
delete global.regeneratorRuntime;
|
2017-09-06 10:25:01 +00:00
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an
|
|
|
|
* error found when Flow v0.54 was deployed. To see the error delete this
|
|
|
|
* comment and run Flow. */
|
2017-07-19 18:54:16 +00:00
|
|
|
require('regenerator-runtime/runtime');
|
|
|
|
return global.regeneratorRuntime;
|
|
|
|
});
|
|
|
|
|
2017-06-23 17:19:17 +00:00
|
|
|
// Set up timers
|
|
|
|
const defineLazyTimer = name => {
|
2017-07-24 14:09:39 +00:00
|
|
|
polyfillGlobal(name, () => require('JSTimers')[name]);
|
2017-06-23 17:19:17 +00:00
|
|
|
};
|
|
|
|
defineLazyTimer('setTimeout');
|
|
|
|
defineLazyTimer('setInterval');
|
|
|
|
defineLazyTimer('setImmediate');
|
|
|
|
defineLazyTimer('clearTimeout');
|
|
|
|
defineLazyTimer('clearInterval');
|
|
|
|
defineLazyTimer('clearImmediate');
|
|
|
|
defineLazyTimer('requestAnimationFrame');
|
|
|
|
defineLazyTimer('cancelAnimationFrame');
|
|
|
|
defineLazyTimer('requestIdleCallback');
|
|
|
|
defineLazyTimer('cancelIdleCallback');
|
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up XHR
|
|
|
|
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
|
|
|
|
// let you fetch anything from the internet
|
2017-07-24 14:09:39 +00:00
|
|
|
polyfillGlobal('XMLHttpRequest', () => require('XMLHttpRequest'));
|
|
|
|
polyfillGlobal('FormData', () => require('FormData'));
|
2016-10-11 13:51:48 +00:00
|
|
|
|
2017-07-24 14:09:39 +00:00
|
|
|
polyfillGlobal('fetch', () => require('fetch').fetch);
|
|
|
|
polyfillGlobal('Headers', () => require('fetch').Headers);
|
|
|
|
polyfillGlobal('Request', () => require('fetch').Request);
|
|
|
|
polyfillGlobal('Response', () => require('fetch').Response);
|
|
|
|
polyfillGlobal('WebSocket', () => require('WebSocket'));
|
2017-07-26 15:12:12 +00:00
|
|
|
polyfillGlobal('Blob', () => require('Blob'));
|
2018-01-26 17:06:14 +00:00
|
|
|
polyfillGlobal('File', () => require('File'));
|
|
|
|
polyfillGlobal('FileReader', () => require('FileReader'));
|
2017-07-26 15:12:12 +00:00
|
|
|
polyfillGlobal('URL', () => require('URL'));
|
2016-10-11 13:51:48 +00:00
|
|
|
|
2017-06-23 17:19:17 +00:00
|
|
|
// Set up alert
|
|
|
|
if (!global.alert) {
|
|
|
|
global.alert = function(text) {
|
|
|
|
// Require Alert on demand. Requiring it too early can lead to issues
|
|
|
|
// with things like Platform not being fully initialized.
|
|
|
|
require('Alert').alert('Alert', '' + text);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up Geolocation
|
|
|
|
let navigator = global.navigator;
|
|
|
|
if (navigator === undefined) {
|
|
|
|
global.navigator = navigator = {};
|
|
|
|
}
|
2016-11-22 15:15:10 +00:00
|
|
|
|
|
|
|
// see https://github.com/facebook/react-native/issues/10881
|
2018-02-15 11:52:52 +00:00
|
|
|
polyfillObjectProperty(navigator, 'product', () => 'ReactNative');
|
|
|
|
polyfillObjectProperty(navigator, 'geolocation', () => require('Geolocation'));
|
2016-10-11 13:51:48 +00:00
|
|
|
|
2017-06-23 17:19:17 +00:00
|
|
|
// Just to make sure the JS gets packaged up. Wait until the JS environment has
|
|
|
|
// been initialized before requiring them.
|
|
|
|
const BatchedBridge = require('BatchedBridge');
|
|
|
|
BatchedBridge.registerLazyCallableModule('Systrace', () => require('Systrace'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('JSTimers', () => require('JSTimers'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('HeapCapture', () => require('HeapCapture'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('SamplingProfiler', () => require('SamplingProfiler'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('RCTLog', () => require('RCTLog'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('RCTDeviceEventEmitter', () => require('RCTDeviceEventEmitter'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('RCTNativeAppEventEmitter', () => require('RCTNativeAppEventEmitter'));
|
|
|
|
BatchedBridge.registerLazyCallableModule('PerformanceLogger', () => require('PerformanceLogger'));
|
2018-01-19 20:34:23 +00:00
|
|
|
BatchedBridge.registerLazyCallableModule('JSDevSupportModule', () => require('JSDevSupportModule'));
|
2016-10-11 13:51:48 +00:00
|
|
|
|
2018-03-09 16:17:25 +00:00
|
|
|
global.__fetchSegment = function(
|
2017-11-08 15:44:43 +00:00
|
|
|
segmentId: number,
|
2017-10-12 16:20:17 +00:00
|
|
|
callback: (?Error) => void,
|
|
|
|
) {
|
2017-11-08 15:44:43 +00:00
|
|
|
const {SegmentFetcher} = require('NativeModules');
|
|
|
|
if (!SegmentFetcher) {
|
|
|
|
throw new Error('SegmentFetcher is missing. Please ensure that it is ' +
|
|
|
|
'included as a NativeModule.');
|
2017-10-12 16:20:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 15:44:43 +00:00
|
|
|
SegmentFetcher.fetchSegment(segmentId, (errorObject: ?{message: string, code: string}) => {
|
2017-10-12 16:20:17 +00:00
|
|
|
if (errorObject) {
|
|
|
|
const error = new Error(errorObject.message);
|
|
|
|
(error: any).code = errorObject.code;
|
|
|
|
callback(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
callback(null);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-10-11 13:51:48 +00:00
|
|
|
// Set up devtools
|
|
|
|
if (__DEV__) {
|
2017-05-11 22:09:05 +00:00
|
|
|
if (!global.__RCTProfileIsProfiling) {
|
2017-06-23 17:19:17 +00:00
|
|
|
BatchedBridge.registerCallableModule('HMRClient', require('HMRClient'));
|
|
|
|
|
2017-05-11 22:09:05 +00:00
|
|
|
// not when debugging in chrome
|
|
|
|
// TODO(t12832058) This check is broken
|
|
|
|
if (!window.document) {
|
|
|
|
require('setupDevtools');
|
|
|
|
}
|
2015-05-07 19:29:36 +00:00
|
|
|
|
2017-06-06 10:53:34 +00:00
|
|
|
// Set up inspector
|
2017-05-11 22:09:05 +00:00
|
|
|
const JSInspector = require('JSInspector');
|
2017-10-03 03:59:10 +00:00
|
|
|
/* $FlowFixMe(>=0.56.0 site=react_native_fb,react_native_oss) This comment
|
|
|
|
* suppresses an error found when Flow v0.56 was deployed. To see the error
|
|
|
|
* delete this comment and run Flow. */
|
2017-05-11 22:09:05 +00:00
|
|
|
JSInspector.registerAgent(require('NetworkAgent'));
|
|
|
|
}
|
2016-11-02 19:18:17 +00:00
|
|
|
}
|