mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-31 19:02:59 +01:00
hooks: Factor out callback attachment
The separate function will be reused in a future commit.
This commit is contained in:
parent
13e806ad7a
commit
708206449a
1 changed files with 10 additions and 6 deletions
|
@ -24,6 +24,11 @@ const checkDeprecation = (hook) => {
|
||||||
deprecationWarned[hook.hook_fn_name] = true;
|
deprecationWarned[hook.hook_fn_name] = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Calls the node-style callback when the Promise settles. Unlike util.callbackify, this takes a
|
||||||
|
// Promise (rather than a function that returns a Promise), and it returns a Promise (rather than a
|
||||||
|
// function that returns undefined).
|
||||||
|
const attachCallback = (p, cb) => p.then((val) => cb(null, val), cb);
|
||||||
|
|
||||||
// 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), []);
|
||||||
|
|
||||||
|
@ -333,15 +338,14 @@ const callHookFnAsync = async (hook, context) => {
|
||||||
// 2. Convert each `undefined` entry into `[]`.
|
// 2. Convert each `undefined` entry into `[]`.
|
||||||
// 3. Flatten one level.
|
// 3. Flatten one level.
|
||||||
// If cb is non-null, this function resolves to the value returned by cb.
|
// If cb is non-null, this function resolves to the value returned by cb.
|
||||||
exports.aCallAll = async (hookName, context, cb) => {
|
exports.aCallAll = async (hookName, context, cb = null) => {
|
||||||
|
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] || [];
|
||||||
let resultsPromise = Promise.all(hooks.map((hook) => callHookFnAsync(hook, context)
|
const results = await Promise.all(hooks.map((hook) => callHookFnAsync(hook, context)
|
||||||
// `undefined` (but not `null`!) is treated the same as [].
|
// `undefined` (but not `null`!) is treated the same as [].
|
||||||
.then((result) => (result === undefined) ? [] : result)))
|
.then((result) => (result === undefined) ? [] : result)));
|
||||||
.then(flatten1);
|
return flatten1(results);
|
||||||
if (cb != null) resultsPromise = resultsPromise.then((val) => cb(null, val), cb);
|
|
||||||
return await resultsPromise;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.callFirst = (hookName, context) => {
|
exports.callFirst = (hookName, context) => {
|
||||||
|
|
Loading…
Reference in a new issue