mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-01-19 14:13:34 +01:00
hooks: Asyncify aCallFirst
This commit is contained in:
parent
22d02dbcbf
commit
77f480d954
1 changed files with 8 additions and 16 deletions
|
@ -362,29 +362,21 @@ exports.callFirst = (hookName, context) => {
|
||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
|
|
||||||
const aCallFirst = (hookName, context, cb, predicate = null) => {
|
const aCallFirst = async (hookName, context, predicate = null) => {
|
||||||
if (!context) context = {};
|
if (!context) context = {};
|
||||||
if (!cb) cb = () => {};
|
|
||||||
if (predicate == null) predicate = (val) => val.length;
|
if (predicate == null) predicate = (val) => val.length;
|
||||||
const hooks = pluginDefs.hooks[hookName] || [];
|
const hooks = pluginDefs.hooks[hookName] || [];
|
||||||
util.callbackify(async () => {
|
|
||||||
for (const hook of hooks) {
|
for (const hook of hooks) {
|
||||||
const val = await util.promisify(hookCallWrapper)(hook, hookName, context);
|
const val = await util.promisify(hookCallWrapper)(hook, hookName, context);
|
||||||
if (predicate(val)) return val;
|
if (predicate(val)) return val;
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
})(cb);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* return a Promise if cb is not supplied */
|
/* return a Promise if cb is not supplied */
|
||||||
exports.aCallFirst = (hookName, context, cb, predicate) => {
|
exports.aCallFirst = (hookName, context, cb, predicate) => {
|
||||||
if (cb === undefined) {
|
if (cb == null) return aCallFirst(hookName, context, predicate);
|
||||||
return new Promise((resolve, reject) => {
|
util.callbackify(aCallFirst)(hookName, context, predicate, cb);
|
||||||
aCallFirst(hookName, context, (err, res) => err ? reject(err) : resolve(res), predicate);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
return aCallFirst(hookName, context, cb, predicate);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.exportedForTestingOnly = {
|
exports.exportedForTestingOnly = {
|
||||||
|
|
Loading…
Reference in a new issue