From 935704a83caab7e53d46e713f3b40378c9b99b63 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 15 Mar 2022 16:30:05 -0400 Subject: [PATCH] feat: collect hook logs in order to reduce console noise, flush on ajaxify loadScript completion --- public/src/ajaxify.js | 2 ++ public/src/modules/hooks.js | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index e60a618bdb..3ca6c91f68 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -122,6 +122,7 @@ ajaxify = window.ajaxify || {}; url: url, }; + hooks.logs.collect(); hooks.fire('action:ajaxify.start', payload); ajaxify.count += 1; @@ -304,6 +305,7 @@ ajaxify = window.ajaxify || {}; } ajaxify.loadScript(tpl_url, function done() { hooks.fire('action:ajaxify.end', { url: url, tpl_url: tpl_url, title: ajaxify.data.title }); + hooks.logs.flush(); }); ajaxify.widgets.render(tpl_url); diff --git a/public/src/modules/hooks.js b/public/src/modules/hooks.js index 8a83bc3c2c..7ba651e252 100644 --- a/public/src/modules/hooks.js +++ b/public/src/modules/hooks.js @@ -8,6 +8,37 @@ define('hooks', [], () => { deprecated: { }, + logs: { + _collection: new Set(), + }, + }; + + Hooks.logs.collect = () => { + if (Hooks.logs._collection) { + return; + } + + Hooks.logs._collection = new Set(); + }; + + Hooks.logs.log = (...args) => { + if (Hooks.logs._collection) { + Hooks.logs._collection.add(args); + } else { + console.log.apply(console, args); + } + }; + + Hooks.logs.flush = () => { + if (Hooks.logs._collection && Hooks.logs._collection.size) { + console.groupCollapsed('[hooks] Changes to hooks on this page …'); + Hooks.logs._collection.forEach((args) => { + console.log.apply(console, args); + }); + console.groupEnd(); + } + + delete Hooks.logs._collection; }; Hooks.register = (hookName, method) => { @@ -27,7 +58,7 @@ define('hooks', [], () => { console.groupEnd(); } - console.debug(`[hooks] Registered ${hookName}`, method); + Hooks.logs.log(`[hooks] Registered ${hookName}`, method); }; Hooks.on = Hooks.register; Hooks.one = (hookName, method) => { @@ -51,9 +82,9 @@ define('hooks', [], () => { Hooks.unregister = (hookName, method) => { if (Hooks.loaded[hookName] && Hooks.loaded[hookName].has(method)) { Hooks.loaded[hookName].delete(method); - console.debug(`[hooks] Unregistered ${hookName}`, method); + Hooks.logs.log(`[hooks] Unregistered ${hookName}`, method); } else { - console.debug(`[hooks] Unregistration of ${hookName} failed, passed-in method is not a registered listener or the hook itself has no listeners, currently.`); + Hooks.logs.log(`[hooks] Unregistration of ${hookName} failed, passed-in method is not a registered listener or the hook itself has no listeners, currently.`); } }; Hooks.off = Hooks.unregister;