UI for Zipcoin Blue

Tapable.js 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. // polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
  6. // using the polyfill specifically to avoid the call to `Object.defineProperty` for performance reasons
  7. function fastFilter(fun/*, thisArg*/) {
  8. 'use strict';
  9. if (this === void 0 || this === null) {
  10. throw new TypeError();
  11. }
  12. var t = Object(this);
  13. var len = t.length >>> 0;
  14. if (typeof fun !== 'function') {
  15. throw new TypeError();
  16. }
  17. var res = [];
  18. var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
  19. for (var i = 0; i < len; i++) {
  20. if (i in t) {
  21. var val = t[i];
  22. // NOTE: Technically this should Object.defineProperty at
  23. // the next index, as push can be affected by
  24. // properties on Object.prototype and Array.prototype.
  25. // But that method's new, and collisions should be
  26. // rare, so use the more-compatible alternative.
  27. if (fun.call(thisArg, val, i, t)) {
  28. res.push(val);
  29. }
  30. }
  31. }
  32. return res;
  33. }
  34. function Tapable() {
  35. this._plugins = {};
  36. }
  37. module.exports = Tapable;
  38. function copyProperties(from, to) {
  39. for(var key in from)
  40. to[key] = from[key];
  41. return to;
  42. }
  43. Tapable.mixin = function mixinTapable(pt) {
  44. copyProperties(Tapable.prototype, pt);
  45. };
  46. Tapable.prototype.applyPlugins = function applyPlugins(name) {
  47. if(!this._plugins[name]) return;
  48. var args = Array.prototype.slice.call(arguments, 1);
  49. var plugins = this._plugins[name];
  50. for(var i = 0; i < plugins.length; i++)
  51. plugins[i].apply(this, args);
  52. };
  53. Tapable.prototype.applyPlugins0 = function applyPlugins0(name) {
  54. var plugins = this._plugins[name];
  55. if(!plugins) return;
  56. for(var i = 0; i < plugins.length; i++)
  57. plugins[i].call(this);
  58. };
  59. Tapable.prototype.applyPlugins1 = function applyPlugins1(name, param) {
  60. var plugins = this._plugins[name];
  61. if(!plugins) return;
  62. for(var i = 0; i < plugins.length; i++)
  63. plugins[i].call(this, param);
  64. };
  65. Tapable.prototype.applyPlugins2 = function applyPlugins2(name, param1, param2) {
  66. var plugins = this._plugins[name];
  67. if(!plugins) return;
  68. for(var i = 0; i < plugins.length; i++)
  69. plugins[i].call(this, param1, param2);
  70. };
  71. Tapable.prototype.applyPluginsWaterfall = function applyPluginsWaterfall(name, init) {
  72. if(!this._plugins[name]) return init;
  73. var args = Array.prototype.slice.call(arguments, 1);
  74. var plugins = this._plugins[name];
  75. var current = init;
  76. for(var i = 0; i < plugins.length; i++) {
  77. args[0] = current;
  78. current = plugins[i].apply(this, args);
  79. }
  80. return current;
  81. };
  82. Tapable.prototype.applyPluginsWaterfall0 = function applyPluginsWaterfall0(name, init) {
  83. var plugins = this._plugins[name];
  84. if(!plugins) return init;
  85. var current = init;
  86. for(var i = 0; i < plugins.length; i++)
  87. current = plugins[i].call(this, current);
  88. return current;
  89. };
  90. Tapable.prototype.applyPluginsWaterfall1 = function applyPluginsWaterfall1(name, init, param) {
  91. var plugins = this._plugins[name];
  92. if(!plugins) return init;
  93. var current = init;
  94. for(var i = 0; i < plugins.length; i++)
  95. current = plugins[i].call(this, current, param);
  96. return current;
  97. };
  98. Tapable.prototype.applyPluginsWaterfall2 = function applyPluginsWaterfall2(name, init, param1, param2) {
  99. var plugins = this._plugins[name];
  100. if(!plugins) return init;
  101. var current = init;
  102. for(var i = 0; i < plugins.length; i++)
  103. current = plugins[i].call(this, current, param1, param2);
  104. return current;
  105. };
  106. Tapable.prototype.applyPluginsBailResult = function applyPluginsBailResult(name) {
  107. if(!this._plugins[name]) return;
  108. var args = Array.prototype.slice.call(arguments, 1);
  109. var plugins = this._plugins[name];
  110. for(var i = 0; i < plugins.length; i++) {
  111. var result = plugins[i].apply(this, args);
  112. if(typeof result !== "undefined") {
  113. return result;
  114. }
  115. }
  116. };
  117. Tapable.prototype.applyPluginsBailResult1 = function applyPluginsBailResult1(name, param) {
  118. if(!this._plugins[name]) return;
  119. var plugins = this._plugins[name];
  120. for(var i = 0; i < plugins.length; i++) {
  121. var result = plugins[i].call(this, param);
  122. if(typeof result !== "undefined") {
  123. return result;
  124. }
  125. }
  126. };
  127. Tapable.prototype.applyPluginsBailResult2 = function applyPluginsBailResult2(name, param1, param2) {
  128. if(!this._plugins[name]) return;
  129. var plugins = this._plugins[name];
  130. for(var i = 0; i < plugins.length; i++) {
  131. var result = plugins[i].call(this, param1, param2);
  132. if(typeof result !== "undefined") {
  133. return result;
  134. }
  135. }
  136. };
  137. Tapable.prototype.applyPluginsBailResult3 = function applyPluginsBailResult3(name, param1, param2, param3) {
  138. if(!this._plugins[name]) return;
  139. var plugins = this._plugins[name];
  140. for(var i = 0; i < plugins.length; i++) {
  141. var result = plugins[i].call(this, param1, param2, param3);
  142. if(typeof result !== "undefined") {
  143. return result;
  144. }
  145. }
  146. };
  147. Tapable.prototype.applyPluginsBailResult4 = function applyPluginsBailResult4(name, param1, param2, param3, param4) {
  148. if(!this._plugins[name]) return;
  149. var plugins = this._plugins[name];
  150. for(var i = 0; i < plugins.length; i++) {
  151. var result = plugins[i].call(this, param1, param2, param3, param4);
  152. if(typeof result !== "undefined") {
  153. return result;
  154. }
  155. }
  156. };
  157. Tapable.prototype.applyPluginsBailResult5 = function applyPluginsBailResult5(name, param1, param2, param3, param4, param5) {
  158. if(!this._plugins[name]) return;
  159. var plugins = this._plugins[name];
  160. for(var i = 0; i < plugins.length; i++) {
  161. var result = plugins[i].call(this, param1, param2, param3, param4, param5);
  162. if(typeof result !== "undefined") {
  163. return result;
  164. }
  165. }
  166. };
  167. Tapable.prototype.applyPluginsAsyncSeries = Tapable.prototype.applyPluginsAsync = function applyPluginsAsyncSeries(name) {
  168. var args = Array.prototype.slice.call(arguments, 1);
  169. var callback = args.pop();
  170. var plugins = this._plugins[name];
  171. if(!plugins || plugins.length === 0) return callback();
  172. var i = 0;
  173. var _this = this;
  174. args.push(copyProperties(callback, function next(err) {
  175. if(err) return callback(err);
  176. i++;
  177. if(i >= plugins.length) {
  178. return callback();
  179. }
  180. plugins[i].apply(_this, args);
  181. }));
  182. plugins[0].apply(this, args);
  183. };
  184. Tapable.prototype.applyPluginsAsyncSeries1 = function applyPluginsAsyncSeries1(name, param, callback) {
  185. var plugins = this._plugins[name];
  186. if(!plugins || plugins.length === 0) return callback();
  187. var i = 0;
  188. var _this = this;
  189. var innerCallback = copyProperties(callback, function next(err) {
  190. if(err) return callback(err);
  191. i++;
  192. if(i >= plugins.length) {
  193. return callback();
  194. }
  195. plugins[i].call(_this, param, innerCallback);
  196. });
  197. plugins[0].call(this, param, innerCallback);
  198. };
  199. Tapable.prototype.applyPluginsAsyncSeriesBailResult = function applyPluginsAsyncSeriesBailResult(name) {
  200. var args = Array.prototype.slice.call(arguments, 1);
  201. var callback = args.pop();
  202. if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
  203. var plugins = this._plugins[name];
  204. var i = 0;
  205. var _this = this;
  206. args.push(copyProperties(callback, function next() {
  207. if(arguments.length > 0) return callback.apply(null, arguments);
  208. i++;
  209. if(i >= plugins.length) {
  210. return callback();
  211. }
  212. plugins[i].apply(_this, args);
  213. }));
  214. plugins[0].apply(this, args);
  215. };
  216. Tapable.prototype.applyPluginsAsyncSeriesBailResult1 = function applyPluginsAsyncSeriesBailResult1(name, param, callback) {
  217. var plugins = this._plugins[name];
  218. if(!plugins || plugins.length === 0) return callback();
  219. var i = 0;
  220. var _this = this;
  221. var innerCallback = copyProperties(callback, function next(err, result) {
  222. if(arguments.length > 0) return callback(err, result);
  223. i++;
  224. if(i >= plugins.length) {
  225. return callback();
  226. }
  227. plugins[i].call(_this, param, innerCallback);
  228. });
  229. plugins[0].call(this, param, innerCallback);
  230. };
  231. Tapable.prototype.applyPluginsAsyncWaterfall = function applyPluginsAsyncWaterfall(name, init, callback) {
  232. if(!this._plugins[name] || this._plugins[name].length === 0) return callback(null, init);
  233. var plugins = this._plugins[name];
  234. var i = 0;
  235. var _this = this;
  236. var next = copyProperties(callback, function(err, value) {
  237. if(err) return callback(err);
  238. i++;
  239. if(i >= plugins.length) {
  240. return callback(null, value);
  241. }
  242. plugins[i].call(_this, value, next);
  243. });
  244. plugins[0].call(this, init, next);
  245. };
  246. Tapable.prototype.applyPluginsParallel = function applyPluginsParallel(name) {
  247. var args = Array.prototype.slice.call(arguments, 1);
  248. var callback = args.pop();
  249. if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
  250. var plugins = this._plugins[name];
  251. var remaining = plugins.length;
  252. args.push(copyProperties(callback, function(err) {
  253. if(remaining < 0) return; // ignore
  254. if(err) {
  255. remaining = -1;
  256. return callback(err);
  257. }
  258. remaining--;
  259. if(remaining === 0) {
  260. return callback();
  261. }
  262. }));
  263. for(var i = 0; i < plugins.length; i++) {
  264. plugins[i].apply(this, args);
  265. if(remaining < 0) return;
  266. }
  267. };
  268. Tapable.prototype.applyPluginsParallelBailResult = function applyPluginsParallelBailResult(name) {
  269. var args = Array.prototype.slice.call(arguments, 1);
  270. var callback = args[args.length - 1];
  271. if(!this._plugins[name] || this._plugins[name].length === 0) return callback();
  272. var plugins = this._plugins[name];
  273. var currentPos = plugins.length;
  274. var currentResult;
  275. var done = [];
  276. for(var i = 0; i < plugins.length; i++) {
  277. args[args.length - 1] = (function(i) {
  278. return copyProperties(callback, function() {
  279. if(i >= currentPos) return; // ignore
  280. done.push(i);
  281. if(arguments.length > 0) {
  282. currentPos = i + 1;
  283. done = fastFilter.call(done, function(item) {
  284. return item <= i;
  285. });
  286. currentResult = Array.prototype.slice.call(arguments);
  287. }
  288. if(done.length === currentPos) {
  289. callback.apply(null, currentResult);
  290. currentPos = 0;
  291. }
  292. });
  293. }(i));
  294. plugins[i].apply(this, args);
  295. }
  296. };
  297. Tapable.prototype.applyPluginsParallelBailResult1 = function applyPluginsParallelBailResult1(name, param, callback) {
  298. var plugins = this._plugins[name];
  299. if(!plugins || plugins.length === 0) return callback();
  300. var currentPos = plugins.length;
  301. var currentResult;
  302. var done = [];
  303. for(var i = 0; i < plugins.length; i++) {
  304. var innerCallback = (function(i) {
  305. return copyProperties(callback, function() {
  306. if(i >= currentPos) return; // ignore
  307. done.push(i);
  308. if(arguments.length > 0) {
  309. currentPos = i + 1;
  310. done = fastFilter.call(done, function(item) {
  311. return item <= i;
  312. });
  313. currentResult = Array.prototype.slice.call(arguments);
  314. }
  315. if(done.length === currentPos) {
  316. callback.apply(null, currentResult);
  317. currentPos = 0;
  318. }
  319. });
  320. }(i));
  321. plugins[i].call(this, param, innerCallback);
  322. }
  323. };
  324. Tapable.prototype.hasPlugins = function hasPlugins(name) {
  325. var plugins = this._plugins[name];
  326. return plugins && plugins.length > 0;
  327. };
  328. Tapable.prototype.plugin = function plugin(name, fn) {
  329. if(Array.isArray(name)) {
  330. name.forEach(function(name) {
  331. this.plugin(name, fn);
  332. }, this);
  333. return;
  334. }
  335. if(!this._plugins[name]) this._plugins[name] = [fn];
  336. else this._plugins[name].push(fn);
  337. };
  338. Tapable.prototype.apply = function apply() {
  339. for(var i = 0; i < arguments.length; i++) {
  340. arguments[i].apply(this);
  341. }
  342. };