2012-05-29 02:23:43 +02:00
|
|
|
var async = require("async");
|
|
|
|
var _ = require("underscore");
|
2012-03-01 18:45:02 +01:00
|
|
|
|
2012-03-27 22:23:28 +02:00
|
|
|
exports.bubbleExceptions = true
|
|
|
|
|
2012-03-01 18:45:02 +01:00
|
|
|
var hookCallWrapper = function (hook, hook_name, args, cb) {
|
|
|
|
if (cb === undefined) cb = function (x) { return x; };
|
2012-04-19 16:03:42 +02:00
|
|
|
|
|
|
|
// Normalize output to list for both sync and async cases
|
|
|
|
var normalize = function(x) {
|
|
|
|
if (x == undefined) return [];
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
var normalizedhook = function () {
|
|
|
|
return normalize(hook.hook_fn(hook_name, args, function (x) {
|
|
|
|
return cb(normalize(x));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:23:28 +02:00
|
|
|
if (exports.bubbleExceptions) {
|
2012-04-19 16:03:42 +02:00
|
|
|
return normalizedhook();
|
2012-03-27 22:23:28 +02:00
|
|
|
} else {
|
|
|
|
try {
|
2012-04-19 16:03:42 +02:00
|
|
|
return normalizedhook();
|
2012-03-27 22:23:28 +02:00
|
|
|
} catch (ex) {
|
|
|
|
console.error([hook_name, hook.part.full_name, ex.stack || ex]);
|
|
|
|
}
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 16:03:42 +02:00
|
|
|
exports.syncMapFirst = function (lst, fn) {
|
|
|
|
var i;
|
|
|
|
var result;
|
|
|
|
for (i = 0; i < lst.length; i++) {
|
|
|
|
result = fn(lst[i])
|
|
|
|
if (result.length) return result;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.mapFirst = function (lst, fn, cb) {
|
|
|
|
var i = 0;
|
|
|
|
|
|
|
|
next = function () {
|
|
|
|
if (i >= lst.length) return cb(undefined);
|
|
|
|
fn(lst[i++], function (err, result) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
if (result.length) return cb(null, result);
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
2012-03-01 18:45:02 +01:00
|
|
|
|
|
|
|
/* 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) {
|
2012-03-27 22:23:28 +02:00
|
|
|
if (!args) args = {};
|
2012-05-29 03:58:55 +02:00
|
|
|
if (exports.plugins.hooks[hook_name] === undefined) return [];
|
|
|
|
return _.flatten(_.map(exports.plugins.hooks[hook_name], function (hook) {
|
2012-03-01 18:45:02 +01:00
|
|
|
return hookCallWrapper(hook, hook_name, args);
|
2012-04-19 16:03:42 +02:00
|
|
|
}), true);
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.aCallAll = function (hook_name, args, cb) {
|
2012-03-27 22:23:28 +02:00
|
|
|
if (!args) args = {};
|
|
|
|
if (!cb) cb = function () {};
|
2012-05-29 03:58:55 +02:00
|
|
|
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
|
2012-03-01 18:45:02 +01:00
|
|
|
async.map(
|
2012-05-29 03:58:55 +02:00
|
|
|
exports.plugins.hooks[hook_name],
|
2012-03-01 18:45:02 +01:00
|
|
|
function (hook, cb) {
|
|
|
|
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
|
|
|
|
},
|
|
|
|
function (err, res) {
|
2012-04-19 16:03:42 +02:00
|
|
|
cb(null, _.flatten(res, true));
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.callFirst = function (hook_name, args) {
|
2012-03-27 22:23:28 +02:00
|
|
|
if (!args) args = {};
|
2012-05-29 03:58:55 +02:00
|
|
|
if (exports.plugins.hooks[hook_name] === undefined) return [];
|
|
|
|
return exports.syncMapFirst(exports.plugins.hooks[hook_name], function (hook) {
|
2012-04-19 16:03:42 +02:00
|
|
|
return hookCallWrapper(hook, hook_name, args);
|
|
|
|
});
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.aCallFirst = function (hook_name, args, cb) {
|
2012-03-27 22:23:28 +02:00
|
|
|
if (!args) args = {};
|
|
|
|
if (!cb) cb = function () {};
|
2012-05-29 03:58:55 +02:00
|
|
|
if (exports.plugins.hooks[hook_name] === undefined) return cb(null, []);
|
2012-04-19 16:03:42 +02:00
|
|
|
exports.mapFirst(
|
2012-05-29 03:58:55 +02:00
|
|
|
exports.plugins.hooks[hook_name],
|
2012-04-19 16:03:42 +02:00
|
|
|
function (hook, cb) {
|
|
|
|
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
|
|
|
|
},
|
|
|
|
cb
|
|
|
|
);
|
2012-03-01 18:45:02 +01:00
|
|
|
}
|
2012-03-01 19:22:02 +01:00
|
|
|
|
|
|
|
exports.callAllStr = function(hook_name, args, sep, pre, post) {
|
|
|
|
if (sep == undefined) sep = '';
|
|
|
|
if (pre == undefined) pre = '';
|
|
|
|
if (post == undefined) post = '';
|
|
|
|
var newCallhooks = [];
|
|
|
|
var callhooks = exports.callAll(hook_name, args);
|
|
|
|
for (var i = 0, ii = callhooks.length; i < ii; i++) {
|
|
|
|
newCallhooks[i] = pre + callhooks[i] + post;
|
|
|
|
}
|
|
|
|
return newCallhooks.join(sep || "");
|
|
|
|
}
|