2015-11-27 22:57:14 +00:00
|
|
|
/*
|
2015-11-27 23:46:17 +00:00
|
|
|
* Originally taken from https://github.com/decker405/figwheel-react-native
|
|
|
|
*
|
2015-11-27 22:57:14 +00:00
|
|
|
* @providesModule figwheel-bridge
|
|
|
|
*/
|
|
|
|
|
|
|
|
var CLOSURE_UNCOMPILED_DEFINES = null;
|
|
|
|
|
|
|
|
var config = {
|
2015-12-05 22:53:27 +00:00
|
|
|
basePath: "target/",
|
2016-01-20 23:14:35 +00:00
|
|
|
googBasePath: 'goog/',
|
|
|
|
serverPort: 8081
|
2015-11-27 22:57:14 +00:00
|
|
|
};
|
|
|
|
|
2016-01-09 22:35:40 +00:00
|
|
|
var React = require('react-native');
|
2016-01-04 22:10:13 +00:00
|
|
|
var self;
|
2015-12-05 22:53:27 +00:00
|
|
|
var scriptQueue = [];
|
2016-01-20 23:14:35 +00:00
|
|
|
var serverHost = null; // will be set dynamically
|
2015-12-05 22:53:27 +00:00
|
|
|
var fileBasePath = null; // will be set dynamically
|
|
|
|
var evaluate = eval; // This is needed, direct calls to eval does not work (RN packager???)
|
2016-01-04 22:10:13 +00:00
|
|
|
var externalModules = {};
|
2016-01-09 22:35:40 +00:00
|
|
|
var evalListeners = []; // functions to be called when a script is evaluated
|
|
|
|
|
|
|
|
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") {
|
|
|
|
loadApp(platform, devHost, function(appRoot) {
|
|
|
|
app.setState({root: appRoot, loaded: true})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
2015-12-05 22:53:27 +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);
|
|
|
|
console.info('Evaluated: ' + url);
|
|
|
|
scriptQueue.shift();
|
2016-01-09 22:35:40 +00:00
|
|
|
evalListeners.forEach(function (listener) {
|
|
|
|
listener(url)
|
|
|
|
});
|
2015-12-05 22:53:27 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
var isChrome = function () {
|
|
|
|
return typeof importScripts === "function"
|
|
|
|
};
|
|
|
|
|
|
|
|
function asyncImportScripts(url, success, error) {
|
2015-12-05 22:53:27 +00:00
|
|
|
console.info('(asyncImportScripts) Importing: ' + url);
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
function syncImportScripts(url, success, error) {
|
|
|
|
try {
|
|
|
|
importScripts(url);
|
|
|
|
console.info('Evaluated: ' + url);
|
|
|
|
evalListeners.forEach(function (listener) {
|
|
|
|
listener(url)
|
|
|
|
});
|
|
|
|
success();
|
|
|
|
} catch (e) {
|
|
|
|
error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads js file sync if possible or async.
|
2015-12-05 22:53:27 +00:00
|
|
|
function importJs(src, success, error) {
|
|
|
|
if (typeof success !== 'function') {
|
|
|
|
success = function () {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (typeof error !== 'function') {
|
|
|
|
error = function () {
|
|
|
|
};
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
2015-12-05 22:53:27 +00:00
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
var file = fileBasePath + '/' + src;
|
2015-12-05 22:53:27 +00:00
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
console.info('(importJs) Importing: ' + file);
|
|
|
|
if (isChrome()) {
|
|
|
|
syncImportScripts(serverBaseUrl("localhost") + '/' + file, success, error);
|
|
|
|
} else {
|
|
|
|
asyncImportScripts(serverBaseUrl(serverHost) + '/' + file, success, error);
|
|
|
|
}
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 22:10:13 +00:00
|
|
|
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-01-09 22:35:40 +00:00
|
|
|
// do not show debug messages in yellow box
|
|
|
|
function debugToLog() {
|
|
|
|
console.debug = console.log;
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
function serverBaseUrl(host) {
|
|
|
|
return "http://" + host + ":" + config.serverPort
|
|
|
|
}
|
|
|
|
|
2016-01-09 22:35:40 +00:00
|
|
|
function loadApp(platform, devHost, onLoadCb) {
|
2016-01-20 23:14:35 +00:00
|
|
|
serverHost = devHost;
|
2015-12-05 22:53:27 +00:00
|
|
|
fileBasePath = config.basePath + platform;
|
2015-11-27 22:57:14 +00:00
|
|
|
|
2016-01-09 22:35:40 +00:00
|
|
|
evalListeners.push(function (url) {
|
|
|
|
if (url.indexOf('jsloader') > -1) {
|
|
|
|
shimJsLoader();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// callback when app is ready to get the reloadable component
|
2016-01-27 20:42:07 +00:00
|
|
|
var mainJs = '/env/' + platform + '/main.js';
|
2016-01-09 22:35:40 +00:00
|
|
|
evalListeners.push(function (url) {
|
2016-01-27 20:42:07 +00:00
|
|
|
if (url.indexOf(mainJs) > -1) {
|
2016-01-09 22:35:40 +00:00
|
|
|
onLoadCb(env[platform].main.root_el);
|
|
|
|
console.log('Done loading Clojure app');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
if (typeof goog === "undefined") {
|
2015-11-27 22:57:14 +00:00
|
|
|
console.log('Loading Closure base.');
|
2016-01-04 22:10:13 +00:00
|
|
|
interceptRequire();
|
2015-12-05 22:53:27 +00:00
|
|
|
importJs('goog/base.js', function () {
|
|
|
|
shimBaseGoog();
|
|
|
|
fakeLocalStorageAndDocument();
|
|
|
|
importJs('cljs_deps.js');
|
|
|
|
importJs('goog/deps.js', function () {
|
2016-01-09 22:35:40 +00:00
|
|
|
debugToLog();
|
2015-12-05 22:53:27 +00:00
|
|
|
// This is needed because of RN packager
|
|
|
|
// seriously React packager? why.
|
|
|
|
var googreq = goog.require;
|
|
|
|
|
|
|
|
googreq('figwheel.connect');
|
|
|
|
});
|
|
|
|
});
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 19:22:53 +00:00
|
|
|
function startApp(appName, platform, devHost) {
|
2016-01-09 22:35:40 +00:00
|
|
|
React.AppRegistry.registerComponent(
|
|
|
|
appName, () => figwheelApp(platform, devHost));
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 22:10:13 +00:00
|
|
|
function withModules(moduleById) {
|
|
|
|
externalModules = moduleById;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
// Goog fixes
|
|
|
|
function shimBaseGoog() {
|
|
|
|
console.info('Shimming goog functions.');
|
2015-11-27 22:57:14 +00:00
|
|
|
goog.basePath = 'goog/';
|
|
|
|
goog.writeScriptSrcNode = importJs;
|
2015-12-05 22:53:27 +00:00
|
|
|
goog.writeScriptTag_ = function (src, optSourceText) {
|
2015-11-27 22:57:14 +00:00
|
|
|
importJs(src);
|
|
|
|
return true;
|
2015-12-05 22:53:27 +00:00
|
|
|
};
|
|
|
|
goog.inHtmlDocument_ = function () {
|
|
|
|
return true;
|
|
|
|
};
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function fakeLocalStorageAndDocument() {
|
|
|
|
window.localStorage = {};
|
2015-12-05 22:53:27 +00:00
|
|
|
window.localStorage.getItem = function () {
|
|
|
|
return 'true';
|
|
|
|
};
|
|
|
|
window.localStorage.setItem = function () {
|
|
|
|
};
|
2015-11-27 22:57:14 +00:00
|
|
|
|
|
|
|
window.document = {};
|
|
|
|
window.document.body = {};
|
2015-12-05 22:53:27 +00:00
|
|
|
window.document.body.dispatchEvent = function () {
|
|
|
|
};
|
|
|
|
window.document.createElement = function () {
|
|
|
|
};
|
2015-11-27 22:57:14 +00:00
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
if (typeof window.location === 'undefined') {
|
|
|
|
window.location = {};
|
|
|
|
}
|
|
|
|
console.debug = console.warn;
|
|
|
|
window.addEventListener = function () {
|
|
|
|
};
|
2016-01-09 22:35:40 +00:00
|
|
|
// make figwheel think that heads-up-display divs are there
|
|
|
|
window.document.querySelector = function (selector) {
|
|
|
|
return {};
|
|
|
|
};
|
|
|
|
window.document.getElementById = function (id) {
|
|
|
|
return {style:{}};
|
|
|
|
};
|
2015-12-05 22:53:27 +00:00
|
|
|
}
|
2015-11-27 22:57:14 +00:00
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
// Figwheel fixes
|
2015-11-27 22:57:14 +00:00
|
|
|
// Used by figwheel - uses importScript to load JS rather than <script>'s
|
2015-12-05 22:53:27 +00:00
|
|
|
function shimJsLoader() {
|
|
|
|
console.info('==== Shimming jsloader ====');
|
|
|
|
goog.net.jsloader.load = function (uri, options) {
|
2015-11-27 22:57:14 +00:00
|
|
|
var deferred = {
|
|
|
|
callbacks: [],
|
|
|
|
errbacks: [],
|
2015-12-05 22:53:27 +00:00
|
|
|
addCallback: function (cb) {
|
2015-11-27 22:57:14 +00:00
|
|
|
deferred.callbacks.push(cb);
|
|
|
|
},
|
2015-12-05 22:53:27 +00:00
|
|
|
addErrback: function (cb) {
|
2015-11-27 22:57:14 +00:00
|
|
|
deferred.errbacks.push(cb);
|
|
|
|
},
|
2015-12-05 22:53:27 +00:00
|
|
|
callAllCallbacks: function () {
|
|
|
|
while (deferred.callbacks.length > 0) {
|
2015-11-27 22:57:14 +00:00
|
|
|
deferred.callbacks.shift()();
|
|
|
|
}
|
|
|
|
},
|
2015-12-05 22:53:27 +00:00
|
|
|
callAllErrbacks: function () {
|
|
|
|
while (deferred.errbacks.length > 0) {
|
2015-11-27 22:57:14 +00:00
|
|
|
deferred.errbacks.shift()();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
// 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);
|
2015-11-27 22:57:14 +00:00
|
|
|
}, 1);
|
|
|
|
|
|
|
|
return deferred;
|
2015-12-05 22:53:27 +00:00
|
|
|
};
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
2016-01-04 22:10:13 +00:00
|
|
|
self = {
|
|
|
|
withModules: withModules,
|
2015-12-05 22:53:27 +00:00
|
|
|
start: startApp
|
2015-11-27 22:57:14 +00:00
|
|
|
};
|
2016-01-04 22:10:13 +00:00
|
|
|
|
|
|
|
module.exports = self;
|