reverted figwheel-bridge.js

This commit is contained in:
Jarrad Hope 2016-04-24 20:54:40 +07:00
parent c36ce07c84
commit 0638ac36de
1 changed files with 52 additions and 43 deletions

View File

@ -5,7 +5,6 @@
*/
var CLOSURE_UNCOMPILED_DEFINES = null;
var debugEnabled = false;
var config = {
basePath: "target/",
@ -14,24 +13,13 @@ var config = {
};
var React = require('react-native');
var WebSocket = require('WebSocket');
var self;
var scriptQueue = [];
var serverHost = null; // will be set dynamically
var fileBasePath = null; // will be set dynamically
var evaluate = eval; // This is needed, direct calls to eval does not work (RN packager???)
var externalModules = {};
var evalListeners = [ // Functions to be called after each js file is loaded and evaluated
function (url) {
if (url.indexOf('jsloader') > -1) {
shimJsLoader();
}
},
function (url) {
if (url.indexOf('/figwheel/client/socket') > -1) {
setCorrectWebSocketImpl();
}
}];
var evalListeners = []; // functions to be called when a script is evaluated
var figwheelApp = function (platform, devHost) {
return React.createClass({
@ -52,7 +40,7 @@ var figwheelApp = function (platform, devHost) {
componentDidMount: function () {
var app = this;
if (typeof goog === "undefined") {
loadApp(platform, devHost, function (appRoot) {
loadApp(platform, devHost, function(appRoot) {
app.setState({root: appRoot, loaded: true})
});
}
@ -60,19 +48,13 @@ var figwheelApp = function (platform, devHost) {
})
};
function logDebug(msg) {
if (debugEnabled) {
console.log(msg);
}
}
// evaluates js code ensuring proper ordering
function customEval(url, javascript, success, error) {
if (scriptQueue.length > 0) {
if (scriptQueue[0] === url) {
try {
evaluate(javascript);
logDebug('Evaluated: ' + url);
console.info('Evaluated: ' + url);
scriptQueue.shift();
evalListeners.forEach(function (listener) {
listener(url)
@ -99,7 +81,7 @@ var isChrome = function () {
};
function asyncImportScripts(url, success, error) {
logDebug('(asyncImportScripts) Importing: ' + url);
console.info('(asyncImportScripts) Importing: ' + url);
scriptQueue.push(url);
fetch(url)
.then(function (response) {
@ -118,7 +100,7 @@ function asyncImportScripts(url, success, error) {
function syncImportScripts(url, success, error) {
try {
importScripts(url);
logDebug('Evaluated: ' + url);
console.info('Evaluated: ' + url);
evalListeners.forEach(function (listener) {
listener(url)
});
@ -141,7 +123,7 @@ function importJs(src, success, error) {
var file = fileBasePath + '/' + src;
logDebug('(importJs) Importing: ' + file);
console.info('(importJs) Importing: ' + file);
if (isChrome()) {
syncImportScripts(serverBaseUrl("localhost") + '/' + file, success, error);
} else {
@ -161,49 +143,43 @@ function interceptRequire() {
};
}
function compileWarningsToYellowBox() {
var log = window.console.log;
var compileWarningRx = /Figwheel: Compile/;
window.console.log = function (msg) {
if (compileWarningRx.test(msg)) {
console.warn(msg);
} else {
log.apply(window.console, arguments);
}
};
// do not show debug messages in yellow box
function debugToLog() {
console.debug = console.log;
}
function serverBaseUrl(host) {
return "http://" + host + ":" + config.serverPort
}
function setCorrectWebSocketImpl() {
figwheel.client.socket.get_websocket_imp = function () {
return WebSocket;
};
}
function loadApp(platform, devHost, onLoadCb) {
serverHost = devHost;
fileBasePath = config.basePath + platform;
evalListeners.push(function (url) {
if (url.indexOf('jsloader') > -1) {
shimJsLoader();
}
});
// callback when app is ready to get the reloadable component
var mainJs = '/env/' + platform + '/main.js';
evalListeners.push(function (url) {
if (url.indexOf(mainJs) > -1) {
onLoadCb(env[platform].main.root_el);
console.info('Done loading Clojure app');
console.log('Done loading Clojure app');
}
});
if (typeof goog === "undefined") {
console.info('Loading Closure base.');
console.log('Loading Closure base.');
interceptRequire();
compileWarningsToYellowBox();
importJs('goog/base.js', function () {
shimBaseGoog();
fakeLocalStorageAndDocument();
importJs('cljs_deps.js');
importJs('goog/deps.js', function () {
debugToLog();
// This is needed because of RN packager
// seriously React packager? why.
var googreq = goog.require;
@ -233,6 +209,39 @@ function shimBaseGoog() {
importJs(src);
return true;
};
goog.inHtmlDocument_ = function () {
return true;
};
}
function fakeLocalStorageAndDocument() {
window.localStorage = {};
window.localStorage.getItem = function () {
return 'true';
};
window.localStorage.setItem = function () {
};
window.document = {};
window.document.body = {};
window.document.body.dispatchEvent = function () {
};
window.document.createElement = function () {
};
if (typeof window.location === 'undefined') {
window.location = {};
}
console.debug = console.warn;
window.addEventListener = function () {
};
// make figwheel think that heads-up-display divs are there
window.document.querySelector = function (selector) {
return {};
};
window.document.getElementById = function (id) {
return {style:{}};
};
}
// Figwheel fixes