mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 12:36:39 -04:00
74 lines
No EOL
2.3 KiB
JavaScript
74 lines
No EOL
2.3 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 {Object} [constant] - An optional constant that will be passed along to each callback
|
|
*/
|
|
Telescope.callbacks.run = function (hook, item, constant) {
|
|
|
|
var callbacks = Telescope.callbacks[hook];
|
|
|
|
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
|
|
|
|
return callbacks.reduce(function(result, callback) {
|
|
return callback(result);
|
|
}, item);
|
|
|
|
} else { // else, just return the item unchanged
|
|
return item;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Successively run all of a hook's callbacks on an item, in async mode (only works on server)
|
|
* @param {String} hook - The name of the hook
|
|
* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
|
|
* @param {Object} [constant] - An optional constant that will be passed along to each callback
|
|
*/
|
|
Telescope.callbacks.runAsync = function (hook, item, constant) {
|
|
|
|
var callbacks = Telescope.callbacks[hook];
|
|
|
|
if (Meteor.isServer && typeof callbacks !== "undefined" && !!callbacks.length) {
|
|
|
|
// 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, constant);
|
|
});
|
|
});
|
|
|
|
} else {
|
|
return item;
|
|
}
|
|
}; |