Hook.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const util = require("util");
  7. const deprecateContext = util.deprecate(
  8. () => {},
  9. "Hook.context is deprecated and will be removed"
  10. );
  11. const CALL_DELEGATE = function (...args) {
  12. this.call = this._createCall("sync");
  13. return this.call(...args);
  14. };
  15. const CALL_ASYNC_DELEGATE = function (...args) {
  16. this.callAsync = this._createCall("async");
  17. return this.callAsync(...args);
  18. };
  19. const PROMISE_DELEGATE = function (...args) {
  20. this.promise = this._createCall("promise");
  21. return this.promise(...args);
  22. };
  23. class Hook {
  24. constructor(args = [], name = undefined) {
  25. this._args = args;
  26. this.name = name;
  27. this.taps = [];
  28. this.interceptors = [];
  29. this._call = CALL_DELEGATE;
  30. this.call = CALL_DELEGATE;
  31. this._callAsync = CALL_ASYNC_DELEGATE;
  32. this.callAsync = CALL_ASYNC_DELEGATE;
  33. this._promise = PROMISE_DELEGATE;
  34. this.promise = PROMISE_DELEGATE;
  35. this._x = undefined;
  36. this.compile = this.compile;
  37. this.tap = this.tap;
  38. this.tapAsync = this.tapAsync;
  39. this.tapPromise = this.tapPromise;
  40. }
  41. compile(options) {
  42. throw new Error("Abstract: should be overridden");
  43. }
  44. _createCall(type) {
  45. return this.compile({
  46. taps: this.taps,
  47. interceptors: this.interceptors,
  48. args: this._args,
  49. type: type
  50. });
  51. }
  52. _tap(type, options, fn) {
  53. if (typeof options === "string") {
  54. options = {
  55. name: options.trim()
  56. };
  57. } else if (typeof options !== "object" || options === null) {
  58. throw new Error("Invalid tap options");
  59. }
  60. if (typeof options.name !== "string" || options.name === "") {
  61. throw new Error("Missing name for tap");
  62. }
  63. if (typeof options.context !== "undefined") {
  64. deprecateContext();
  65. }
  66. options = Object.assign({ type, fn }, options);
  67. options = this._runRegisterInterceptors(options);
  68. this._insert(options);
  69. }
  70. tap(options, fn) {
  71. this._tap("sync", options, fn);
  72. }
  73. tapAsync(options, fn) {
  74. this._tap("async", options, fn);
  75. }
  76. tapPromise(options, fn) {
  77. this._tap("promise", options, fn);
  78. }
  79. _runRegisterInterceptors(options) {
  80. for (const interceptor of this.interceptors) {
  81. if (interceptor.register) {
  82. const newOptions = interceptor.register(options);
  83. if (newOptions !== undefined) {
  84. options = newOptions;
  85. }
  86. }
  87. }
  88. return options;
  89. }
  90. withOptions(options) {
  91. const mergeOptions = (opt) =>
  92. Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt);
  93. return {
  94. name: this.name,
  95. tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
  96. tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
  97. tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
  98. intercept: (interceptor) => this.intercept(interceptor),
  99. isUsed: () => this.isUsed(),
  100. withOptions: (opt) => this.withOptions(mergeOptions(opt))
  101. };
  102. }
  103. isUsed() {
  104. return this.taps.length > 0 || this.interceptors.length > 0;
  105. }
  106. intercept(interceptor) {
  107. this._resetCompilation();
  108. this.interceptors.push(Object.assign({}, interceptor));
  109. if (interceptor.register) {
  110. for (let i = 0; i < this.taps.length; i++) {
  111. this.taps[i] = interceptor.register(this.taps[i]);
  112. }
  113. }
  114. }
  115. _resetCompilation() {
  116. this.call = this._call;
  117. this.callAsync = this._callAsync;
  118. this.promise = this._promise;
  119. }
  120. _insert(item) {
  121. this._resetCompilation();
  122. let before;
  123. if (typeof item.before === "string") {
  124. before = new Set([item.before]);
  125. } else if (Array.isArray(item.before)) {
  126. before = new Set(item.before);
  127. }
  128. let stage = 0;
  129. if (typeof item.stage === "number") {
  130. stage = item.stage;
  131. }
  132. let i = this.taps.length;
  133. while (i > 0) {
  134. i--;
  135. const x = this.taps[i];
  136. this.taps[i + 1] = x;
  137. const xStage = x.stage || 0;
  138. if (before) {
  139. if (before.has(x.name)) {
  140. before.delete(x.name);
  141. continue;
  142. }
  143. if (before.size > 0) {
  144. continue;
  145. }
  146. }
  147. if (xStage > stage) {
  148. continue;
  149. }
  150. i++;
  151. break;
  152. }
  153. this.taps[i] = item;
  154. }
  155. }
  156. Object.setPrototypeOf(Hook.prototype, null);
  157. module.exports = Hook;