mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-31 19:02:59 +01:00
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:
parent
4ab7a99512
commit
13e806ad7a
1 changed files with 9 additions and 12 deletions
|
@ -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 */
|
||||||
|
|
Loading…
Reference in a new issue