2015-02-20 04:10:52 +00:00
|
|
|
/**
|
2015-03-23 20:35:08 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-02-20 04:10:52 +00:00
|
|
|
*
|
|
|
|
* Sets up global variables typical in most JavaScript environments.
|
|
|
|
*
|
|
|
|
* 1. Global timers (via `setTimeout` etc).
|
|
|
|
* 2. Global console object.
|
|
|
|
* 3. Hooks for printing stack traces with source maps.
|
|
|
|
*
|
|
|
|
* Leaves enough room in the environment for implementing your own:
|
|
|
|
* 1. Require system.
|
|
|
|
* 2. Bridged modules.
|
|
|
|
*
|
|
|
|
* @providesModule InitializeJavaScriptAppEngine
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* eslint global-strict: 0 */
|
|
|
|
/* globals GLOBAL: true, window: true */
|
|
|
|
|
2015-03-04 02:02:42 +00:00
|
|
|
// Just to make sure the JS gets packaged up.
|
2015-06-25 12:37:22 +00:00
|
|
|
require('RCTDebugComponentOwnership');
|
2015-02-20 04:10:52 +00:00
|
|
|
require('RCTDeviceEventEmitter');
|
2015-06-19 21:59:42 +00:00
|
|
|
require('PerformanceLogger');
|
2015-08-04 12:23:31 +00:00
|
|
|
require('regenerator/runtime');
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
if (typeof GLOBAL === 'undefined') {
|
|
|
|
GLOBAL = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
window = GLOBAL;
|
|
|
|
}
|
|
|
|
|
2015-06-03 00:51:36 +00:00
|
|
|
function handleError(e, isFatal) {
|
2015-03-19 16:51:33 +00:00
|
|
|
try {
|
2015-05-13 17:56:09 +00:00
|
|
|
require('ExceptionsManager').handleException(e, isFatal);
|
2015-03-19 16:51:33 +00:00
|
|
|
} catch(ee) {
|
|
|
|
console.log('Failed to print error: ', ee.message);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpRedBoxErrorHandler() {
|
2015-03-04 02:02:42 +00:00
|
|
|
var ErrorUtils = require('ErrorUtils');
|
2015-06-03 00:51:36 +00:00
|
|
|
ErrorUtils.setGlobalHandler(handleError);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpRedBoxConsoleErrorHandler() {
|
2015-04-30 09:45:00 +00:00
|
|
|
// ExceptionsManager transitively requires Promise so we install it after
|
|
|
|
var ExceptionsManager = require('ExceptionsManager');
|
2015-04-30 17:45:10 +00:00
|
|
|
var Platform = require('Platform');
|
|
|
|
// TODO (#6925182): Enable console.error redbox on Android
|
2015-04-30 21:18:00 +00:00
|
|
|
if (__DEV__ && Platform.OS === 'ios') {
|
2015-04-30 17:45:10 +00:00
|
|
|
ExceptionsManager.installConsoleErrorReporter();
|
|
|
|
}
|
2015-04-30 09:45:00 +00:00
|
|
|
}
|
|
|
|
|
2015-08-17 09:05:29 +00:00
|
|
|
function setupFlowChecker() {
|
|
|
|
if (__DEV__) {
|
|
|
|
var checkFlowAtRuntime = require('checkFlowAtRuntime');
|
|
|
|
checkFlowAtRuntime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* Sets up a set of window environment wrappers that ensure that the
|
|
|
|
* BatchedBridge is flushed after each tick. In both the case of the
|
2015-03-17 10:08:46 +00:00
|
|
|
* `UIWebView` based `RCTJavaScriptCaller` and `RCTContextCaller`, we
|
2015-02-20 04:10:52 +00:00
|
|
|
* implement our own custom timing bridge that should be immune to
|
|
|
|
* unexplainably dropped timing signals.
|
|
|
|
*/
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpTimers() {
|
2015-03-04 02:02:42 +00:00
|
|
|
var JSTimers = require('JSTimers');
|
2015-02-20 04:10:52 +00:00
|
|
|
GLOBAL.setTimeout = JSTimers.setTimeout;
|
|
|
|
GLOBAL.setInterval = JSTimers.setInterval;
|
|
|
|
GLOBAL.setImmediate = JSTimers.setImmediate;
|
|
|
|
GLOBAL.clearTimeout = JSTimers.clearTimeout;
|
|
|
|
GLOBAL.clearInterval = JSTimers.clearInterval;
|
|
|
|
GLOBAL.clearImmediate = JSTimers.clearImmediate;
|
|
|
|
GLOBAL.cancelAnimationFrame = JSTimers.clearInterval;
|
|
|
|
GLOBAL.requestAnimationFrame = function(cb) {
|
|
|
|
/*requestAnimationFrame() { [native code] };*/ // Trick scroller library
|
|
|
|
return JSTimers.requestAnimationFrame(cb); // into thinking it's native
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpAlert() {
|
2015-03-18 05:22:03 +00:00
|
|
|
var RCTAlertManager = require('NativeModules').AlertManager;
|
2015-02-20 04:10:52 +00:00
|
|
|
if (!GLOBAL.alert) {
|
|
|
|
GLOBAL.alert = function(text) {
|
|
|
|
var alertOpts = {
|
|
|
|
title: 'Alert',
|
|
|
|
message: '' + text,
|
2015-06-05 22:23:30 +00:00
|
|
|
buttons: [{'cancel': 'OK'}],
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
2015-08-10 20:09:38 +00:00
|
|
|
RCTAlertManager.alertWithArgs(alertOpts, function () {});
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpPromise() {
|
2015-02-20 04:10:52 +00:00
|
|
|
// The native Promise implementation throws the following error:
|
|
|
|
// ERROR: Event loop not supported.
|
|
|
|
GLOBAL.Promise = require('Promise');
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpXHR() {
|
2015-02-20 04:10:52 +00:00
|
|
|
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
|
|
|
|
// let you fetch anything from the internet
|
|
|
|
GLOBAL.XMLHttpRequest = require('XMLHttpRequest');
|
2015-06-09 19:25:24 +00:00
|
|
|
GLOBAL.FormData = require('FormData');
|
2015-05-07 19:29:36 +00:00
|
|
|
|
|
|
|
var fetchPolyfill = require('fetch');
|
|
|
|
GLOBAL.fetch = fetchPolyfill.fetch;
|
|
|
|
GLOBAL.Headers = fetchPolyfill.Headers;
|
|
|
|
GLOBAL.Request = fetchPolyfill.Request;
|
|
|
|
GLOBAL.Response = fetchPolyfill.Response;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpGeolocation() {
|
2015-03-01 04:46:42 +00:00
|
|
|
GLOBAL.navigator = GLOBAL.navigator || {};
|
2015-03-09 10:04:44 +00:00
|
|
|
GLOBAL.navigator.geolocation = require('Geolocation');
|
2015-03-01 04:46:42 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
function setUpWebSockets() {
|
|
|
|
GLOBAL.WebSocket = require('WebSocket');
|
|
|
|
}
|
|
|
|
|
2015-05-14 22:55:31 +00:00
|
|
|
function setupProfile() {
|
|
|
|
console.profile = console.profile || GLOBAL.consoleProfile || function () {};
|
|
|
|
console.profileEnd = console.profileEnd || GLOBAL.consoleProfileEnd || function () {};
|
2015-06-15 20:05:05 +00:00
|
|
|
require('BridgeProfiling').swizzleReactPerf();
|
2015-05-14 22:55:31 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 02:31:05 +00:00
|
|
|
function setUpProcessEnv() {
|
2015-08-17 22:41:47 +00:00
|
|
|
GLOBAL.process = GLOBAL.process || {};
|
|
|
|
GLOBAL.process.env = {NODE_ENV: __DEV__ ? 'development' : 'production'};
|
2015-08-11 02:31:05 +00:00
|
|
|
}
|
|
|
|
|
2015-05-14 16:28:09 +00:00
|
|
|
setUpRedBoxErrorHandler();
|
|
|
|
setUpTimers();
|
|
|
|
setUpAlert();
|
|
|
|
setUpPromise();
|
|
|
|
setUpXHR();
|
|
|
|
setUpRedBoxConsoleErrorHandler();
|
|
|
|
setUpGeolocation();
|
|
|
|
setUpWebSockets();
|
2015-05-14 22:55:31 +00:00
|
|
|
setupProfile();
|
2015-08-11 02:31:05 +00:00
|
|
|
setUpProcessEnv();
|
2015-08-17 09:05:29 +00:00
|
|
|
setupFlowChecker();
|