hooks: Asyncify `aCallFirst`

pull/4698/head
Richard Hansen 2021-02-01 00:03:06 -05:00 committed by John McLear
parent 22d02dbcbf
commit 77f480d954
1 changed files with 8 additions and 16 deletions

View File

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