refactor(@embark/code_generator) simplify listenToCommands function; use this instead of self; use bind for functions

This commit is contained in:
Iuri Matias 2019-01-31 06:12:39 +01:00
parent 7d325ee28b
commit 1c10b17e15
1 changed files with 19 additions and 31 deletions

View File

@ -42,60 +42,48 @@ class CodeGenerator {
} }
listenToCommands() { listenToCommands() {
let self = this; this.events.on('config:load:contracts', this.generateContractConfig.bind(this));
this.events.on('config:load:storage', this.generateStorageConfig.bind(this));
this.events.on('config:load:contracts', (contractConfig) => { this.events.on('config:load:communication', this.generateCommunicationConfig.bind(this));
this.generateContractConfig(contractConfig);
});
this.events.on('config:load:storage', (storageConfig) => {
this.generateStorageConfig(storageConfig);
});
this.events.on('config:load:communication', (communicationConfig) => {
this.generateCommunicationConfig(communicationConfig);
});
this.events.setCommandHandler('code', function(cb) { this.events.setCommandHandler('code', function(cb) {
self.events.request("contracts:list", (_err, contractsList) => { this.events.request("contracts:list", (_err, contractsList) => {
let embarkJSABI = self.generateABI(contractsList, {useEmbarkJS: true}); let embarkJSABI = this.generateABI(contractsList, {useEmbarkJS: true});
let contractsJSON = self.generateContractsJSON(contractsList); let contractsJSON = this.generateContractsJSON(contractsList);
cb(embarkJSABI, contractsJSON); cb(embarkJSABI, contractsJSON);
}); });
}); });
this.events.setCommandHandler('code-generator:web3js', function(cb) { this.events.setCommandHandler('code-generator:web3js', this.buildWeb3JS.bind(this));
self.buildWeb3JS(cb);
});
self.events.setCommandHandler('code-generator:contract', (contractName, cb) => { this.events.setCommandHandler('code-generator:contract', (contractName, cb) => {
self.events.request('contracts:contract', contractName, (contract) => { this.events.request('contracts:contract', contractName, (contract) => {
self.buildContractJS(contractName, self.generateContractJSON(contract, contract), cb); this.buildContractJS(contractName, this.generateContractJSON(contract, contract), cb);
}); });
}); });
self.events.setCommandHandler('code-generator:contract:vanilla', (contract, gasLimit, cb) => { this.events.setCommandHandler('code-generator:contract:vanilla', (contract, gasLimit, cb) => {
cb(self.generateContractCode(contract, gasLimit)); cb(this.generateContractCode(contract, gasLimit));
}); });
self.events.setCommandHandler('code-generator:contract:custom', (contract, cb) => { this.events.setCommandHandler('code-generator:contract:custom', (contract, cb) => {
const customCode = self.generateCustomContractCode(contract); const customCode = this.generateCustomContractCode(contract);
if (!customCode) { if (!customCode) {
// Fallback to generate code from vanilla contract generator. // Fallback to generate code from vanilla contract generator.
// //
// TODO: can be moved into a afterDeploy event // TODO: can be moved into a afterDeploy event
// just need to figure out the gasLimit coupling issue // just need to figure out the gasLimit coupling issue
return cb(self.generateContractCode(contract, contract._gasLimit || false)); return cb(this.generateContractCode(contract, contract._gasLimit || false));
} }
cb(customCode); cb(customCode);
}); });
self.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => { this.events.setCommandHandler('code-generator:embarkjs:provider-code', (cb) => {
cb(self.getEmbarkJsProviderCode()); cb(this.getEmbarkJsProviderCode());
}); });
self.events.setCommandHandler('code-generator:embarkjs:init-provider-code', (cb) => { this.events.setCommandHandler('code-generator:embarkjs:init-provider-code', (cb) => {
cb(self.getInitProviderCode()); cb(this.getInitProviderCode());
}); });
} }