mirror of https://github.com/embarklabs/embark.git
Refactor code runner
This commit is contained in:
parent
deb433c0fb
commit
cc312a91b6
|
@ -12,99 +12,123 @@ 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();
|
||||
}
|
||||
|
||||
if (this.ipc.isServer()) {
|
||||
this.ipc.on('runcode:getCommands', (_err, callback) => {
|
||||
let result = {web3Config: self.runCode.getWeb3Config(), commands: self.commands};
|
||||
callback(null, result);
|
||||
});
|
||||
registerIpcEvents() {
|
||||
if (!this.ipc.isServer()) {
|
||||
return;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (!this.ipc.connected) {
|
||||
this.runCode.registerVar('IpfsApi', IpfsApi);
|
||||
this.runCode.registerVar('Web3', Web3);
|
||||
this.runCode.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.events.on("runcode:register", (varName, code) => {
|
||||
if (self.ipc.isServer() && varName !== 'web3') {
|
||||
self.commands.push({varName, code});
|
||||
self.ipc.broadcast("runcode:newCommand", {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) => {
|
||||
if (!cb) {
|
||||
cb = function() {};
|
||||
}
|
||||
const awaitIdx = code.indexOf('await');
|
||||
if (awaitIdx > -1) {
|
||||
if (awaitIdx < 2) {
|
||||
let end = code.length;
|
||||
if (code[end - 1] === ';') {
|
||||
end--; // Remove the `;` because we add function calls
|
||||
}
|
||||
code = code.substring(5, end); // remove await keyword
|
||||
} else {
|
||||
code = `(async function() {${code}})();`;
|
||||
}
|
||||
}
|
||||
let result = self.runCode.doEval(code);
|
||||
|
||||
if (forConsoleOnly && self.ipc.isServer()) {
|
||||
self.commands.push({code});
|
||||
self.ipc.broadcast("runcode:newCommand", {code});
|
||||
}
|
||||
|
||||
if (result instanceof Promise) {
|
||||
return result.then((value) => cb(null, value)).catch(cb);
|
||||
}
|
||||
|
||||
cb(null, result);
|
||||
this.ipc.on('runcode:getCommands', (_err, callback) => {
|
||||
let result = {web3Config: this.runCode.getWeb3Config(), commands: this.commands};
|
||||
callback(null, result);
|
||||
});
|
||||
}
|
||||
|
||||
IpcClientListen() {
|
||||
if (!this.ipc.isClient() || !this.ipc.connected) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.ipc.listenTo('runcode:newCommand', (command) => {
|
||||
if (command.varName) {
|
||||
this.events.emit("runcode:register", command.varName, command.code);
|
||||
} else {
|
||||
this.events.request("runcode:eval", command.code);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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.evalCode(code);
|
||||
this.evalCode(this.getInitProviderCode());
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
evalCode(code, cb, forConsoleOnly = false) {
|
||||
if (!cb) {
|
||||
cb = function() {};
|
||||
}
|
||||
const awaitIdx = code.indexOf('await');
|
||||
if (awaitIdx > -1) {
|
||||
if (awaitIdx < 2) {
|
||||
let end = code.length;
|
||||
if (code[end - 1] === ';') {
|
||||
end--; // Remove the `;` because we add function calls
|
||||
}
|
||||
code = code.substring(5, end); // remove await keyword
|
||||
} else {
|
||||
code = `(async function() {${code}})();`;
|
||||
}
|
||||
}
|
||||
let result = this.runCode.doEval(code);
|
||||
|
||||
if (forConsoleOnly && this.ipc.isServer()) {
|
||||
this.commands.push({code});
|
||||
this.ipc.broadcast("runcode:newCommand", {code});
|
||||
}
|
||||
|
||||
if (result instanceof Promise) {
|
||||
return result.then((value) => cb(null, value)).catch(cb);
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
@ -9,7 +9,7 @@ class RunCode {
|
|||
try {
|
||||
return vm.runInNewContext(code, this.context);
|
||||
} catch(e) {
|
||||
console.log(e.message);
|
||||
console.error(e.message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
let __MessageEvents = function() {
|
||||
this.cb = function() {};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue