Vulcan/packages/telescope-lib/lib/callbacks.js
Maxime Quandalle 94c6121d91 Improve jsHint consistency
This commit touch a lot of lines of code with the goal to be more
rigorous about JavaScript code conventions defined in the `.jshintrc`.

Some modification:

* Add a list of used global symbols in the corresponding section of
  `.jshintrc`
* Use local variables instead of global in a lot of places where the
  keyword `var` was mistakenly forgotten
* Add missing semi-colons after instructions
* Add new lines at the end of files
* Remove trailing whitespaces
* Use newer name of some Meteor APIs, eg `addFiles` instead of
  `add_files`
* Add missing `break` statements in `switch` blocks
* Use `===` instead of `==` and `!==` instead of `!=`
* Remove unused variables

This commit should also fix a few bugs due to this lack of rigor. One
example of that was the test `typeof navElements === "array"` that was
never true because in JavaScript, `typeof [] === "object"`, we
replaced this test by the `_.isArray` method provided by underscore.
It might also fix some potential collision related to global
variables.

There is still plenty of work until Telescope code base passes jsHint
validation, but at least this commit is a step in the right direction.
2015-05-01 18:38:27 +02:00

66 lines
1.9 KiB
JavaScript

Telescope.callbacks = {};
/**
* Add a callback function to a hook
* @param {String} hook - The name of the hook
* @param {Function} callback - The callback function
*/
Telescope.callbacks.register = function (hook, callback) {
// if callback array doesn't exist yet, initialize it
if (typeof Telescope.callbacks[hook] === "undefined") {
Telescope.callbacks[hook] = [];
}
Telescope.callbacks[hook].push(callback);
};
/**
* Remove a callback from a hook
* @param {string} hook - The name of the hook
* @param {string} functionName - The name of the function to remove
*/
Telescope.callbacks.remove = function (hook, functionName) {
Telescope.callbacks[hook] = _.reject(Telescope.callbacks[hook], function (callback) {
return callback.name === functionName;
});
};
/**
* Successively run all of a hook's callbacks on an item
* @param {String} hook - The name of the hook
* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
* @param {Boolean} async - Whether to run the callback in async mode or not
*/
Telescope.callbacks.run = function (hook, item, async) {
async = async || false; // default to sync
var callbacks = Telescope.callbacks[hook];
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
if (async) { // run callbacks in async mode, without returning anything
if (Meteor.isServer) {
// use defer to avoid holding up client
Meteor.defer(function () {
// run all post submit server callbacks on post object successively
callbacks.forEach(function(callback) {
callback(item);
});
});
}
} else { // else run callbacks in sync mode, and return the modified item
return callbacks.reduce(function(result, callback) {
return callback(result);
}, item);
}
} else { // else, just return the item unchanged
return item;
}
};