Refactor code runner

This commit is contained in:
Anthony Laibe 2018-08-29 10:49:53 +01:00
parent deb433c0fb
commit cc312a91b6
3 changed files with 112 additions and 89 deletions

View File

@ -12,69 +12,74 @@ class CodeRunner {
this.ipc = options.ipc;
this.commands = [];
this.runCode = new RunCode();
let self = this;
this.registerIpcEvents();
this.IpcClientListen();
this.registerEvents();
this.registerCommands();
this.registerEmbarkJs();
}
registerIpcEvents() {
if (!this.ipc.isServer()) {
return;
}
if (this.ipc.isServer()) {
this.ipc.on('runcode:getCommands', (_err, callback) => {
let result = {web3Config: self.runCode.getWeb3Config(), commands: self.commands};
let result = {web3Config: this.runCode.getWeb3Config(), commands: this.commands};
callback(null, result);
});
}
if (this.ipc.isClient() && this.ipc.connected) {
this.ipc.listenTo('runcode:newCommand', function (command) {
IpcClientListen() {
if (!this.ipc.isClient() || !this.ipc.connected) {
return;
}
this.ipc.listenTo('runcode:newCommand', (command) => {
if (command.varName) {
self.events.emit("runcode:register", command.varName, command.code);
this.events.emit("runcode:register", command.varName, command.code);
} else {
self.events.request("runcode:eval", command.code);
this.events.request("runcode:eval", command.code);
}
});
}
if (!this.ipc.connected) {
this.runCode.registerVar('IpfsApi', IpfsApi);
this.runCode.registerVar('Web3', Web3);
this.runCode.registerVar('EmbarkJS', EmbarkJS);
registerEvents() {
this.events.on("runcode:register", this.registerVar.bind(this));
}
registerCommands() {
this.events.setCommandHandler('runcode:getContext', (cb) => {
cb(this.runCode.context);
});
this.events.setCommandHandler('runcode:eval', this.evalCode.bind(this));
}
registerEmbarkJs() {
if (this.ipc.connected) {
return;
}
this.registerVar('IpfsApi', IpfsApi);
this.registerVar('Web3', Web3);
this.registerVar('EmbarkJS', EmbarkJS);
this.events.on('code-generator-ready', () => {
this.events.request('code-generator:embarkjs:provider-code', (code) => {
this.runCode.doEval(code);
const codeTypes = {
'communication': this.config.communicationConfig || {},
'names': this.config.namesystemConfig || {},
'storage': this.config.storageConfig || {}
};
let initProvidersCode = '';
let initCodes = this.plugins.getPluginsFor('initConsoleCode');
for (let plugin of initCodes) {
for (let codeTypeName of Object.keys(codeTypes)) {
let initCodes = plugin.embarkjs_init_console_code[codeTypeName] || [];
for (let initCode of initCodes) {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, codeTypes[codeTypeName])) {
initProvidersCode += block;
}
}
}
}
this.runCode.doEval(initProvidersCode);
this.evalCode(code);
this.evalCode(this.getInitProviderCode());
});
});
}
this.events.on("runcode:register", (varName, code) => {
if (self.ipc.isServer() && varName !== 'web3') {
self.commands.push({varName, code});
self.ipc.broadcast("runcode:newCommand", {varName, code});
registerVar(varName, code) {
if (this.ipc.isServer() && varName !== 'web3') {
this.commands.push({varName, code});
this.ipc.broadcast("runcode:newCommand", {varName, code});
}
this.runCode.registerVar(varName, code);
}
self.runCode.registerVar(varName, code);
});
this.events.setCommandHandler('runcode:getContext', (cb) => {
cb(self.runCode.context);
});
this.events.setCommandHandler('runcode:eval', (code, cb, forConsoleOnly = false) => {
evalCode(code, cb, forConsoleOnly = false) {
if (!cb) {
cb = function() {};
}
@ -90,11 +95,11 @@ class CodeRunner {
code = `(async function() {${code}})();`;
}
}
let result = self.runCode.doEval(code);
let result = this.runCode.doEval(code);
if (forConsoleOnly && self.ipc.isServer()) {
self.commands.push({code});
self.ipc.broadcast("runcode:newCommand", {code});
if (forConsoleOnly && this.ipc.isServer()) {
this.commands.push({code});
this.ipc.broadcast("runcode:newCommand", {code});
}
if (result instanceof Promise) {
@ -102,9 +107,28 @@ class CodeRunner {
}
cb(null, result);
});
}
getInitProviderCode() {
const codeTypes = {
'communication': this.config.communicationConfig || {},
'names': this.config.namesystemConfig || {},
'storage': this.config.storageConfig || {}
};
return this.plugins.getPluginsFor('initConsoleCode').reduce((acc, plugin) => {
Object.keys(codeTypes).forEach(codeTypeName => {
(plugin.embarkjs_init_console_code[codeTypeName] || []).forEach(initCode => {
let [block, shouldInit] = initCode;
if (shouldInit.call(plugin, codeTypes[codeTypeName])) {
acc += block;
}
});
});
return acc;
}, '');
}
}
module.exports = CodeRunner;

View File

@ -9,7 +9,7 @@ class RunCode {
try {
return vm.runInNewContext(code, this.context);
} catch(e) {
console.log(e.message);
console.error(e.message);
}
}

View File

@ -1,4 +1,3 @@
let __MessageEvents = function() {
this.cb = function() {};
};