diff options
author | Egil Moeller <egil.moller@freecode.no> | 2012-03-27 22:23:28 +0200 |
---|---|---|
committer | Egil Moeller <egil.moller@freecode.no> | 2012-03-27 22:23:28 +0200 |
commit | 12600446c2a796ac0941b2a1d85784f4d9ca84f3 (patch) | |
tree | 7a5d7fb9a6d51e7d4bec421db7f105002ea88d1c /src | |
parent | d0ad90456ea47215943985b2fe5e26b6aa05f41f (diff) | |
download | etherpad-lite-12600446c2a796ac0941b2a1d85784f4d9ca84f3.zip |
Better argument handling and defaults
Diffstat (limited to 'src')
-rw-r--r-- | src/static/js/pluginfw/hooks.js | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/src/static/js/pluginfw/hooks.js b/src/static/js/pluginfw/hooks.js index 0b96c882..3be90792 100644 --- a/src/static/js/pluginfw/hooks.js +++ b/src/static/js/pluginfw/hooks.js @@ -7,12 +7,18 @@ if (plugins.isClient) { var async = require("async"); } +exports.bubbleExceptions = true + var hookCallWrapper = function (hook, hook_name, args, cb) { if (cb === undefined) cb = function (x) { return x; }; - try { + if (exports.bubbleExceptions) { return hook.hook_fn(hook_name, args, cb); - } catch (ex) { - console.error([hook_name, hook.part.full_name, ex.stack || ex]); + } else { + try { + return hook.hook_fn(hook_name, args, cb); + } catch (ex) { + console.error([hook_name, hook.part.full_name, ex.stack || ex]); + } } } @@ -33,6 +39,7 @@ exports.flatten = function (lst) { } exports.callAll = function (hook_name, args) { + if (!args) args = {}; if (plugins.hooks[hook_name] === undefined) return []; return exports.flatten(plugins.hooks[hook_name].map(function (hook) { return hookCallWrapper(hook, hook_name, args); @@ -40,6 +47,8 @@ exports.callAll = function (hook_name, args) { } exports.aCallAll = function (hook_name, args, cb) { + if (!args) args = {}; + if (!cb) cb = function () {}; if (plugins.hooks[hook_name] === undefined) return cb(null, []); async.map( plugins.hooks[hook_name], @@ -53,11 +62,14 @@ exports.aCallAll = function (hook_name, args, cb) { } exports.callFirst = function (hook_name, args) { + if (!args) args = {}; if (plugins.hooks[hook_name][0] === undefined) return []; return exports.flatten(hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args)); } exports.aCallFirst = function (hook_name, args, cb) { + if (!args) args = {}; + if (!cb) cb = function () {}; if (plugins.hooks[hook_name][0] === undefined) return cb(null, []); hookCallWrapper(plugins.hooks[hook_name][0], hook_name, args, function (res) { cb(null, exports.flatten(res)); }); } |