hooks: Use `callHookFn{Sync,Async}()` for `{call,aCall}First()`
Benefits of `callHookFnSync()` and `callHookFnAsync()`: * They are a lot more forgiving than `hookCallWrapper()` was. * They perform useful sanity checks. * They have extensive unit test coverage. * They make the behavior of `callFirst()` and `aCallFirst()` match the behavior of `callAll()` and `aCallAll()`.pull/4698/head
parent
c11d60c5f6
commit
6f30ea7c38
|
@ -1,7 +1,6 @@
|
|||
'use strict';
|
||||
|
||||
const pluginDefs = require('./plugin_defs');
|
||||
const util = require('util');
|
||||
|
||||
// Maps the name of a server-side hook to a string explaining the deprecation
|
||||
// (e.g., 'use the foo hook instead').
|
||||
|
@ -47,12 +46,6 @@ const normalizeValue = (val) => {
|
|||
// Flattens the array one level.
|
||||
const flatten1 = (array) => array.reduce((a, b) => a.concat(b), []);
|
||||
|
||||
const hookCallWrapper = (hook, hookName, context, cb) => {
|
||||
if (cb === undefined) cb = (x) => x;
|
||||
checkDeprecation(hook);
|
||||
return () => normalizeValue(hook.hook_fn(hookName, context, (x) => cb(normalizeValue(x))));
|
||||
};
|
||||
|
||||
// Calls the hook function synchronously and returns the value provided by the hook function (via
|
||||
// callback or return value).
|
||||
//
|
||||
|
@ -356,7 +349,7 @@ exports.callFirst = (hookName, context) => {
|
|||
const predicate = (val) => val.length;
|
||||
const hooks = pluginDefs.hooks[hookName] || [];
|
||||
for (const hook of hooks) {
|
||||
const val = hookCallWrapper(hook, hookName, context);
|
||||
const val = normalizeValue(callHookFnSync(hook, context));
|
||||
if (predicate(val)) return val;
|
||||
}
|
||||
return [];
|
||||
|
@ -370,7 +363,7 @@ exports.aCallFirst = async (hookName, context, cb = null, predicate = null) => {
|
|||
if (predicate == null) predicate = (val) => val.length;
|
||||
const hooks = pluginDefs.hooks[hookName] || [];
|
||||
for (const hook of hooks) {
|
||||
const val = await util.promisify(hookCallWrapper)(hook, hookName, context);
|
||||
const val = normalizeValue(await callHookFnAsync(hook, context));
|
||||
if (predicate(val)) return val;
|
||||
}
|
||||
return [];
|
||||
|
|
Loading…
Reference in New Issue