hooks: Inline mapFirst() into aCallFirst() for readability

There's only one caller of the function, and the function is simple,
so there's no need for a separate function.
This commit is contained in:
Richard Hansen 2021-01-31 18:35:17 -05:00 committed by John McLear
parent 4ab7a99512
commit 13e806ad7a

View file

@ -40,15 +40,6 @@ const hookCallWrapper = (hook, hookName, context, cb) => {
return () => normalize(hook.hook_fn(hookName, context, (x) => cb(normalize(x)))); return () => normalize(hook.hook_fn(hookName, context, (x) => cb(normalize(x))));
}; };
const mapFirst = async (hooks, fn, predicate = null) => {
if (predicate == null) predicate = (val) => val.length;
for (const hook of hooks) {
const val = await fn(hook);
if (predicate(val)) return val;
}
return [];
};
// Calls the hook function synchronously and returns the value provided by the hook function (via // Calls the hook function synchronously and returns the value provided by the hook function (via
// callback or return value). // callback or return value).
// //
@ -364,12 +355,18 @@ exports.callFirst = (hookName, context) => {
return []; return [];
}; };
const aCallFirst = (hookName, context, cb, predicate) => { const aCallFirst = (hookName, context, cb, predicate = null) => {
if (!context) context = {}; if (!context) context = {};
if (!cb) cb = () => {}; if (!cb) cb = () => {};
if (predicate == null) predicate = (val) => val.length;
const hooks = pluginDefs.hooks[hookName] || []; const hooks = pluginDefs.hooks[hookName] || [];
const fn = async (hook) => await util.promisify(hookCallWrapper)(hook, hookName, context); util.callbackify(async () => {
util.callbackify(mapFirst)(hooks, fn, predicate, cb); for (const hook of hooks) {
const val = await util.promisify(hookCallWrapper)(hook, hookName, context);
if (predicate(val)) return val;
}
return [];
})(cb);
}; };
/* return a Promise if cb is not supplied */ /* return a Promise if cb is not supplied */