2018-05-23 15:16:56 +00:00
|
|
|
// still needs to be run on a separate file due to the global context
|
|
|
|
var RunCode = require('./runCode.js');
|
2018-05-21 21:22:19 +00:00
|
|
|
|
|
|
|
class CodeRunner {
|
|
|
|
constructor(options) {
|
|
|
|
this.plugins = options.plugins;
|
|
|
|
this.logger = options.logger;
|
|
|
|
this.events = options.events;
|
2018-08-08 12:42:45 +00:00
|
|
|
this.ipc = options.ipc;
|
|
|
|
this.commands = [];
|
|
|
|
let self = this;
|
2018-05-23 15:16:56 +00:00
|
|
|
// necessary to init the context
|
|
|
|
RunCode.initContext();
|
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
if (this.ipc.isServer()) {
|
|
|
|
this.ipc.on('runcode:getCommands', (_, callback) => {
|
|
|
|
let result = {web3Config: RunCode.getWeb3Config(), commands: self.commands};
|
|
|
|
callback(null, result);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.ipc.isClient() && this.ipc.connected) {
|
|
|
|
this.ipc.listenTo('runcode:newCommand', function (command) {
|
|
|
|
if (command.varName) {
|
|
|
|
self.events.emit("runcode:register", command.varName, command.code);
|
|
|
|
} else {
|
|
|
|
self.events.request("runcode:eval", command.code);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-23 15:16:56 +00:00
|
|
|
this.events.on("runcode:register", (varName, code) => {
|
2018-08-08 12:42:45 +00:00
|
|
|
if (self.ipc.isServer() && varName !== 'web3') {
|
|
|
|
self.commands.push({varName, code});
|
|
|
|
self.ipc.broadcast("runcode:newCommand", {varName, code});
|
|
|
|
}
|
2018-05-23 15:16:56 +00:00
|
|
|
RunCode.registerVar(varName, code);
|
|
|
|
});
|
2018-05-21 21:22:19 +00:00
|
|
|
|
2018-08-08 12:42:45 +00:00
|
|
|
this.events.setCommandHandler('runcode:eval', (code, cb, dashboard = false) => {
|
2018-06-14 15:10:31 +00:00
|
|
|
if (!cb) {
|
|
|
|
cb = function() {};
|
|
|
|
}
|
2018-08-08 12:42:45 +00:00
|
|
|
let result = RunCode.doEval(code);
|
|
|
|
if (!dashboard && self.ipc.isServer()) {
|
|
|
|
self.commands.push({code});
|
|
|
|
self.ipc.broadcast("runcode:newCommand", {code});
|
2018-05-23 15:16:56 +00:00
|
|
|
}
|
2018-08-08 12:42:45 +00:00
|
|
|
cb(null, result);
|
2018-05-23 15:16:56 +00:00
|
|
|
});
|
2018-05-21 21:22:19 +00:00
|
|
|
}
|
2018-08-08 12:42:45 +00:00
|
|
|
|
2018-05-21 21:22:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = CodeRunner;
|