feat: allow filter functions that return promises or the data directly

This commit is contained in:
Barış Soner Uşaklı
2021-03-13 11:27:53 -05:00
parent 53f67ff396
commit e6c52cf26c
2 changed files with 34 additions and 32 deletions

View File

@@ -1,6 +1,5 @@
'use strict';
const async = require('async');
const util = require('util');
const winston = require('winston');
const plugins = require('.');
@@ -116,37 +115,38 @@ async function fireFilterHook(hook, hookList, params) {
if (!Array.isArray(hookList) || !hookList.length) {
return params;
}
return await async.reduce(hookList, params, (params, hookObj, next) => {
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 next(null, params);
return params;
}
const returned = hookObj.method(params, next);
if (utils.isPromise(returned)) {
returned.then(
payload => setImmediate(next, null, payload),
err => setImmediate(next, err)
);
if (hookObj.method.constructor && hookObj.method.constructor.name === 'AsyncFunction') {
return await hookObj.method(params);
}
});
// breaks plugins that use a non-async function ie emoji-one parse.raw
// for (const hookObj of hookList) {
// if (typeof hookObj.method !== 'function') {
// if (global.env === 'development') {
// winston.warn(`[plugins] Expected method for hook '${hook}' in plugin '${hookObj.id}' not found, skipping.`);
// }
// } else {
// let hookFn = hookObj.method;
// if (hookFn.constructor && hookFn.constructor.name !== 'AsyncFunction') {
// hookFn = util.promisify(hookFn);
// }
// // eslint-disable-next-line
// params = await hookFn(params);
// }
// }
// return params;
return new Promise((resolve, reject) => {
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(returned);
});
}
for (const hookObj of hookList) {
// eslint-disable-next-line
params = await fireMethod(hookObj, params);
}
return params;
}
async function fireActionHook(hook, hookList, params) {