mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-03 11:01:20 +01:00
refactor: isHookValid
This commit is contained in:
@@ -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,21 +181,27 @@ 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/
|
||||
const timeout = (prom, time, error) => {
|
||||
let timer;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user