MultiHook.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. class MultiHook {
  7. constructor(hooks, name = undefined) {
  8. this.hooks = hooks;
  9. this.name = name;
  10. }
  11. tap(options, fn) {
  12. const { hooks } = this;
  13. for (let i = 0; i < hooks.length; i++) {
  14. hooks[i].tap(options, fn);
  15. }
  16. }
  17. tapAsync(options, fn) {
  18. const { hooks } = this;
  19. for (let i = 0; i < hooks.length; i++) {
  20. hooks[i].tapAsync(options, fn);
  21. }
  22. }
  23. tapPromise(options, fn) {
  24. const { hooks } = this;
  25. for (let i = 0; i < hooks.length; i++) {
  26. hooks[i].tapPromise(options, fn);
  27. }
  28. }
  29. isUsed() {
  30. const { hooks } = this;
  31. for (let i = 0; i < hooks.length; i++) {
  32. if (hooks[i].isUsed()) return true;
  33. }
  34. return false;
  35. }
  36. intercept(interceptor) {
  37. const { hooks } = this;
  38. for (let i = 0; i < hooks.length; i++) {
  39. hooks[i].intercept(interceptor);
  40. }
  41. }
  42. withOptions(options) {
  43. return new MultiHook(
  44. this.hooks.map((hook) => hook.withOptions(options)),
  45. this.name
  46. );
  47. }
  48. }
  49. module.exports = MultiHook;