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-02-20 04:10:52 +00:00
|
|
|
require('RCTDeviceEventEmitter');
|
|
|
|
|
|
|
|
if (typeof GLOBAL === 'undefined') {
|
|
|
|
GLOBAL = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof window === 'undefined') {
|
|
|
|
window = GLOBAL;
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:02:42 +00:00
|
|
|
/**
|
|
|
|
* The document must be shimmed before anything else that might define the
|
|
|
|
* `ExecutionEnvironment` module (which checks for `document.createElement`).
|
|
|
|
*/
|
|
|
|
function setupDocumentShim() {
|
|
|
|
// The browser defines Text and Image globals by default. If you forget to
|
|
|
|
// require them, then the error message is very confusing.
|
|
|
|
function getInvalidGlobalUseError(name) {
|
|
|
|
return new Error(
|
|
|
|
'You are trying to render the global ' + name + ' variable as a ' +
|
|
|
|
'React element. You probably forgot to require ' + name + '.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
GLOBAL.Text = {
|
|
|
|
get defaultProps() {
|
|
|
|
throw getInvalidGlobalUseError('Text');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
GLOBAL.Image = {
|
|
|
|
get defaultProps() {
|
|
|
|
throw getInvalidGlobalUseError('Image');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// Force `ExecutionEnvironment.canUseDOM` to be false.
|
2015-03-04 21:06:01 +00:00
|
|
|
if (GLOBAL.document) {
|
|
|
|
GLOBAL.document.createElement = null;
|
|
|
|
}
|
2015-03-31 21:56:10 +00:00
|
|
|
|
|
|
|
// There is no DOM so MutationObserver doesn't make sense. It is used
|
|
|
|
// as feature detection in Bluebird Promise implementation
|
|
|
|
GLOBAL.MutationObserver = undefined;
|
2015-03-04 02:02:42 +00:00
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
function handleErrorWithRedBox(e) {
|
2015-03-19 16:51:33 +00:00
|
|
|
try {
|
|
|
|
require('ExceptionsManager').handleException(e);
|
|
|
|
} catch(ee) {
|
|
|
|
console.log('Failed to print error: ', ee.message);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupRedBoxErrorHandler() {
|
2015-03-04 02:02:42 +00:00
|
|
|
var ErrorUtils = require('ErrorUtils');
|
2015-02-20 04:10:52 +00:00
|
|
|
ErrorUtils.setGlobalHandler(handleErrorWithRedBox);
|
|
|
|
}
|
|
|
|
|
2015-04-30 09:45:00 +00:00
|
|
|
function setupRedBoxConsoleErrorHandler() {
|
|
|
|
// ExceptionsManager transitively requires Promise so we install it after
|
|
|
|
var ExceptionsManager = require('ExceptionsManager');
|
|
|
|
ExceptionsManager.installConsoleErrorReporter();
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
buttons: [{'cancel': 'Okay'}],
|
|
|
|
};
|
2015-03-17 10:08:46 +00:00
|
|
|
RCTAlertManager.alertWithArgs(alertOpts, null);
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupPromise() {
|
|
|
|
// The native Promise implementation throws the following error:
|
|
|
|
// ERROR: Event loop not supported.
|
|
|
|
GLOBAL.Promise = require('Promise');
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupXHR() {
|
|
|
|
// The native XMLHttpRequest in Chrome dev tools is CORS aware and won't
|
|
|
|
// let you fetch anything from the internet
|
|
|
|
GLOBAL.XMLHttpRequest = require('XMLHttpRequest');
|
|
|
|
GLOBAL.fetch = require('fetch');
|
|
|
|
}
|
|
|
|
|
2015-03-01 04:46:42 +00:00
|
|
|
function setupGeolocation() {
|
|
|
|
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-02-20 04:10:52 +00:00
|
|
|
setupDocumentShim();
|
2015-04-23 18:34:25 +00:00
|
|
|
setupRedBoxErrorHandler();
|
2015-02-20 04:10:52 +00:00
|
|
|
setupTimers();
|
|
|
|
setupAlert();
|
|
|
|
setupPromise();
|
|
|
|
setupXHR();
|
2015-04-30 09:45:00 +00:00
|
|
|
setupRedBoxConsoleErrorHandler();
|
2015-03-01 04:46:42 +00:00
|
|
|
setupGeolocation();
|