2015-02-10 19:41:24 +00:00
|
|
|
define(function(require, exports, module) {
|
|
|
|
main.consumes = ["c9", "Plugin", "ext"];
|
|
|
|
main.provides = ["bridge"];
|
|
|
|
return main;
|
|
|
|
|
|
|
|
function main(options, imports, register) {
|
|
|
|
var Plugin = imports.Plugin;
|
|
|
|
var c9 = imports.c9;
|
|
|
|
var ext = imports.ext;
|
2015-04-30 04:01:13 +00:00
|
|
|
|
|
|
|
var JSONStream = require("./json-stream");
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
/***** Initialization *****/
|
|
|
|
|
|
|
|
var plugin = new Plugin("Ajax.org", main.consumes);
|
|
|
|
var emit = plugin.getEmitter();
|
|
|
|
|
2015-02-15 19:48:26 +00:00
|
|
|
var ENABLED = options.startBridge !== false;
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
var stream, api;
|
|
|
|
|
|
|
|
function load(){
|
|
|
|
if (!ENABLED) return;
|
|
|
|
|
|
|
|
ext.loadRemotePlugin("bridge", {
|
|
|
|
code: require("text!./bridge-service.js"),
|
|
|
|
redefine: true
|
|
|
|
}, function(err, remote) {
|
|
|
|
if (err)
|
|
|
|
return console.error(err);
|
|
|
|
|
|
|
|
api = remote;
|
|
|
|
|
2015-04-30 04:01:13 +00:00
|
|
|
api.connect(function(err, meta) {
|
|
|
|
if (err)
|
|
|
|
return console.error(err); // this should never happen
|
|
|
|
|
|
|
|
stream = new JSONStream(meta.stream);
|
|
|
|
|
|
|
|
stream.on("error", function(err) {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on("data", function(payload) {
|
2015-04-30 22:30:29 +00:00
|
|
|
emit("message", {
|
|
|
|
message: payload.message,
|
|
|
|
respond: function(err, message){
|
|
|
|
stream.write({
|
|
|
|
id: payload.id,
|
|
|
|
message: message,
|
|
|
|
error: err
|
|
|
|
});
|
|
|
|
}
|
2015-04-30 04:01:13 +00:00
|
|
|
});
|
2015-04-30 22:30:29 +00:00
|
|
|
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
stream.on("close", function(){
|
2015-04-30 04:01:13 +00:00
|
|
|
load();
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-30 04:01:13 +00:00
|
|
|
window.addEventListener("unload", function(){
|
|
|
|
api && api.disconnect();
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 04:01:13 +00:00
|
|
|
function write(json){
|
|
|
|
if (!stream) return false;
|
2015-04-30 22:30:29 +00:00
|
|
|
stream.write(json);
|
2015-04-30 04:01:13 +00:00
|
|
|
return true;
|
2015-02-10 19:41:24 +00:00
|
|
|
}
|
2015-04-30 04:01:13 +00:00
|
|
|
|
2015-02-10 19:41:24 +00:00
|
|
|
/***** Methods *****/
|
|
|
|
|
|
|
|
plugin.on("load", function(){
|
|
|
|
c9.on("connect", load, plugin);
|
|
|
|
});
|
|
|
|
|
|
|
|
plugin.on("unload", function(){
|
|
|
|
api && api.disconnect();
|
2015-04-30 04:01:13 +00:00
|
|
|
stream = null;
|
|
|
|
api = null;
|
2015-02-10 19:41:24 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
/***** Register and define API *****/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Bridge To Communicate from CLI to IDE
|
|
|
|
**/
|
2015-04-30 04:01:13 +00:00
|
|
|
plugin.freezePublicAPI({
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
write: write
|
|
|
|
});
|
2015-02-10 19:41:24 +00:00
|
|
|
|
|
|
|
register(null, {
|
|
|
|
bridge: plugin
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|