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;
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
var React = require('react-native');
|
|
|
|
|
2015-11-27 22:57:14 +00:00
|
|
|
var config = {
|
2015-12-05 22:53:27 +00:00
|
|
|
basePath: "target/",
|
|
|
|
googBasePath: 'goog/',
|
|
|
|
splash: React.createClass({
|
|
|
|
render: function () {
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
2015-11-27 22:57:14 +00:00
|
|
|
};
|
|
|
|
|
2016-01-04 22:10:13 +00:00
|
|
|
var self;
|
2015-12-05 22:53:27 +00:00
|
|
|
var scriptQueue = [];
|
2015-12-06 19:22:53 +00:00
|
|
|
var server = 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 = {};
|
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();
|
|
|
|
if (url.indexOf('jsloader') > -1) {
|
|
|
|
shimJsLoader();
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function asyncImportScripts(path, success, error) {
|
2015-12-06 19:22:53 +00:00
|
|
|
var url = server + '/' + path;
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Async load of javascript files
|
|
|
|
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
|
|
|
|
|
|
|
var filePath = fileBasePath + '/' + src;
|
|
|
|
|
|
|
|
console.info('(importJs) Importing: ' + filePath);
|
|
|
|
asyncImportScripts(filePath, success, error);
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 22:53:27 +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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-12-06 19:22:53 +00:00
|
|
|
function loadApp(platform, devHost) {
|
2016-01-04 22:10:13 +00:00
|
|
|
server = "http://" + devHost + ":8081";
|
2015-12-05 22:53:27 +00:00
|
|
|
fileBasePath = config.basePath + platform;
|
2015-11-27 22:57:14 +00:00
|
|
|
|
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 () {
|
|
|
|
|
|
|
|
// This is needed because of RN packager
|
|
|
|
// seriously React packager? why.
|
|
|
|
var googreq = goog.require;
|
|
|
|
|
|
|
|
googreq('figwheel.connect');
|
|
|
|
googreq('env.' + platform + '.main');
|
|
|
|
|
|
|
|
console.log('Done loading Clojure app');
|
|
|
|
});
|
|
|
|
});
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 19:22:53 +00:00
|
|
|
function startApp(appName, platform, devHost) {
|
2015-12-05 22:53:27 +00:00
|
|
|
React.AppRegistry.registerComponent(appName, () => config.splash);
|
|
|
|
if (typeof goog === "undefined") {
|
2015-12-06 19:22:53 +00:00
|
|
|
loadApp(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 () {
|
|
|
|
};
|
|
|
|
}
|
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;
|