From 53f67ff3964a2a9ebdcd269ecc852aa00cb851e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sat, 13 Mar 2021 10:12:30 -0500 Subject: [PATCH] fix: regression from filter hook change --- src/plugins/hooks.js | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/src/plugins/hooks.js b/src/plugins/hooks.js index cc7a2da5b5..95b5aec47a 100644 --- a/src/plugins/hooks.js +++ b/src/plugins/hooks.js @@ -1,8 +1,10 @@ 'use strict'; +const async = require('async'); const util = require('util'); const winston = require('winston'); const plugins = require('.'); +const utils = require('../utils'); const Hooks = module.exports; @@ -114,22 +116,37 @@ async function fireFilterHook(hook, hookList, params) { if (!Array.isArray(hookList) || !hookList.length) { return params; } - - for (const hookObj of hookList) { + return await async.reduce(hookList, params, (params, hookObj, next) => { 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 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) + ); + } + }); + // 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; } async function fireActionHook(hook, hookList, params) {