index.cjs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. 'use strict';
  2. function flatHooks(configHooks, hooks = {}, parentName) {
  3. for (const key in configHooks) {
  4. const subHook = configHooks[key];
  5. const name = parentName ? `${parentName}:${key}` : key;
  6. if (typeof subHook === "object" && subHook !== null) {
  7. flatHooks(subHook, hooks, name);
  8. } else if (typeof subHook === "function") {
  9. hooks[name] = subHook;
  10. }
  11. }
  12. return hooks;
  13. }
  14. function mergeHooks(...hooks) {
  15. const finalHooks = {};
  16. for (const hook of hooks) {
  17. const flatenHook = flatHooks(hook);
  18. for (const key in flatenHook) {
  19. if (finalHooks[key]) {
  20. finalHooks[key].push(flatenHook[key]);
  21. } else {
  22. finalHooks[key] = [flatenHook[key]];
  23. }
  24. }
  25. }
  26. for (const key in finalHooks) {
  27. if (finalHooks[key].length > 1) {
  28. const array = finalHooks[key];
  29. finalHooks[key] = (...arguments_) => serial(array, (function_) => function_(...arguments_));
  30. } else {
  31. finalHooks[key] = finalHooks[key][0];
  32. }
  33. }
  34. return finalHooks;
  35. }
  36. function serial(tasks, function_) {
  37. return tasks.reduce(
  38. (promise, task) => promise.then(() => function_(task)),
  39. Promise.resolve()
  40. );
  41. }
  42. const defaultTask = { run: (function_) => function_() };
  43. const _createTask = () => defaultTask;
  44. const createTask = typeof console.createTask !== "undefined" ? console.createTask : _createTask;
  45. function serialTaskCaller(hooks, args) {
  46. const name = args.shift();
  47. const task = createTask(name);
  48. return hooks.reduce(
  49. (promise, hookFunction) => promise.then(() => task.run(() => hookFunction(...args))),
  50. Promise.resolve()
  51. );
  52. }
  53. function parallelTaskCaller(hooks, args) {
  54. const name = args.shift();
  55. const task = createTask(name);
  56. return Promise.all(hooks.map((hook) => task.run(() => hook(...args))));
  57. }
  58. function serialCaller(hooks, arguments_) {
  59. return hooks.reduce(
  60. (promise, hookFunction) => promise.then(() => hookFunction(...arguments_ || [])),
  61. Promise.resolve()
  62. );
  63. }
  64. function parallelCaller(hooks, args) {
  65. return Promise.all(hooks.map((hook) => hook(...args || [])));
  66. }
  67. function callEachWith(callbacks, arg0) {
  68. for (const callback of [...callbacks]) {
  69. callback(arg0);
  70. }
  71. }
  72. class Hookable {
  73. constructor() {
  74. this._hooks = {};
  75. this._before = void 0;
  76. this._after = void 0;
  77. this._deprecatedMessages = void 0;
  78. this._deprecatedHooks = {};
  79. this.hook = this.hook.bind(this);
  80. this.callHook = this.callHook.bind(this);
  81. this.callHookWith = this.callHookWith.bind(this);
  82. }
  83. hook(name, function_, options = {}) {
  84. if (!name || typeof function_ !== "function") {
  85. return () => {
  86. };
  87. }
  88. const originalName = name;
  89. let dep;
  90. while (this._deprecatedHooks[name]) {
  91. dep = this._deprecatedHooks[name];
  92. name = dep.to;
  93. }
  94. if (dep && !options.allowDeprecated) {
  95. let message = dep.message;
  96. if (!message) {
  97. message = `${originalName} hook has been deprecated` + (dep.to ? `, please use ${dep.to}` : "");
  98. }
  99. if (!this._deprecatedMessages) {
  100. this._deprecatedMessages = /* @__PURE__ */ new Set();
  101. }
  102. if (!this._deprecatedMessages.has(message)) {
  103. console.warn(message);
  104. this._deprecatedMessages.add(message);
  105. }
  106. }
  107. if (!function_.name) {
  108. try {
  109. Object.defineProperty(function_, "name", {
  110. get: () => "_" + name.replace(/\W+/g, "_") + "_hook_cb",
  111. configurable: true
  112. });
  113. } catch {
  114. }
  115. }
  116. this._hooks[name] = this._hooks[name] || [];
  117. this._hooks[name].push(function_);
  118. return () => {
  119. if (function_) {
  120. this.removeHook(name, function_);
  121. function_ = void 0;
  122. }
  123. };
  124. }
  125. hookOnce(name, function_) {
  126. let _unreg;
  127. let _function = (...arguments_) => {
  128. if (typeof _unreg === "function") {
  129. _unreg();
  130. }
  131. _unreg = void 0;
  132. _function = void 0;
  133. return function_(...arguments_);
  134. };
  135. _unreg = this.hook(name, _function);
  136. return _unreg;
  137. }
  138. removeHook(name, function_) {
  139. if (this._hooks[name]) {
  140. const index = this._hooks[name].indexOf(function_);
  141. if (index !== -1) {
  142. this._hooks[name].splice(index, 1);
  143. }
  144. if (this._hooks[name].length === 0) {
  145. delete this._hooks[name];
  146. }
  147. }
  148. }
  149. deprecateHook(name, deprecated) {
  150. this._deprecatedHooks[name] = typeof deprecated === "string" ? { to: deprecated } : deprecated;
  151. const _hooks = this._hooks[name] || [];
  152. delete this._hooks[name];
  153. for (const hook of _hooks) {
  154. this.hook(name, hook);
  155. }
  156. }
  157. deprecateHooks(deprecatedHooks) {
  158. Object.assign(this._deprecatedHooks, deprecatedHooks);
  159. for (const name in deprecatedHooks) {
  160. this.deprecateHook(name, deprecatedHooks[name]);
  161. }
  162. }
  163. addHooks(configHooks) {
  164. const hooks = flatHooks(configHooks);
  165. const removeFns = Object.keys(hooks).map(
  166. (key) => this.hook(key, hooks[key])
  167. );
  168. return () => {
  169. for (const unreg of removeFns.splice(0, removeFns.length)) {
  170. unreg();
  171. }
  172. };
  173. }
  174. removeHooks(configHooks) {
  175. const hooks = flatHooks(configHooks);
  176. for (const key in hooks) {
  177. this.removeHook(key, hooks[key]);
  178. }
  179. }
  180. removeAllHooks() {
  181. for (const key in this._hooks) {
  182. delete this._hooks[key];
  183. }
  184. }
  185. callHook(name, ...arguments_) {
  186. arguments_.unshift(name);
  187. return this.callHookWith(serialTaskCaller, name, ...arguments_);
  188. }
  189. callHookParallel(name, ...arguments_) {
  190. arguments_.unshift(name);
  191. return this.callHookWith(parallelTaskCaller, name, ...arguments_);
  192. }
  193. callHookWith(caller, name, ...arguments_) {
  194. const event = this._before || this._after ? { name, args: arguments_, context: {} } : void 0;
  195. if (this._before) {
  196. callEachWith(this._before, event);
  197. }
  198. const result = caller(
  199. name in this._hooks ? [...this._hooks[name]] : [],
  200. arguments_
  201. );
  202. if (result instanceof Promise) {
  203. return result.finally(() => {
  204. if (this._after && event) {
  205. callEachWith(this._after, event);
  206. }
  207. });
  208. }
  209. if (this._after && event) {
  210. callEachWith(this._after, event);
  211. }
  212. return result;
  213. }
  214. beforeEach(function_) {
  215. this._before = this._before || [];
  216. this._before.push(function_);
  217. return () => {
  218. if (this._before !== void 0) {
  219. const index = this._before.indexOf(function_);
  220. if (index !== -1) {
  221. this._before.splice(index, 1);
  222. }
  223. }
  224. };
  225. }
  226. afterEach(function_) {
  227. this._after = this._after || [];
  228. this._after.push(function_);
  229. return () => {
  230. if (this._after !== void 0) {
  231. const index = this._after.indexOf(function_);
  232. if (index !== -1) {
  233. this._after.splice(index, 1);
  234. }
  235. }
  236. };
  237. }
  238. }
  239. function createHooks() {
  240. return new Hookable();
  241. }
  242. const isBrowser = typeof window !== "undefined";
  243. function createDebugger(hooks, _options = {}) {
  244. const options = {
  245. inspect: isBrowser,
  246. group: isBrowser,
  247. filter: () => true,
  248. ..._options
  249. };
  250. const _filter = options.filter;
  251. const filter = typeof _filter === "string" ? (name) => name.startsWith(_filter) : _filter;
  252. const _tag = options.tag ? `[${options.tag}] ` : "";
  253. const logPrefix = (event) => _tag + event.name + "".padEnd(event._id, "\0");
  254. const _idCtr = {};
  255. const unsubscribeBefore = hooks.beforeEach((event) => {
  256. if (filter !== void 0 && !filter(event.name)) {
  257. return;
  258. }
  259. _idCtr[event.name] = _idCtr[event.name] || 0;
  260. event._id = _idCtr[event.name]++;
  261. console.time(logPrefix(event));
  262. });
  263. const unsubscribeAfter = hooks.afterEach((event) => {
  264. if (filter !== void 0 && !filter(event.name)) {
  265. return;
  266. }
  267. if (options.group) {
  268. console.groupCollapsed(event.name);
  269. }
  270. if (options.inspect) {
  271. console.timeLog(logPrefix(event), event.args);
  272. } else {
  273. console.timeEnd(logPrefix(event));
  274. }
  275. if (options.group) {
  276. console.groupEnd();
  277. }
  278. _idCtr[event.name]--;
  279. });
  280. return {
  281. /** Stop debugging and remove listeners */
  282. close: () => {
  283. unsubscribeBefore();
  284. unsubscribeAfter();
  285. }
  286. };
  287. }
  288. exports.Hookable = Hookable;
  289. exports.createDebugger = createDebugger;
  290. exports.createHooks = createHooks;
  291. exports.flatHooks = flatHooks;
  292. exports.mergeHooks = mergeHooks;
  293. exports.parallelCaller = parallelCaller;
  294. exports.serial = serial;
  295. exports.serialCaller = serialCaller;