Bugfix for callFirst to actually call call hooks until it finds one that returns non-empty, which is actually usefull, as opposed to just calling one hook.
This commit is contained in:
parent
ac36a99a72
commit
7b39da2d69
|
@ -4,27 +4,63 @@ var _;
|
||||||
/* FIXME: Ugly hack, in the future, use same code for server & client */
|
/* FIXME: Ugly hack, in the future, use same code for server & client */
|
||||||
if (plugins.isClient) {
|
if (plugins.isClient) {
|
||||||
var async = require("ep_etherpad-lite/static/js/pluginfw/async");
|
var async = require("ep_etherpad-lite/static/js/pluginfw/async");
|
||||||
_ = require("ep_etherpad-lite/static/js/underscore");
|
var _ = require("ep_etherpad-lite/static/js/underscore");
|
||||||
} else {
|
} else {
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
_ = require("underscore");
|
var _ = require("underscore");
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.bubbleExceptions = true
|
exports.bubbleExceptions = true
|
||||||
|
|
||||||
var hookCallWrapper = function (hook, hook_name, args, cb) {
|
var hookCallWrapper = function (hook, hook_name, args, cb) {
|
||||||
if (cb === undefined) cb = function (x) { return x; };
|
if (cb === undefined) cb = function (x) { return x; };
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
if (exports.bubbleExceptions) {
|
if (exports.bubbleExceptions) {
|
||||||
return hook.hook_fn(hook_name, args, cb);
|
return normalizedhook();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
return hook.hook_fn(hook_name, args, cb);
|
return normalizedhook();
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.error([hook_name, hook.part.full_name, ex.stack || ex]);
|
console.error([hook_name, hook.part.full_name, ex.stack || ex]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Don't use Array.concat as it flatterns arrays within the array */
|
/* Don't use Array.concat as it flatterns arrays within the array */
|
||||||
exports.flatten = function (lst) {
|
exports.flatten = function (lst) {
|
||||||
|
@ -44,9 +80,9 @@ exports.flatten = function (lst) {
|
||||||
exports.callAll = function (hook_name, args) {
|
exports.callAll = function (hook_name, args) {
|
||||||
if (!args) args = {};
|
if (!args) args = {};
|
||||||
if (plugins.hooks[hook_name] === undefined) return [];
|
if (plugins.hooks[hook_name] === undefined) return [];
|
||||||
return exports.flatten(_.map(plugins.hooks[hook_name], function (hook) {
|
return _.flatten(_.map(plugins.hooks[hook_name], function (hook) {
|
||||||
return hookCallWrapper(hook, hook_name, args);
|
return hookCallWrapper(hook, hook_name, args);
|
||||||
}));
|
}), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.aCallAll = function (hook_name, args, cb) {
|
exports.aCallAll = function (hook_name, args, cb) {
|
||||||
|
@ -59,7 +95,7 @@ exports.aCallAll = function (hook_name, args, cb) {
|
||||||
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
|
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
|
||||||
},
|
},
|
||||||
function (err, res) {
|
function (err, res) {
|
||||||
cb(null, exports.flatten(res));
|
cb(null, _.flatten(res, true));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -67,14 +103,22 @@ exports.aCallAll = function (hook_name, args, cb) {
|
||||||
exports.callFirst = function (hook_name, args) {
|
exports.callFirst = function (hook_name, args) {
|
||||||
if (!args) args = {};
|
if (!args) args = {};
|
||||||
if (plugins.hooks[hook_name][0] === undefined) return [];
|
if (plugins.hooks[hook_name][0] === undefined) return [];
|
||||||
return exports.flatten(hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args));
|
return exports.syncMapFirst(plugins.hooks[hook_name], function (hook) {
|
||||||
|
return hookCallWrapper(hook, hook_name, args);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.aCallFirst = function (hook_name, args, cb) {
|
exports.aCallFirst = function (hook_name, args, cb) {
|
||||||
if (!args) args = {};
|
if (!args) args = {};
|
||||||
if (!cb) cb = function () {};
|
if (!cb) cb = function () {};
|
||||||
if (plugins.hooks[hook_name][0] === undefined) return cb(null, []);
|
if (plugins.hooks[hook_name] === undefined) return cb(null, []);
|
||||||
hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args, function (res) { cb(null, exports.flatten(res)); });
|
exports.mapFirst(
|
||||||
|
plugins.hooks[hook_name],
|
||||||
|
function (hook, cb) {
|
||||||
|
hookCallWrapper(hook, hook_name, args, function (res) { cb(null, res); });
|
||||||
|
},
|
||||||
|
cb
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.callAllStr = function(hook_name, args, sep, pre, post) {
|
exports.callAllStr = function(hook_name, args, sep, pre, post) {
|
||||||
|
|
Loading…
Reference in New Issue