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;
|
2018-07-23 15:21:31 +00:00
|
|
|
var debugEnabled = true;
|
2016-02-22 22:04:42 +00:00
|
|
|
|
|
|
|
var config = {
|
|
|
|
basePath: "target/",
|
|
|
|
googBasePath: 'goog/',
|
|
|
|
serverPort: 8081
|
|
|
|
};
|
|
|
|
|
2016-07-20 09:03:05 +00:00
|
|
|
var React = require('react');
|
2017-10-19 04:38:20 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2016-07-20 09:03:05 +00:00
|
|
|
var ReactNative = require('react-native');
|
2016-02-22 22:04:42 +00:00
|
|
|
var self;
|
|
|
|
var evaluate = eval; // This is needed, direct calls to eval does not work (RN packager???)
|
|
|
|
var externalModules = {};
|
2017-10-19 04:38:20 +00:00
|
|
|
var evalListeners = {};
|
|
|
|
var asyncImportChain = new Promise(function (succ,fail) {succ(true);});
|
|
|
|
|
|
|
|
function fireEvalListenters(url) {
|
|
|
|
Object.values(evalListeners).forEach(function (listener) {
|
|
|
|
listener(url)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatCompileError(msg) {
|
|
|
|
var errorStr = "Figwheel Compile Exception: "
|
|
|
|
var data = msg['exception-data'];
|
|
|
|
if(data['message']) {
|
|
|
|
errorStr += data['message'] + " ";
|
|
|
|
}
|
|
|
|
if(data['file']) {
|
|
|
|
errorStr += "in file " + data['file'] + " ";
|
|
|
|
}
|
|
|
|
if(data['line']) {
|
|
|
|
errorStr += "at line " + data['line'];
|
|
|
|
}
|
|
|
|
if(data['column']) {
|
|
|
|
errorStr += ", column " + data['column'];
|
|
|
|
}
|
|
|
|
return errorStr;
|
|
|
|
}
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
/* This is simply demonstrating that we can receive and react to
|
2017-10-19 04:38:20 +00:00
|
|
|
* arbitrary messages from Figwheel this will enable creating a nicer
|
|
|
|
* feedback system in the Figwheel top level React component.
|
|
|
|
*/
|
|
|
|
function figwheelMessageHandler(msg) {
|
|
|
|
if(msg["msg-name"] == "compile-failed") {
|
|
|
|
console.warn(formatCompileError(msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function listenToFigwheelMessages() {
|
|
|
|
if(figwheel.client.add_json_message_watch) {
|
|
|
|
figwheel.client.add_json_message_watch("ReactNativeMessageIntercept",
|
|
|
|
figwheelMessageHandler);
|
|
|
|
}
|
|
|
|
}
|
2016-02-22 22:04:42 +00:00
|
|
|
|
|
|
|
var figwheelApp = function (platform, devHost) {
|
2017-10-19 04:38:20 +00:00
|
|
|
return createReactClass({
|
2016-02-22 22:04:42 +00:00
|
|
|
getInitialState: function () {
|
|
|
|
return {loaded: false}
|
|
|
|
},
|
|
|
|
render: function () {
|
|
|
|
if (!this.state.loaded) {
|
|
|
|
var plainStyle = {flex: 1, alignItems: 'center', justifyContent: 'center'};
|
|
|
|
return (
|
2016-07-20 09:03:05 +00:00
|
|
|
<ReactNative.View style={plainStyle}>
|
|
|
|
<ReactNative.Text>Waiting for Figwheel to load files.</ReactNative.Text>
|
|
|
|
</ReactNative.View>
|
2016-02-22 22:04:42 +00:00
|
|
|
);
|
|
|
|
}
|
2018-10-27 19:24:47 +00:00
|
|
|
return React.createElement(this.state.root, this.props);
|
2016-02-22 22:04:42 +00:00
|
|
|
},
|
2017-10-19 04:38:20 +00:00
|
|
|
|
2016-02-22 22:04:42 +00:00
|
|
|
componentDidMount: function () {
|
|
|
|
var app = this;
|
|
|
|
if (typeof goog === "undefined") {
|
2016-07-20 09:03:05 +00:00
|
|
|
loadApp(platform, devHost, function (appRoot) {
|
2017-10-19 04:38:20 +00:00
|
|
|
app.setState({root: appRoot, loaded: true});
|
|
|
|
listenToFigwheelMessages();
|
2016-02-22 22:04:42 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
2016-07-20 09:03:05 +00:00
|
|
|
function logDebug(msg) {
|
|
|
|
if (debugEnabled) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:04:42 +00:00
|
|
|
var isChrome = function () {
|
|
|
|
return typeof importScripts === "function"
|
|
|
|
};
|
|
|
|
|
2018-07-23 15:21:31 +00:00
|
|
|
async function getUrlText(url) {
|
|
|
|
const text = await fetch(url).then(response => {
|
|
|
|
if(!response.ok) {
|
|
|
|
throw new Error("Failed to Fetch: " + url + " - Perhaps your project was cleaned and you haven't recompiled?");
|
|
|
|
}
|
|
|
|
return response.text()
|
|
|
|
});
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
var ATTEMPTS_COUNT = 3;
|
|
|
|
async function asyncImportScripts(url, success, error) {
|
|
|
|
|
|
|
|
var attempt = 0;
|
|
|
|
var text = await getUrlText(url);
|
|
|
|
|
|
|
|
while(attempt < ATTEMPTS_COUNT && text.length === 0)
|
|
|
|
{
|
|
|
|
text = await getUrlText(url);
|
|
|
|
++attempt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!text || 0 === text.length) {
|
|
|
|
console.log("Can't fetch file: ", url)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
evaluate(text);
|
|
|
|
fireEvalListenters(url);
|
|
|
|
success();
|
2016-02-22 22:04:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function syncImportScripts(url, success, error) {
|
|
|
|
try {
|
|
|
|
importScripts(url);
|
2016-07-20 09:03:05 +00:00
|
|
|
logDebug('Evaluated: ' + url);
|
2017-10-19 04:38:20 +00:00
|
|
|
fireEvalListenters(url);
|
2016-02-22 22:04:42 +00:00
|
|
|
success();
|
|
|
|
} catch (e) {
|
2016-07-20 09:03:05 +00:00
|
|
|
console.error(e);
|
2016-02-22 22:04:42 +00:00
|
|
|
error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads js file sync if possible or async.
|
|
|
|
function importJs(src, success, error) {
|
2017-10-19 04:38:20 +00:00
|
|
|
var noop = function(){};
|
|
|
|
success = (typeof success == 'function') ? success : noop;
|
|
|
|
error = (typeof error == 'function') ? error : noop;
|
|
|
|
logDebug('(importJs) Importing: ' + src);
|
2016-02-22 22:04:42 +00:00
|
|
|
if (isChrome()) {
|
2017-10-19 04:38:20 +00:00
|
|
|
syncImportScripts(src, success, error);
|
2016-02-22 22:04:42 +00:00
|
|
|
} else {
|
2018-07-23 15:21:31 +00:00
|
|
|
asyncImportChain = asyncImportChain.then(function (v) {return asyncImportScripts(src, success, error);})
|
2016-02-22 22:04:42 +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);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function serverBaseUrl(host) {
|
|
|
|
return "http://" + host + ":" + config.serverPort
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:38:20 +00:00
|
|
|
function isUnDefined(x) {
|
|
|
|
return typeof x == "undefined";
|
2016-07-20 09:03:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-19 04:38:20 +00:00
|
|
|
// unlikely to happen but it happened to me a couple of times so ...
|
|
|
|
function assertRootElExists(platform) {
|
|
|
|
var basicMessage = "ClojureScript project didn't compile, or didn't load correctly.";
|
|
|
|
if(isUnDefined(env)) {
|
|
|
|
throw new Error("Critical Error: env namespace not defined - " + basicMessage);
|
|
|
|
} else if(isUnDefined(env[platform])) {
|
|
|
|
throw new Error("Critical Error: env." + platform + " namespace not defined - " + basicMessage);
|
|
|
|
} else if(isUnDefined(env[platform].main)) {
|
|
|
|
throw new Error("Critical Error: env." + platform + ".main namespace not defined - " + basicMessage);
|
|
|
|
} else if(isUnDefined(env[platform].main.root_el)) {
|
|
|
|
throw new Error("Critical Error: env." +
|
|
|
|
platform + ".main namespace doesn't define a root-el which should hold the root react node of your app.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:04:42 +00:00
|
|
|
function loadApp(platform, devHost, onLoadCb) {
|
2017-10-19 04:38:20 +00:00
|
|
|
var fileBasePath = serverBaseUrl((isChrome() ? "localhost" : devHost)) + "/" + config.basePath + platform;
|
2016-02-22 22:04:42 +00:00
|
|
|
|
|
|
|
// callback when app is ready to get the reloadable component
|
2017-10-19 04:38:20 +00:00
|
|
|
var mainJs = `/env/${platform}/main.js`;
|
|
|
|
evalListeners.waitForFinalEval = function (url) {
|
2016-02-22 22:04:42 +00:00
|
|
|
if (url.indexOf(mainJs) > -1) {
|
2017-10-19 04:38:20 +00:00
|
|
|
assertRootElExists(platform);
|
2016-02-22 22:04:42 +00:00
|
|
|
onLoadCb(env[platform].main.root_el);
|
2016-07-20 09:03:05 +00:00
|
|
|
console.info('Done loading Clojure app');
|
2017-10-19 04:38:20 +00:00
|
|
|
delete evalListeners.waitForFinalEval;
|
2016-02-22 22:04:42 +00:00
|
|
|
}
|
2017-10-19 04:38:20 +00:00
|
|
|
};
|
2016-02-22 22:04:42 +00:00
|
|
|
|
|
|
|
if (typeof goog === "undefined") {
|
2016-07-20 09:03:05 +00:00
|
|
|
console.info('Loading Closure base.');
|
2016-02-22 22:04:42 +00:00
|
|
|
interceptRequire();
|
2017-10-19 04:38:20 +00:00
|
|
|
// need to know base path here
|
|
|
|
importJs(fileBasePath + '/goog/base.js', function () {
|
|
|
|
shimBaseGoog(fileBasePath);
|
|
|
|
importJs(fileBasePath + '/cljs_deps.js');
|
|
|
|
importJs(fileBasePath + '/goog/deps.js', function () {
|
2016-02-22 22:04:42 +00:00
|
|
|
// This is needed because of RN packager
|
|
|
|
// seriously React packager? why.
|
|
|
|
var googreq = goog.require;
|
2018-03-05 12:45:51 +00:00
|
|
|
// NOTE: :closure-defines is not working with RN
|
|
|
|
goog.global.CLOSURE_UNCOMPILED_DEFINES = {
|
|
|
|
"re_frame.trace.trace_enabled_QMARK_":true,
|
|
|
|
"day8.re_frame.tracing.trace_enabled_QMARK_": true
|
|
|
|
};
|
2017-10-19 04:38:20 +00:00
|
|
|
googreq(`env.${platform}.main`);
|
2016-02-22 22:04:42 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function startApp(appName, platform, devHost) {
|
2016-07-20 09:03:05 +00:00
|
|
|
ReactNative.AppRegistry.registerComponent(
|
2016-02-22 22:04:42 +00:00
|
|
|
appName, () => figwheelApp(platform, devHost));
|
|
|
|
}
|
|
|
|
|
|
|
|
function withModules(moduleById) {
|
|
|
|
externalModules = moduleById;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-10-19 04:38:20 +00:00
|
|
|
function figwheelImportScript(uri, callback) {
|
|
|
|
importJs(uri.toString(),
|
|
|
|
function () {callback(true);},
|
|
|
|
function () {callback(false);})
|
|
|
|
}
|
|
|
|
|
2016-02-22 22:04:42 +00:00
|
|
|
// Goog fixes
|
2017-10-19 04:38:20 +00:00
|
|
|
function shimBaseGoog(basePath) {
|
2016-02-22 22:04:42 +00:00
|
|
|
console.info('Shimming goog functions.');
|
2017-10-19 04:38:20 +00:00
|
|
|
goog.basePath = basePath + '/' + config.googBasePath;
|
|
|
|
goog.global.FIGWHEEL_WEBSOCKET_CLASS = WebSocket;
|
|
|
|
goog.global.FIGWHEEL_IMPORT_SCRIPT = figwheelImportScript;
|
2016-02-22 22:04:42 +00:00
|
|
|
goog.writeScriptSrcNode = importJs;
|
|
|
|
goog.writeScriptTag_ = function (src, optSourceText) {
|
|
|
|
importJs(src);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
self = {
|
|
|
|
withModules: withModules,
|
|
|
|
start: startApp
|
|
|
|
};
|
|
|
|
|
2016-11-02 20:27:31 +00:00
|
|
|
module.exports = self;
|