hooks: Asyncify aCallFirst

This commit is contained in:
Richard Hansen 2021-02-01 00:03:06 -05:00 committed by John McLear
parent 22d02dbcbf
commit 77f480d954

View file

@ -362,29 +362,21 @@ exports.callFirst = (hookName, context) => {
return []; return [];
}; };
const aCallFirst = (hookName, context, cb, predicate = null) => { const aCallFirst = async (hookName, context, predicate = null) => {
if (!context) context = {}; if (!context) context = {};
if (!cb) cb = () => {};
if (predicate == null) predicate = (val) => val.length; if (predicate == null) predicate = (val) => val.length;
const hooks = pluginDefs.hooks[hookName] || []; const hooks = pluginDefs.hooks[hookName] || [];
util.callbackify(async () => {
for (const hook of hooks) { for (const hook of hooks) {
const val = await util.promisify(hookCallWrapper)(hook, hookName, context); const val = await util.promisify(hookCallWrapper)(hook, hookName, context);
if (predicate(val)) return val; if (predicate(val)) return val;
} }
return []; return [];
})(cb);
}; };
/* return a Promise if cb is not supplied */ /* return a Promise if cb is not supplied */
exports.aCallFirst = (hookName, context, cb, predicate) => { exports.aCallFirst = (hookName, context, cb, predicate) => {
if (cb === undefined) { if (cb == null) return aCallFirst(hookName, context, predicate);
return new Promise((resolve, reject) => { util.callbackify(aCallFirst)(hookName, context, predicate, cb);
aCallFirst(hookName, context, (err, res) => err ? reject(err) : resolve(res), predicate);
});
} else {
return aCallFirst(hookName, context, cb, predicate);
}
}; };
exports.exportedForTestingOnly = { exports.exportedForTestingOnly = {