Skip to content

Node.js based forum software built for the modern web

refactor: isHookValid

Barış Soner Uşaklı63c9a6e03c66
diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js
index a64b152b23..d9addd2b18 100644
--- a/src/plugins/hooks.js
+++ b/src/plugins/hooks.js
@@ -159,10 +159,7 @@ async function fireFilterHook(hook, hookList, params) {
}
async function fireMethod(hookObj, params) {
- if (typeof hookObj.method !== 'function') {
- if (process.env.NODE_ENV === 'development') {
- winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
- }
+ if (!isHookValid(hook, hookObj)) {
return params;
}
@@ -184,19 +181,25 @@ async function fireActionHook(hook, hookList, params) {
return;
}
for (const hookObj of hookList) {
- if (typeof hookObj.method !== 'function') {
- if (process.env.NODE_ENV === 'development') {
- winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
- }
- } else {
- try {
- // eslint-disable-next-line
- await hookObj.method(params);
- } catch (err) {
- winston.error(`[plugins] Error in hook ${hookObj.id}@${hookObj.hook} \n${err.stack}`);
- }
+ if (!isHookValid(hook, hookObj)) {
+ continue;
}
+
+ try {
+ // eslint-disable-next-line
+ await hookObj.method(params);
+ } catch (err) {
+ winston.error(`[plugins] Error in hook ${hookObj.id}@${hookObj.hook} \n${err.stack}`);
+ }
+ }
+}
+
+function isHookValid(hook, hookObj) {
+ const isValid = typeof hookObj.method === 'function';
+ if (!isValid && process.env.NODE_ENV === 'development') {
+ winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
}
+ return isValid;
}
// https://advancedweb.hu/how-to-add-timeout-to-a-promise-in-javascript/
@@ -218,10 +221,7 @@ async function fireStaticHook(hook, hookList, params) {
const noErrorHooks = ['static:app.load', 'static:assets.prepare', 'static:app.preload'];
async function fireMethod(hookObj, params) {
- if (typeof hookObj.method !== 'function') {
- if (process.env.NODE_ENV === 'development') {
- winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
- }
+ if (!isHookValid(hook, hookObj)) {
return params;
}
@@ -255,17 +255,16 @@ async function fireResponseHook(hook, hookList, params) {
return;
}
for (const hookObj of hookList) {
- if (typeof hookObj.method !== 'function') {
- if (process.env.NODE_ENV === 'development') {
- winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
- }
- } else {
- // Skip remaining hooks if headers have been sent
- if (params.res.headersSent) {
- return;
- }
- // eslint-disable-next-line
- await hookObj.method(params);
+ if (!isHookValid(hook, hookObj)) {
+ continue;
}
+
+ // Skip remaining hooks if headers have been sent
+ if (params.res.headersSent) {
+ return;
+ }
+
+ // eslint-disable-next-line no-await-in-loop
+ await hookObj.method(params);
}
}