mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 23:46:14 -04:00
Added hook.callAll and hook.aCallAll
This commit is contained in:
parent
e994b5b2b1
commit
e5a46f5850
2 changed files with 90 additions and 6 deletions
46
node/pluginfw/hooks.js
Normal file
46
node/pluginfw/hooks.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
var plugins = require("./plugins");
|
||||
var async = require("async");
|
||||
|
||||
|
||||
/* Don't use Array.concat as it flatterns arrays within the array */
|
||||
exports.flatten = function (lst) {
|
||||
var res = [];
|
||||
if (lst != undefined && lst != null) {
|
||||
for (var i = 0; i < lst.length; i++) {
|
||||
if (lst[i] != undefined && lst[i] != null) {
|
||||
for (var j = 0; j < lst[i].length; j++) {
|
||||
res.push(lst[i][j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
exports.callAll = function (hook_name, args) {
|
||||
return exports.flatten(plugins.hooks[hook_name].map(function (hook) {
|
||||
return hook.hook(hook_name, args, function (x) { return x; });
|
||||
}));
|
||||
}
|
||||
|
||||
exports.aCallAll = function (hook_name, args, cb) {
|
||||
async.map(
|
||||
plugins.hooks[hook_name],
|
||||
function (hook, cb) {
|
||||
hook.hook(hook_name, args, function (res) { cb(null, res); });
|
||||
},
|
||||
function (err, res) {
|
||||
cb(exports.flatten(res));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
exports.callFirst = function (hook_name, args) {
|
||||
if (plugins.hooks[hook_name][0] === undefined) return [];
|
||||
return exports.flatten(plugins.hooks[hook_name][0].hook(hook_name, args, function (x) { return x; }));
|
||||
}
|
||||
|
||||
exports.aCallFirst = function (hook_name, args, cb) {
|
||||
if (plugins.hooks[hook_name][0] === undefined) cb([]);
|
||||
plugins.hooks[hook_name][0].hook(hook_name, args, function (res) { cb(exports.flatten(res)); });
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue