status-react/figwheel-bridge.js

281 lines
7.7 KiB
JavaScript
Raw Normal View History

2016-02-22 22:04:42 +00:00
/*
* Originally taken from https://github.com/decker405/figwheel-react-native
*
* @providesModule figwheel-bridge
*/
var CLOSURE_UNCOMPILED_DEFINES = null;
2016-04-08 00:59:58 +00:00
var debugEnabled = false;
2016-02-22 22:04:42 +00:00
var config = {
basePath: "target/",
googBasePath: 'goog/',
serverPort: 8081
};
var React = require('react-native');
2016-04-08 00:59:58 +00:00
var WebSocket = require('WebSocket');
2016-02-22 22:04:42 +00:00
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 = {};
2016-04-08 00:59:58 +00:00
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();
}
}];
2016-02-22 22:04:42 +00:00
var figwheelApp = function (platform, devHost) {
return React.createClass({
getInitialState: function () {
return {loaded: false}
},
render: function () {
if (!this.state.loaded) {
var plainStyle = {flex: 1, alignItems: 'center', justifyContent: 'center'};
return (
<React.View style={plainStyle}>
<React.Text>Waiting for Figwheel to load files.</React.Text>
</React.View>
);
}
return this.state.root;
},
componentDidMount: function () {
var app = this;
if (typeof goog === "undefined") {
2016-04-08 00:59:58 +00:00
loadApp(platform, devHost, function (appRoot) {
2016-02-22 22:04:42 +00:00
app.setState({root: appRoot, loaded: true})
});
}
}
})
};
2016-04-08 00:59:58 +00:00
function logDebug(msg) {
if (debugEnabled) {
console.log(msg);
}
}
2016-02-22 22:04:42 +00:00
// evaluates js code ensuring proper ordering
function customEval(url, javascript, success, error) {
if (scriptQueue.length > 0) {
if (scriptQueue[0] === url) {
try {
evaluate(javascript);
2016-04-08 00:59:58 +00:00
logDebug('Evaluated: ' + url);
2016-02-22 22:04:42 +00:00
scriptQueue.shift();
evalListeners.forEach(function (listener) {
listener(url)
});
success();
} catch (e) {
console.error('Evaluation error in: ' + url);
console.error(e);
error();
}
} else {
setTimeout(function () {
customEval(url, javascript, success, error)
}, 5);
}
} else {
console.error('Something bad happened...');
error()
}
}
var isChrome = function () {
return typeof importScripts === "function"
};
function asyncImportScripts(url, success, error) {
2016-04-08 00:59:58 +00:00
logDebug('(asyncImportScripts) Importing: ' + url);
2016-02-22 22:04:42 +00:00
scriptQueue.push(url);
fetch(url)
.then(function (response) {
return response.text()
})
.then(function (responseText) {
return customEval(url, responseText, success, error);
})
.catch(function (error) {
console.error('Error loading script, please check your config setup.');
console.error(error);
return error();
});
}
function syncImportScripts(url, success, error) {
try {
importScripts(url);
2016-04-08 00:59:58 +00:00
logDebug('Evaluated: ' + url);
2016-02-22 22:04:42 +00:00
evalListeners.forEach(function (listener) {
listener(url)
});
success();
} catch (e) {
error()
}
}
// Loads js file sync if possible or async.
function importJs(src, success, error) {
if (typeof success !== 'function') {
success = function () {
};
}
if (typeof error !== 'function') {
error = function () {
};
}
var file = fileBasePath + '/' + src;
2016-04-08 00:59:58 +00:00
logDebug('(importJs) Importing: ' + file);
2016-02-22 22:04:42 +00:00
if (isChrome()) {
syncImportScripts(serverBaseUrl("localhost") + '/' + file, success, error);
} else {
asyncImportScripts(serverBaseUrl(serverHost) + '/' + file, success, error);
}
}
function interceptRequire() {
var oldRequire = window.require;
console.info("Shimming require");
window.require = function (id) {
console.info("Requiring: " + id);
if (externalModules[id]) {
return externalModules[id];
}
return oldRequire(id);
};
}
2016-04-08 00:59:58 +00:00
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);
2016-04-08 00:59:58 +00:00
}
};
2016-02-22 22:04:42 +00:00
}
function serverBaseUrl(host) {
return "http://" + host + ":" + config.serverPort
}
2016-04-08 00:59:58 +00:00
function setCorrectWebSocketImpl() {
figwheel.client.socket.get_websocket_imp = function () {
return WebSocket;
};
}
2016-02-22 22:04:42 +00:00
function loadApp(platform, devHost, onLoadCb) {
serverHost = devHost;
fileBasePath = config.basePath + platform;
// 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);
2016-04-08 00:59:58 +00:00
console.info('Done loading Clojure app');
2016-02-22 22:04:42 +00:00
}
});
if (typeof goog === "undefined") {
2016-04-08 00:59:58 +00:00
console.info('Loading Closure base.');
2016-02-22 22:04:42 +00:00
interceptRequire();
2016-04-08 00:59:58 +00:00
compileWarningsToYellowBox();
2016-02-22 22:04:42 +00:00
importJs('goog/base.js', function () {
shimBaseGoog();
importJs('cljs_deps.js');
importJs('goog/deps.js', function () {
// This is needed because of RN packager
// seriously React packager? why.
var googreq = goog.require;
googreq('figwheel.connect');
});
});
}
}
function startApp(appName, platform, devHost) {
React.AppRegistry.registerComponent(
appName, () => figwheelApp(platform, devHost));
}
function withModules(moduleById) {
externalModules = moduleById;
return self;
}
// Goog fixes
function shimBaseGoog() {
console.info('Shimming goog functions.');
goog.basePath = 'goog/';
goog.writeScriptSrcNode = importJs;
goog.writeScriptTag_ = function (src, optSourceText) {
importJs(src);
return true;
};
}
// Figwheel fixes
// Used by figwheel - uses importScript to load JS rather than <script>'s
function shimJsLoader() {
console.info('==== Shimming jsloader ====');
goog.net.jsloader.load = function (uri, options) {
var deferred = {
callbacks: [],
errbacks: [],
addCallback: function (cb) {
deferred.callbacks.push(cb);
},
addErrback: function (cb) {
deferred.errbacks.push(cb);
},
callAllCallbacks: function () {
while (deferred.callbacks.length > 0) {
deferred.callbacks.shift()();
}
},
callAllErrbacks: function () {
while (deferred.errbacks.length > 0) {
deferred.errbacks.shift()();
}
}
};
// Figwheel needs this to be an async call,
// so that it can add callbacks to deferred
setTimeout(function () {
importJs(uri.getPath(),
deferred.callAllCallbacks,
deferred.callAllErrbacks);
}, 1);
return deferred;
};
}
self = {
withModules: withModules,
start: startApp
};
module.exports = self;