pluginfw/hooks.js: allow returning a Promise in aCallFirst(), aCallAll()

Since this code can end up loaded in browsers when using client side plugins,
avoid use of ES6 syntax features such as arrow functions until MSIE support is
finally dropped.
pull/3559/head
Ray Bellis 2019-01-18 13:49:17 +00:00
parent 3802073695
commit 8d85ae582e
1 changed files with 28 additions and 2 deletions

View File

@ -78,7 +78,7 @@ exports.callAll = function (hook_name, args) {
} }
} }
exports.aCallAll = function (hook_name, args, cb) { function aCallAll(hook_name, args, cb) {
if (!args) args = {}; if (!args) args = {};
if (!cb) cb = function () {}; if (!cb) cb = function () {};
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []); if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
@ -93,6 +93,19 @@ exports.aCallAll = function (hook_name, args, cb) {
); );
} }
/* return a Promise if cb is not supplied */
exports.aCallAll = function (hook_name, args, cb) {
if (cb === undefined) {
return new Promise(function(resolve, reject) {
aCallAll(hook_name, args, function(err, res) {
return err ? reject(err) : resolve(res);
});
});
} else {
return aCallAll(hook_name, args, cb);
}
}
exports.callFirst = function (hook_name, args) { exports.callFirst = function (hook_name, args) {
if (!args) args = {}; if (!args) args = {};
if (exports.plugins.hooks[hook_name] === undefined) return []; if (exports.plugins.hooks[hook_name] === undefined) return [];
@ -101,7 +114,7 @@ exports.callFirst = function (hook_name, args) {
}); });
} }
exports.aCallFirst = function (hook_name, args, cb) { function aCallFirst(hook_name, args, cb) {
if (!args) args = {}; if (!args) args = {};
if (!cb) cb = function () {}; if (!cb) cb = function () {};
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []); if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
@ -114,6 +127,19 @@ exports.aCallFirst = function (hook_name, args, cb) {
); );
} }
/* return a Promise if cb is not supplied */
exports.aCallFirst = function (hook_name, args, cb) {
if (cb === undefined) {
return new Promise(function(resolve, reject) {
aCallFirst(hook_name, args, function(err, res) {
return err ? reject(err) : resolve(res);
});
});
} else {
return aCallFirst(hook_name, args, cb);
}
}
exports.callAllStr = function(hook_name, args, sep, pre, post) { exports.callAllStr = function(hook_name, args, sep, pre, post) {
if (sep == undefined) sep = ''; if (sep == undefined) sep = '';
if (pre == undefined) pre = ''; if (pre == undefined) pre = '';