mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-13 09:25:45 +01:00
Merge branch 'master' into develop
This commit is contained in:
@@ -207,22 +207,7 @@ Hooks.hasListeners = function (hook) {
|
|||||||
return !!(plugins.loadedHooks[hook] && plugins.loadedHooks[hook].length > 0);
|
return !!(plugins.loadedHooks[hook] && plugins.loadedHooks[hook].length > 0);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function fireFilterHook(hook, hookList, params) {
|
function hookHandlerPromise(hook, hookObj, 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 new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let resolved = false;
|
let resolved = false;
|
||||||
function _resolve(result) {
|
function _resolve(result) {
|
||||||
@@ -250,6 +235,25 @@ async function fireFilterHook(hook, hookList, params) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
for (const hookObj of hookList) {
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
params = await fireMethod(hookObj, params);
|
params = await fireMethod(hookObj, params);
|
||||||
@@ -303,28 +307,7 @@ async function fireStaticHook(hook, hookList, params) {
|
|||||||
return timeout(hookObj.method(params), 10000, 'timeout');
|
return timeout(hookObj.method(params), 10000, 'timeout');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return hookHandlerPromise(hook, hookObj, params);
|
||||||
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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const hookObj of hookList) {
|
for (const hookObj of hookList) {
|
||||||
|
|||||||
Reference in New Issue
Block a user