index.mjs 8.1 KB

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