Hook.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.trim()
  60. };
  61. } else if (typeof options !== "object" || options === null) {
  62. throw new Error("Invalid tap options");
  63. }
  64. if (typeof options.name !== "string" || options.name === "") {
  65. throw new Error("Missing name for tap");
  66. }
  67. if (typeof options.context !== "undefined") {
  68. deprecateContext();
  69. }
  70. options = Object.assign({ type, fn }, options);
  71. options = this._runRegisterInterceptors(options);
  72. this._insert(options);
  73. }
  74. tap(options, fn) {
  75. this._tap("sync", options, fn);
  76. }
  77. tapAsync(options, fn) {
  78. this._tap("async", options, fn);
  79. }
  80. tapPromise(options, fn) {
  81. this._tap("promise", options, fn);
  82. }
  83. _runRegisterInterceptors(options) {
  84. for (const interceptor of this.interceptors) {
  85. if (interceptor.register) {
  86. const newOptions = interceptor.register(options);
  87. if (newOptions !== undefined) {
  88. options = newOptions;
  89. }
  90. }
  91. }
  92. return options;
  93. }
  94. withOptions(options) {
  95. const mergeOptions = (opt) =>
  96. Object.assign({}, options, typeof opt === "string" ? { name: opt } : opt);
  97. return {
  98. name: this.name,
  99. tap: (opt, fn) => this.tap(mergeOptions(opt), fn),
  100. tapAsync: (opt, fn) => this.tapAsync(mergeOptions(opt), fn),
  101. tapPromise: (opt, fn) => this.tapPromise(mergeOptions(opt), fn),
  102. intercept: (interceptor) => this.intercept(interceptor),
  103. isUsed: () => this.isUsed(),
  104. withOptions: (opt) => this.withOptions(mergeOptions(opt))
  105. };
  106. }
  107. isUsed() {
  108. return this.taps.length > 0 || this.interceptors.length > 0;
  109. }
  110. intercept(interceptor) {
  111. this._resetCompilation();
  112. this.interceptors.push(Object.assign({}, interceptor));
  113. if (interceptor.register) {
  114. for (let i = 0; i < this.taps.length; i++) {
  115. this.taps[i] = interceptor.register(this.taps[i]);
  116. }
  117. }
  118. }
  119. _resetCompilation() {
  120. this.call = this._call;
  121. this.callAsync = this._callAsync;
  122. this.promise = this._promise;
  123. }
  124. _insert(item) {
  125. this._resetCompilation();
  126. let before;
  127. if (typeof item.before === "string") {
  128. before = new Set([item.before]);
  129. } else if (Array.isArray(item.before)) {
  130. before = new Set(item.before);
  131. }
  132. let stage = 0;
  133. if (typeof item.stage === "number") {
  134. stage = item.stage;
  135. }
  136. let i = this.taps.length;
  137. while (i > 0) {
  138. i--;
  139. const tap = this.taps[i];
  140. this.taps[i + 1] = tap;
  141. const xStage = tap.stage || 0;
  142. if (before) {
  143. if (before.has(tap.name)) {
  144. before.delete(tap.name);
  145. continue;
  146. }
  147. if (before.size > 0) {
  148. continue;
  149. }
  150. }
  151. if (xStage > stage) {
  152. continue;
  153. }
  154. i++;
  155. break;
  156. }
  157. this.taps[i] = item;
  158. }
  159. }
  160. Object.setPrototypeOf(Hook.prototype, null);
  161. module.exports = Hook;