2015-05-07 18:00:23 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Callback hooks provide an easy way to add extra steps to common operations.
|
2015-05-07 18:00:23 +09:00
|
|
|
* @namespace Telescope.callbacks
|
|
|
|
*/
|
2015-04-27 10:10:52 +09:00
|
|
|
Telescope.callbacks = {};
|
2015-04-23 15:42:05 +09:00
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Add a callback function to a hook
|
2015-04-23 15:42:05 +09:00
|
|
|
* @param {String} hook - The name of the hook
|
|
|
|
* @param {Function} callback - The callback function
|
|
|
|
*/
|
2015-05-17 15:38:02 +09:00
|
|
|
Telescope.callbacks.add = function (hook, callback) {
|
2015-04-23 15:42:05 +09:00
|
|
|
|
|
|
|
// if callback array doesn't exist yet, initialize it
|
|
|
|
if (typeof Telescope.callbacks[hook] === "undefined") {
|
|
|
|
Telescope.callbacks[hook] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Telescope.callbacks[hook].push(callback);
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2015-04-27 10:10:52 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Remove a callback from a hook
|
2015-04-27 10:10:52 +09:00
|
|
|
* @param {string} hook - The name of the hook
|
|
|
|
* @param {string} functionName - The name of the function to remove
|
|
|
|
*/
|
2015-05-10 14:36:47 +09:00
|
|
|
Telescope.callbacks.remove = function (hookName, callbackName) {
|
|
|
|
Telescope.callbacks[hookName] = _.reject(Telescope.callbacks[hookName], function (callback) {
|
|
|
|
return callback.name === callbackName;
|
2015-04-27 10:10:52 +09:00
|
|
|
});
|
2015-05-01 18:22:00 +02:00
|
|
|
};
|
2015-04-27 10:10:52 +09:00
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Successively run all of a hook's callbacks on an item
|
2015-04-23 15:42:05 +09:00
|
|
|
* @param {String} hook - The name of the hook
|
|
|
|
* @param {Object} item - The post, comment, modifier, etc. on which to run the callbacks
|
2015-05-07 18:00:23 +09:00
|
|
|
* @param {Object} [constant] - An optional constant that will be passed along to each callback
|
|
|
|
* @returns {Object} Returns the item after it's been through all the callbacks for this hook
|
2015-04-23 15:42:05 +09:00
|
|
|
*/
|
2015-05-04 10:19:50 +09:00
|
|
|
Telescope.callbacks.run = function (hook, item, constant) {
|
2015-04-23 15:42:05 +09:00
|
|
|
|
|
|
|
var callbacks = Telescope.callbacks[hook];
|
|
|
|
|
|
|
|
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
|
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
return callbacks.reduce(function(result, callback) {
|
2015-05-04 12:32:00 +09:00
|
|
|
// console.log(callback.name);
|
|
|
|
return callback(result, constant);
|
2015-05-04 10:19:50 +09:00
|
|
|
}, item);
|
2015-05-01 18:22:00 +02:00
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
} else { // else, just return the item unchanged
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
};
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Successively run all of a hook's callbacks on an item, in async mode (only works on server)
|
2015-05-04 10:19:50 +09:00
|
|
|
* @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
|
|
|
|
*/
|
2016-01-02 18:41:45 +01:00
|
|
|
Telescope.callbacks.runAsync = function () {
|
2016-01-03 18:18:09 +01:00
|
|
|
|
2016-01-02 18:41:45 +01:00
|
|
|
// the first argument is the name of the hook
|
|
|
|
var hook = arguments[0];
|
2016-02-24 18:11:53 +09:00
|
|
|
// successive arguments are passed to each iteration
|
2016-01-02 18:41:45 +01:00
|
|
|
var args = Array.prototype.slice.call(arguments).slice(1);
|
2015-05-04 10:19:50 +09:00
|
|
|
var callbacks = Telescope.callbacks[hook];
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
if (Meteor.isServer && typeof callbacks !== "undefined" && !!callbacks.length) {
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
// use defer to avoid holding up client
|
|
|
|
Meteor.defer(function () {
|
|
|
|
// run all post submit server callbacks on post object successively
|
|
|
|
callbacks.forEach(function(callback) {
|
2016-01-02 18:41:45 +01:00
|
|
|
// console.log("// "+hook+": running callback ["+callback.name+"] at "+moment().format("hh:mm:ss"))
|
2016-02-24 18:11:53 +09:00
|
|
|
callback.apply(this, args);
|
2015-05-04 10:19:50 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
}
|
2016-01-03 18:18:09 +01:00
|
|
|
|
2015-05-04 10:19:50 +09:00
|
|
|
};
|