From 4f45b4fa1399067d603d9258fbbd73aeeba4fef5 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Mon, 28 May 2018 18:49:18 -0400 Subject: [PATCH] add new plugin method for generalized event actions --- lib/contracts/deploy_manager.js | 13 +++++++------ lib/core/plugin.js | 9 +++++++++ lib/core/plugins.js | 18 ++++++++++++++++-- lib/modules/specialconfigs/index.js | 3 ++- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/lib/contracts/deploy_manager.js b/lib/contracts/deploy_manager.js index e5954138..d436a4e9 100644 --- a/lib/contracts/deploy_manager.js +++ b/lib/contracts/deploy_manager.js @@ -84,13 +84,14 @@ class DeployManager { }); }, function runAfterDeploy(callback) { - let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions'); + self.plugins.runActionsForEvent('contracts:deploy:afterAll', callback); + //let afterDeployPlugins = self.plugins.getPluginsProperty('afterContractsDeployActions', 'afterContractsDeployActions'); - async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) { - plugin.call(plugin, nextEach); - }, () => { - callback(); - }); + //async.eachLimit(afterDeployPlugins, 1, function(plugin, nextEach) { + // plugin.call(plugin, nextEach); + //}, () => { + // callback(); + //}); } ], function (err, _result) { done(err); diff --git a/lib/core/plugin.js b/lib/core/plugin.js index 17feb99a..f2320bb2 100644 --- a/lib/core/plugin.js +++ b/lib/core/plugin.js @@ -27,6 +27,7 @@ var Plugin = function(options) { this.embarkjs_init_code = {}; this.afterContractsDeployActions = []; this.onDeployActions = []; + this.eventActions = {}; this.logger = options.logger; this.events = options.events; this.config = options.config; @@ -215,6 +216,14 @@ Plugin.prototype.registerOnDeployContracts = function(cb) { this.pluginTypes.push('onDeployActions'); }; +Plugin.prototype.registerActionForEvent = function(eventName, cb) { + if (!this.eventActions[eventName]) { + this.eventActions[eventName] = []; + } + this.eventActions[eventName].push(cb); + this.pluginTypes.push('eventActions'); +} + Plugin.prototype.runFilePipeline = function() { var self = this; diff --git a/lib/core/plugins.js b/lib/core/plugins.js index 78a21975..8efa8442 100644 --- a/lib/core/plugins.js +++ b/lib/core/plugins.js @@ -1,3 +1,4 @@ +const async = require('async'); var Plugin = require('./plugin.js'); var utils = require('../utils/utils.js'); @@ -98,18 +99,31 @@ Plugins.prototype.getPluginsFor = function(pluginType) { }); }; -Plugins.prototype.getPluginsProperty = function(pluginType, property) { +Plugins.prototype.getPluginsProperty = function(pluginType, property, sub_property) { let matchingPlugins = this.plugins.filter(function(plugin) { return plugin.has(pluginType); }); let matchingProperties = matchingPlugins.map((plugin) => { + if (sub_property) { + return plugin[property][sub_property]; + } return plugin[property]; }); //return flattened list if (matchingProperties.length === 0) return []; - return matchingProperties.reduce((a,b) => { return a.concat(b); }); + return matchingProperties.reduce((a,b) => { return a.concat(b); }) || []; +}; + +//Plugins.prototype.runActionsForEvent = function(eventName, args, cb) { +Plugins.prototype.runActionsForEvent = function(eventName, cb) { + let actionPlugins = this.getPluginsProperty('eventActions', 'eventActions', eventName); + + async.eachLimit(actionPlugins, 1, function(plugin, nextEach) { + //plugin.call(plugin, ...args, nextEach); + plugin.call(plugin, nextEach); + }, cb); }; module.exports = Plugins; diff --git a/lib/modules/specialconfigs/index.js b/lib/modules/specialconfigs/index.js index 94b8328e..0ffe15e1 100644 --- a/lib/modules/specialconfigs/index.js +++ b/lib/modules/specialconfigs/index.js @@ -48,7 +48,8 @@ class SpecialConfigs { registerAfterDeployAction() { const self = this; - this.embark.registerAfterAllContractsDeploy((cb) => { + //this.embark.registerAfterAllContractsDeploy((cb) => { + this.embark.registerActionForEvent("contracts:deploy:afterAll", (cb) => { let afterDeployCmds = self.contractsConfig.afterDeploy || []; async.mapLimit(afterDeployCmds, 1, (cmd, nextMapCb) => {