From 4ab7a99512a69b5438b45a7f37a2b824433f4eec Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sun, 31 Jan 2021 18:35:17 -0500 Subject: [PATCH] hooks: Inline `syncMapFirst()` into `callFirst()` for readability There's only one caller of the function, and the function is simple, so there's no need for a separate function. --- src/static/js/pluginfw/hooks.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.js index de7ddc32c..4370b87f8 100644 --- a/src/static/js/pluginfw/hooks.js +++ b/src/static/js/pluginfw/hooks.js @@ -40,15 +40,6 @@ const hookCallWrapper = (hook, hookName, context, cb) => { return () => normalize(hook.hook_fn(hookName, context, (x) => cb(normalize(x)))); }; -const syncMapFirst = (hooks, fn) => { - const predicate = (val) => val.length; - for (const hook of hooks) { - const val = fn(hook); - if (predicate(val)) return val; - } - return []; -}; - const mapFirst = async (hooks, fn, predicate = null) => { if (predicate == null) predicate = (val) => val.length; for (const hook of hooks) { @@ -364,9 +355,13 @@ exports.aCallAll = async (hookName, context, cb) => { exports.callFirst = (hookName, context) => { if (!context) context = {}; - if (pluginDefs.hooks[hookName] === undefined) return []; - return syncMapFirst(pluginDefs.hooks[hookName], - (hook) => hookCallWrapper(hook, hookName, context)); + const predicate = (val) => val.length; + const hooks = pluginDefs.hooks[hookName] || []; + for (const hook of hooks) { + const val = hookCallWrapper(hook, hookName, context); + if (predicate(val)) return val; + } + return []; }; const aCallFirst = (hookName, context, cb, predicate) => {