mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-03 20:45:58 +01:00
fix: closes #12365, staticHooks with callbacks
This commit is contained in:
@@ -207,22 +207,7 @@ Hooks.hasListeners = function (hook) {
|
||||
return !!(plugins.loadedHooks[hook] && plugins.loadedHooks[hook].length > 0);
|
||||
};
|
||||
|
||||
async function fireFilterHook(hook, hookList, params) {
|
||||
if (!Array.isArray(hookList) || !hookList.length) {
|
||||
return params;
|
||||
}
|
||||
|
||||
async function fireMethod(hookObj, params) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
if (hookObj.method.constructor && hookObj.method.constructor.name === 'AsyncFunction') {
|
||||
return await hookObj.method(params);
|
||||
}
|
||||
function hookHandlerPromise(hook, hookObj, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let resolved = false;
|
||||
function _resolve(result) {
|
||||
@@ -248,6 +233,25 @@ async function fireFilterHook(hook, hookList, params) {
|
||||
_resolve(returned);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function fireFilterHook(hook, hookList, params) {
|
||||
if (!Array.isArray(hookList) || !hookList.length) {
|
||||
return params;
|
||||
}
|
||||
|
||||
async function fireMethod(hookObj, params) {
|
||||
if (typeof hookObj.method !== 'function') {
|
||||
if (global.env === 'development') {
|
||||
winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
|
||||
}
|
||||
return params;
|
||||
}
|
||||
|
||||
if (hookObj.method.constructor && hookObj.method.constructor.name === 'AsyncFunction') {
|
||||
return await hookObj.method(params);
|
||||
}
|
||||
return hookHandlerPromise(hook, hookObj, params);
|
||||
}
|
||||
|
||||
for (const hookObj of hookList) {
|
||||
@@ -303,28 +307,7 @@ async function fireStaticHook(hook, hookList, params) {
|
||||
return timeout(hookObj.method(params), 10000, 'timeout');
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
let resolved = false;
|
||||
function _resolve(result) {
|
||||
if (resolved) {
|
||||
return;
|
||||
}
|
||||
resolved = true;
|
||||
resolve(result);
|
||||
}
|
||||
const returned = hookObj.method(params, (err, result) => {
|
||||
if (err) reject(err); else _resolve(result);
|
||||
});
|
||||
|
||||
if (utils.isPromise(returned)) {
|
||||
returned.then(
|
||||
payload => _resolve(payload),
|
||||
err => reject(err)
|
||||
);
|
||||
return;
|
||||
}
|
||||
_resolve();
|
||||
});
|
||||
return hookHandlerPromise(hook, hookObj, params);
|
||||
}
|
||||
|
||||
for (const hookObj of hookList) {
|
||||
|
||||
Reference in New Issue
Block a user