mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
hooks: Factor out value normalization
This commit is contained in:
parent
f316a3bacd
commit
22d02dbcbf
1 changed files with 15 additions and 17 deletions
|
@ -33,20 +33,24 @@ const attachCallback = (p, cb) => p.then(
|
||||||
// problems, always pass a truthy value as the first argument if the Promise is rejected.
|
// problems, always pass a truthy value as the first argument if the Promise is rejected.
|
||||||
(err) => cb(err || new Error(err)));
|
(err) => cb(err || new Error(err)));
|
||||||
|
|
||||||
|
// Normalizes the value provided by hook functions so that it is always an array. `undefined` (but
|
||||||
|
// not `null`!) becomes an empty array, array values are returned unmodified, and non-array values
|
||||||
|
// are wrapped in an array (so `null` becomes `[null]`).
|
||||||
|
const normalizeValue = (val) => {
|
||||||
|
// `undefined` is treated the same as `[]`. IMPORTANT: `null` is *not* treated the same as `[]`
|
||||||
|
// because some hooks use `null` as a special value.
|
||||||
|
if (val === undefined) return [];
|
||||||
|
if (Array.isArray(val)) return val;
|
||||||
|
return [val];
|
||||||
|
};
|
||||||
|
|
||||||
// Flattens the array one level.
|
// Flattens the array one level.
|
||||||
const flatten1 = (array) => array.reduce((a, b) => a.concat(b), []);
|
const flatten1 = (array) => array.reduce((a, b) => a.concat(b), []);
|
||||||
|
|
||||||
const hookCallWrapper = (hook, hookName, context, cb) => {
|
const hookCallWrapper = (hook, hookName, context, cb) => {
|
||||||
if (cb === undefined) cb = (x) => x;
|
if (cb === undefined) cb = (x) => x;
|
||||||
|
|
||||||
checkDeprecation(hook);
|
checkDeprecation(hook);
|
||||||
|
return () => normalizeValue(hook.hook_fn(hookName, context, (x) => cb(normalizeValue(x))));
|
||||||
// Normalize output to list for both sync and async cases
|
|
||||||
const normalize = (x) => {
|
|
||||||
if (x === undefined) return [];
|
|
||||||
return x;
|
|
||||||
};
|
|
||||||
return () => normalize(hook.hook_fn(hookName, context, (x) => cb(normalize(x))));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// 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
|
||||||
|
@ -192,12 +196,7 @@ const callHookFnSync = (hook, context) => {
|
||||||
exports.callAll = (hookName, context) => {
|
exports.callAll = (hookName, context) => {
|
||||||
if (context == null) context = {};
|
if (context == null) context = {};
|
||||||
const hooks = pluginDefs.hooks[hookName] || [];
|
const hooks = pluginDefs.hooks[hookName] || [];
|
||||||
return flatten1(hooks.map((hook) => {
|
return flatten1(hooks.map((hook) => normalizeValue(callHookFnSync(hook, context))));
|
||||||
const ret = callHookFnSync(hook, context);
|
|
||||||
// `undefined` (but not `null`!) is treated the same as [].
|
|
||||||
if (ret === undefined) return [];
|
|
||||||
return ret;
|
|
||||||
}));
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Calls the hook function asynchronously and returns a Promise that either resolves to the hook
|
// Calls the hook function asynchronously and returns a Promise that either resolves to the hook
|
||||||
|
@ -347,9 +346,8 @@ exports.aCallAll = async (hookName, context, cb = null) => {
|
||||||
if (cb != null) return await attachCallback(exports.aCallAll(hookName, context), cb);
|
if (cb != null) return await attachCallback(exports.aCallAll(hookName, context), cb);
|
||||||
if (context == null) context = {};
|
if (context == null) context = {};
|
||||||
const hooks = pluginDefs.hooks[hookName] || [];
|
const hooks = pluginDefs.hooks[hookName] || [];
|
||||||
const results = await Promise.all(hooks.map((hook) => callHookFnAsync(hook, context)
|
const results = await Promise.all(
|
||||||
// `undefined` (but not `null`!) is treated the same as [].
|
hooks.map(async (hook) => normalizeValue(await callHookFnAsync(hook, context))));
|
||||||
.then((result) => (result === undefined) ? [] : result)));
|
|
||||||
return flatten1(results);
|
return flatten1(results);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue