Hook.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. function CALL_DELEGATE(...args) {
  12. this.call = this._createCall("sync");
  13. return this.call(...args);
  14. }
  15. function CALL_ASYNC_DELEGATE(...args) {
  16. this.callAsync = this._createCall("async");
  17. return this.callAsync(...args);
  18. }
  19. function PROMISE_DELEGATE(...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. // eslint-disable-next-line no-self-assign
  37. this.compile = this.compile;
  38. // eslint-disable-next-line no-self-assign
  39. this.tap = this.tap;
  40. // eslint-disable-next-line no-self-assign
  41. this.tapAsync = this.tapAsync;
  42. // eslint-disable-next-line no-self-assign
  43. this.tapPromise = this.tapPromise;
  44. }
  45. compile(_options) {
  46. throw new Error("Abstract: should be overridden");
  47. }
  48. _createCall(type) {
  49. return this.compile({
  50. taps: this.taps,
  51. interceptors: this.interceptors,
  52. args: this._args,
  53. type
  54. });
  55. }
  56. _tap(type, options, fn) {
  57. if (typeof options === "string") {
  58. options = {
  59. name: options
  60. };
  61. } else if (typeof options !== "object" || options === null) {
  62. throw new Error("Invalid tap options");
  63. }
  64. if (typeof options.name === "string") {
  65. options.name = options.name.trim();
  66. }
  67. if (typeof options.name !== "string" || options.name === "") {
  68. throw new Error("Missing name for tap");
  69. }
  70. if (typeof options.context !== "undefined") {
  71. deprecateContext();
  72. }
  73. options = Object.assign({ type, fn }, options);
  74. options = this._runRegisterInterceptors(options);
  75. this._insert(options);
  76. }
  77. tap(options, fn) {
  78. this._tap("sync", options, fn);
  79. }
  80. tapAsync(options, fn) {
  81. this._tap("async", options, fn);
  82. }
  83. tapPromise(options, fn) {
  84. this._tap("promise", options, fn);
  85. }
  86. _runRegisterInterceptors(options) {
  87. for (const interceptor of this.interceptors) {
  88. if (interceptor.register) {
  89. const newOptions = interceptor.register(options);
  90. if (newOptions !== undefined) {
  91. options = newOptions;
  92. }
  93. }
  94. }
  95. return options;
  96. }
  97. withOptions(options) {
  98. const mergeOptions = (opt) =>
  99. Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt);
  100. return {
  101. name: this.name,
  102. tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
  103. tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
  104. tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
  105. intercept: (interceptor) => this.intercept(interceptor),
  106. isUsed: () => this.isUsed(),
  107. withOptions: (opt) => this.withOptions(mergeOptions(opt))
  108. };
  109. }
  110. isUsed() {
  111. return this.taps.length > 0 || this.interceptors.length > 0;
  112. }
  113. intercept(interceptor) {
  114. this._resetCompilation();
  115. this.interceptors.push(Object.assign({}, interceptor));
  116. if (interceptor.register) {
  117. for (let i = 0; i < this.taps.length; i++) {
  118. this.taps[i] = interceptor.register(this.taps[i]);
  119. }
  120. }
  121. }
  122. _resetCompilation() {
  123. this.call = this._call;
  124. this.callAsync = this._callAsync;
  125. this.promise = this._promise;
  126. }
  127. _insert(item) {
  128. this._resetCompilation();
  129. let before;
  130. if (typeof item.before === "string") {
  131. before = new Set([item.before]);
  132. } else if (Array.isArray(item.before)) {
  133. before = new Set(item.before);
  134. }
  135. let stage = 0;
  136. if (typeof item.stage === "number") {
  137. stage = item.stage;
  138. }
  139. let i = this.taps.length;
  140. while (i > 0) {
  141. i--;
  142. const tap = this.taps[i];
  143. this.taps[i + 1] = tap;
  144. const xStage = tap.stage || 0;
  145. if (before) {
  146. if (before.has(tap.name)) {
  147. before.delete(tap.name);
  148. continue;
  149. }
  150. if (before.size > 0) {
  151. continue;
  152. }
  153. }
  154. if (xStage > stage) {
  155. continue;
  156. }
  157. i++;
  158. break;
  159. }
  160. this.taps[i] = item;
  161. }
  162. }
  163. Object.setPrototypeOf(Hook.prototype, null);
  164. module.exports = Hook;