feat: collect hook logs in order to reduce console noise, flush on ajaxify loadScript completion

isekai-main
Julian Lam 3 years ago
parent e578c60566
commit 935704a83c

@ -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);

@ -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;

Loading…
Cancel
Save