refactor run actions for events to a reduce; add initial plug for shouldDeploy using deployIf

This commit is contained in:
Iuri Matias 2018-08-20 17:25:30 -04:00
parent 3fe1cd4a2c
commit a56431d19a
4 changed files with 53 additions and 9 deletions

View File

@ -138,15 +138,20 @@ Plugins.prototype.runActionsForEvent = function(eventName, args, cb) {
let actionPlugins = this.getPluginsProperty('eventActions', 'eventActions', eventName);
if (actionPlugins.length === 0) {
return cb();
return cb(args);
}
async.eachLimit(actionPlugins, 1, function(plugin, nextEach) {
//async.eachLimit(actionPlugins, 1, function(plugin, nextEach) {
async.reduce(actionPlugins, args, function(current_args, plugin, nextEach) {
if (typeof (args) === 'function') {
plugin.call(plugin, nextEach);
plugin.call(plugin, (params) => {
nextEach(null, (params || current_args));
});
} else {
//plugin.call(plugin, ...args, nextEach);
plugin.call(plugin, args, nextEach);
plugin.call(plugin, args, (params) => {
nextEach(null, (params || current_args));
});
}
}, cb);
};

View File

@ -145,8 +145,15 @@ class ContractDeployer {
return self.deployContract(contract, next);
}
// TODO: this should be a plugin API instead, if not existing, it should by default deploy the contract
self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) {
if (!trackedContract) {
//self.events.request("deploy:contract:shouldDeploy", contract, function(trackedContract) {
self.plugins.emitAndRunActionsForEvent('deploy:contract:shouldDeploy', {contract: contract, shouldDeploy: true}, function(_err, params) {
console.dir(contract.className);
console.dir(arguments);
let trackedContract = params.contract;
if (!params.shouldDeploy) {
return next();
}
if (!trackedContract.address) {
return self.deployContract(contract, next);
}
@ -213,7 +220,9 @@ class ContractDeployer {
});
},
function applyBeforeDeploy(next) {
self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, next);
self.plugins.emitAndRunActionsForEvent('deploy:contract:beforeDeploy', {contract: contract}, () => {
next();
});
},
function getGasPriceForNetwork(next) {
self.events.request("blockchain:gasPrice", (gasPrice) => {

View File

@ -6,6 +6,7 @@ class DeployTracker {
constructor(embark, options) {
this.logger = embark.logger;
this.events = embark.events;
this.embark = embark;
// TODO: unclear where it comes from
this.env = options.env;
@ -24,9 +25,17 @@ class DeployTracker {
self.save();
});
this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => {
//this.events.setCommandHandler("deploy:contract:shouldDeploy", (contract, cb) => {
self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => {
let contract = params.contract;
let trackedContract = self.getContract(contract.className, contract.realRuntimeBytecode, contract.realArgs);
cb(trackedContract);
if (trackedContract) {
params.contract.address = trackedContract.address;
}
if (params.shouldDeploy && trackedContract) {
params.shouldDeploy = true;
}
cb(params);
});
}

View File

@ -12,6 +12,7 @@ class SpecialConfigs {
this.registerAfterDeployAction();
this.registerOnDeployAction();
this.registerDeployIfAction()
}
replaceWithAddresses(cmd, cb) {
@ -104,6 +105,26 @@ class SpecialConfigs {
});
}
registerDeployIfAction() {
const self = this;
self.embark.registerActionForEvent("deploy:contract:shouldDeploy", (params, cb) => {
console.dir(params.contract.className)
console.dir(params.contract.classNideployIf)
self.embark.logger.info("===applying shouldDeploy plugin...");
if (params.contract.deployIf === 'hello') {
console.dir("-------------")
console.dir("-------------")
console.dir("contract has the param")
console.dir("-------------")
console.dir("-------------")
}
params.shouldDeploy = (params.contract.deployIf !== 'hello');
//cb(true, contract);
cb(params);
});
}
}
module.exports = SpecialConfigs;