Skip to content

Commit

Permalink
DRY up tappable code for applyPlugins.
Browse files Browse the repository at this point in the history
migrate the logic to live in the root function and use a switch
statement to decide when to use call / apply.

Keep the existing functions in place and call though to maintain
backwards compat.

original ref bug:
  • Loading branch information
samccone committed Aug 15, 2017
1 parent 004df56 commit fc0e184
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/Tapable.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,33 +53,46 @@ Tapable.mixin = function mixinTapable(pt) {
copyProperties(Tapable.prototype, pt);
};

Tapable.prototype.applyPlugins = function applyPlugins(name) {
Tapable.prototype.applyPlugins = function applyPlugins(name, param1, param2) {
if(!this._plugins[name]) return;
var args = Array.prototype.slice.call(arguments, 1);

var plugins = this._plugins[name];
for(var i = 0; i < plugins.length; i++)
plugins[i].apply(this, args);
};

switch (arguments.length) {
case 1:
for(var i = 0; i < plugins.length; i++) {
plugins[i].call(this);
}
break;
case 2:
for(var i = 0; i < plugins.length; i++) {
plugins[i].call(this, param1);
}
break;
case 3:
for(var i = 0; i < plugins.length; i++) {
plugins[i].call(this, param1, param2);
}
break;
default:
var args = array.prototype.slice.call(arguments, 1);
for(var i = 0; i < plugins.length; i++) {
plugins[i].apply(this, args);
}
break;
}
}

Tapable.prototype.applyPlugins0 = function applyPlugins0(name) {
var plugins = this._plugins[name];
if(!plugins) return;
for(var i = 0; i < plugins.length; i++)
plugins[i].call(this);
this.applyPlugins(name)
};

Tapable.prototype.applyPlugins1 = function applyPlugins1(name, param) {
var plugins = this._plugins[name];
if(!plugins) return;
for(var i = 0; i < plugins.length; i++)
plugins[i].call(this, param);
this.applyPlugins(name, param)
};

Tapable.prototype.applyPlugins2 = function applyPlugins2(name, param1, param2) {
var plugins = this._plugins[name];
if(!plugins) return;
for(var i = 0; i < plugins.length; i++)
plugins[i].call(this, param1, param2);
this.applyPlugins(name, param1, param2)
};

Tapable.prototype.applyPluginsWaterfall = function applyPluginsWaterfall(name, init) {
Expand Down

0 comments on commit fc0e184

Please sign in to comment.