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;
|
2016-03-21 22:05:45 +00:00
|
|
|
var debugEnabled = false;
|
2015-11-27 22:57:14 +00:00
|
|
|
|
|
|
|
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-05-12 14:29:41 +00:00
|
|
|
var React = require('react');
|
2017-07-14 18:37:33 +00:00
|
|
|
var createReactClass = require('create-react-class');
|
2016-05-12 14:29:41 +00:00
|
|
|
var ReactNative = require('react-native');
|
2016-03-20 10:25:45 +00:00
|
|
|
var WebSocket = require('WebSocket');
|
2016-01-04 22:10:13 +00:00
|
|
|
var self;
|
2015-12-05 22:53:27 +00:00
|
|
|
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 = {};
|
2017-09-23 00:06:02 +00:00
|
|
|
var evalListeners = [];
|
|
|
|
var asyncImportChain = new Promise(function (succ,fail) {succ(true);});
|
|
|
|
|
|
|
|
function fireEvalListenters(url) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This is simply demonstrating that we can receive and react to
|
|
|
|
* 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-01-09 22:35:40 +00:00
|
|
|
|
|
|
|
var figwheelApp = function (platform, devHost) {
|
2017-07-14 18:37:33 +00:00
|
|
|
return createReactClass({
|
2016-01-09 22:35:40 +00:00
|
|
|
getInitialState: function () {
|
|
|
|
return {loaded: false}
|
|
|
|
},
|
|
|
|
render: function () {
|
|
|
|
if (!this.state.loaded) {
|
|
|
|
var plainStyle = {flex: 1, alignItems: 'center', justifyContent: 'center'};
|
|
|
|
return (
|
2016-05-12 14:29:41 +00:00
|
|
|
<ReactNative.View style={plainStyle}>
|
|
|
|
<ReactNative.Text>Waiting for Figwheel to load files.</ReactNative.Text>
|
|
|
|
</ReactNative.View>
|
2016-01-09 22:35:40 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return this.state.root;
|
|
|
|
},
|
2017-09-23 00:06:02 +00:00
|
|
|
|
2016-01-09 22:35:40 +00:00
|
|
|
componentDidMount: function () {
|
|
|
|
var app = this;
|
|
|
|
if (typeof goog === "undefined") {
|
2016-03-20 10:25:45 +00:00
|
|
|
loadApp(platform, devHost, function (appRoot) {
|
2017-09-23 00:06:02 +00:00
|
|
|
app.setState({root: appRoot, loaded: true});
|
|
|
|
listenToFigwheelMessages();
|
2016-01-09 22:35:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
2015-12-05 22:53:27 +00:00
|
|
|
|
2016-03-21 22:05:45 +00:00
|
|
|
function logDebug(msg) {
|
|
|
|
if (debugEnabled) {
|
|
|
|
console.log(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
var isChrome = function () {
|
|
|
|
return typeof importScripts === "function"
|
|
|
|
};
|
|
|
|
|
|
|
|
function asyncImportScripts(url, success, error) {
|
2016-03-21 22:05:45 +00:00
|
|
|
logDebug('(asyncImportScripts) Importing: ' + url);
|
2017-09-23 00:06:02 +00:00
|
|
|
asyncImportChain =
|
|
|
|
asyncImportChain
|
|
|
|
.then(function (v) {return fetch(url);})
|
2015-12-05 22:53:27 +00:00
|
|
|
.then(function (response) {
|
2017-09-23 00:06:02 +00:00
|
|
|
if(response.ok)
|
|
|
|
return response.text();
|
|
|
|
throw new Error("Failed to Fetch: " + url + " - Perhaps your project was cleaned and you haven't recompiled?")
|
2015-12-05 22:53:27 +00:00
|
|
|
})
|
|
|
|
.then(function (responseText) {
|
2017-09-23 00:06:02 +00:00
|
|
|
evaluate(responseText);
|
|
|
|
fireEvalListenters(url);
|
|
|
|
success();
|
|
|
|
return true;
|
2015-12-05 22:53:27 +00:00
|
|
|
})
|
2017-09-23 00:06:02 +00:00
|
|
|
.catch(function (e) {
|
|
|
|
console.error(e);
|
|
|
|
error();
|
|
|
|
return true;
|
2015-12-05 22:53:27 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-01-20 23:14:35 +00:00
|
|
|
function syncImportScripts(url, success, error) {
|
|
|
|
try {
|
|
|
|
importScripts(url);
|
2016-03-21 22:05:45 +00:00
|
|
|
logDebug('Evaluated: ' + url);
|
2017-09-23 00:06:02 +00:00
|
|
|
fireEvalListenters(url);
|
2016-01-20 23:14:35 +00:00
|
|
|
success();
|
|
|
|
} catch (e) {
|
2016-05-02 19:55:20 +00:00
|
|
|
console.error(e);
|
2016-01-20 23:14:35 +00:00
|
|
|
error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loads js file sync if possible or async.
|
2015-12-05 22:53:27 +00:00
|
|
|
function importJs(src, success, error) {
|
2017-09-23 00:06:02 +00:00
|
|
|
var noop = function(){};
|
|
|
|
success = (typeof success == 'function') ? success : noop;
|
|
|
|
error = (typeof error == 'function') ? error : noop;
|
|
|
|
logDebug('(importJs) Importing: ' + src);
|
2016-01-20 23:14:35 +00:00
|
|
|
if (isChrome()) {
|
2017-09-23 00:06:02 +00:00
|
|
|
syncImportScripts(src, success, error);
|
2016-01-20 23:14:35 +00:00
|
|
|
} else {
|
2017-09-23 00:06:02 +00:00
|
|
|
asyncImportScripts(src, success, error);
|
2016-01-20 23:14:35 +00:00
|
|
|
}
|
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-20 23:14:35 +00:00
|
|
|
function serverBaseUrl(host) {
|
|
|
|
return "http://" + host + ":" + config.serverPort
|
|
|
|
}
|
|
|
|
|
2017-09-23 00:06:02 +00:00
|
|
|
function isUnDefined(x) {
|
|
|
|
return typeof x == "undefined";
|
2016-03-20 10:25:45 +00:00
|
|
|
}
|
2016-03-20 11:10:09 +00:00
|
|
|
|
2017-09-23 00:06:02 +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-01-09 22:35:40 +00:00
|
|
|
function loadApp(platform, devHost, onLoadCb) {
|
2017-09-23 00:06:02 +00:00
|
|
|
// serverHost = devHost;
|
|
|
|
var fileBasePath = serverBaseUrl((isChrome() ? "localhost" : devHost)) + "/" + config.basePath + platform;
|
2015-11-27 22:57:14 +00:00
|
|
|
|
2016-01-09 22:35:40 +00:00
|
|
|
// 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) {
|
2017-09-23 00:06:02 +00:00
|
|
|
assertRootElExists(platform);
|
2016-01-09 22:35:40 +00:00
|
|
|
onLoadCb(env[platform].main.root_el);
|
2016-03-21 22:05:45 +00:00
|
|
|
console.info('Done loading Clojure app');
|
2016-01-09 22:35:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
if (typeof goog === "undefined") {
|
2016-03-21 22:05:45 +00:00
|
|
|
console.info('Loading Closure base.');
|
2016-01-04 22:10:13 +00:00
|
|
|
interceptRequire();
|
2017-09-23 00:06:02 +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 () {
|
2015-12-05 22:53:27 +00:00
|
|
|
// This is needed because of RN packager
|
|
|
|
// seriously React packager? why.
|
|
|
|
var googreq = goog.require;
|
|
|
|
|
2017-04-29 20:16:37 +00:00
|
|
|
googreq('figwheel.connect.build_' + platform);
|
2015-12-05 22:53:27 +00:00
|
|
|
});
|
|
|
|
});
|
2015-11-27 22:57:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-06 19:22:53 +00:00
|
|
|
function startApp(appName, platform, devHost) {
|
2016-05-12 14:29:41 +00:00
|
|
|
ReactNative.AppRegistry.registerComponent(
|
2016-01-09 22:35:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-23 00:06:02 +00:00
|
|
|
function figwheelImportScript(uri, callback) {
|
|
|
|
importJs(uri.toString(),
|
|
|
|
function () {callback(true);},
|
|
|
|
function () {callback(false);})
|
|
|
|
}
|
|
|
|
|
2015-12-05 22:53:27 +00:00
|
|
|
// Goog fixes
|
2017-09-23 00:06:02 +00:00
|
|
|
function shimBaseGoog(basePath) {
|
2015-12-05 22:53:27 +00:00
|
|
|
console.info('Shimming goog functions.');
|
2017-09-23 00:06:02 +00:00
|
|
|
goog.basePath = basePath + '/' + config.googBasePath;
|
|
|
|
goog.global.FIGWHEEL_WEBSOCKET_CLASS = WebSocket;
|
|
|
|
goog.global.FIGWHEEL_IMPORT_SCRIPT = figwheelImportScript;
|
2015-11-27 22:57:14 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
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
|
|
|
|
2017-09-23 00:06:02 +00:00
|
|
|
module.exports = self;
|