2017-09-14 19:47:35 +02:00
|
|
|
import { debug } from './debug.js';
|
|
|
|
|
2015-05-07 18:00:23 +09:00
|
|
|
/**
|
2016-11-26 02:46:55 +08:00
|
|
|
* @summary Callback hooks provide an easy way to add extra steps to common operations.
|
2016-12-12 10:24:34 +09:00
|
|
|
* @namespace Callbacks
|
2015-05-07 18:00:23 +09:00
|
|
|
*/
|
2016-12-12 10:24:34 +09:00
|
|
|
export const 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
|
|
|
|
*/
|
2016-12-12 10:24:34 +09:00
|
|
|
export const addCallback = function (hook, callback) {
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2017-04-14 11:37:30 +09:00
|
|
|
if (!callback.name) {
|
|
|
|
console.log(`// Warning! You are adding an unnamed callback to ${hook}. Please use the function foo () {} syntax.`);
|
|
|
|
}
|
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
// if callback array doesn't exist yet, initialize it
|
2016-12-12 10:24:34 +09:00
|
|
|
if (typeof Callbacks[hook] === "undefined") {
|
|
|
|
Callbacks[hook] = [];
|
2015-04-23 15:42:05 +09:00
|
|
|
}
|
|
|
|
|
2016-12-12 10:24:34 +09:00
|
|
|
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
|
|
|
|
*/
|
2016-12-12 10:24:34 +09:00
|
|
|
export const removeCallback = function (hookName, callbackName) {
|
|
|
|
Callbacks[hookName] = _.reject(Callbacks[hookName], function (callback) {
|
2015-05-10 14:36:47 +09:00
|
|
|
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
|
2016-04-14 15:54:23 +09:00
|
|
|
* @param {String} hook - First argument: the name of the hook
|
|
|
|
* @param {Object} item - Second argument: the post, comment, modifier, etc. on which to run the callbacks
|
|
|
|
* @param {Any} args - Other arguments will be passed to each successive iteration
|
2015-05-07 18:00:23 +09:00
|
|
|
* @returns {Object} Returns the item after it's been through all the callbacks for this hook
|
2015-04-23 15:42:05 +09:00
|
|
|
*/
|
2016-12-12 10:24:34 +09:00
|
|
|
export const runCallbacks = function () {
|
2015-04-23 15:42:05 +09:00
|
|
|
|
2017-05-30 09:49:38 +09:00
|
|
|
// the first argument is the name of the hook or an array of functions
|
2016-04-14 15:54:23 +09:00
|
|
|
const hook = arguments[0];
|
|
|
|
// the second argument is the item on which to iterate
|
|
|
|
const item = arguments[1];
|
|
|
|
// successive arguments are passed to each iteration
|
|
|
|
const args = Array.prototype.slice.call(arguments).slice(2);
|
|
|
|
|
2017-05-30 09:49:38 +09:00
|
|
|
const callbacks = Array.isArray(hook) ? hook : Callbacks[hook];
|
2015-04-23 15:42:05 +09:00
|
|
|
|
|
|
|
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
|
|
|
|
|
2017-05-14 10:42:19 +09:00
|
|
|
return callbacks.reduce(function(accumulator, callback) {
|
2017-05-23 09:46:34 +09:00
|
|
|
|
2017-09-14 19:47:35 +02:00
|
|
|
debug(`// Running callback [${callback.name}] on hook [${hook}]`);
|
|
|
|
|
2017-05-14 10:42:19 +09:00
|
|
|
const newArguments = [accumulator].concat(args);
|
2017-05-23 09:46:34 +09:00
|
|
|
|
2017-05-14 10:42:19 +09:00
|
|
|
try {
|
2017-05-23 09:46:34 +09:00
|
|
|
const result = callback.apply(this, newArguments);
|
|
|
|
|
2017-08-21 15:35:19 +09:00
|
|
|
// if callback is only supposed to run once, remove it
|
|
|
|
if (callback.runOnce) {
|
|
|
|
removeCallback(hook, callback.name);
|
|
|
|
}
|
|
|
|
|
2017-05-23 09:46:34 +09:00
|
|
|
if (typeof result === 'undefined') {
|
|
|
|
// if result of current iteration is undefined, don't pass it on
|
|
|
|
console.log(`// Warning: Sync callback [${callback.name}] in hook [${hook}] didn't return a result!`)
|
|
|
|
return accumulator
|
|
|
|
} else {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-14 10:42:19 +09:00
|
|
|
} catch (error) {
|
|
|
|
console.log(`// error at callback [${callback.name}] in hook [${hook}]`);
|
|
|
|
console.log(error);
|
2017-07-07 10:21:15 +09:00
|
|
|
if (error.break || error.data && error.data.break) {
|
2017-07-07 09:50:05 +09:00
|
|
|
throw error;
|
|
|
|
}
|
2017-05-23 09:46:34 +09:00
|
|
|
// pass the unchanged accumulator to the next iteration of the loop
|
2017-05-14 10:42:19 +09:00
|
|
|
return accumulator;
|
|
|
|
}
|
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)
|
2016-04-14 15:54:23 +09:00
|
|
|
* @param {String} hook - First argument: the name of the hook
|
|
|
|
* @param {Any} args - Other arguments will be passed to each successive iteration
|
2015-05-04 10:19:50 +09:00
|
|
|
*/
|
2016-12-12 10:24:34 +09:00
|
|
|
export const runCallbacksAsync = function () {
|
2016-01-03 18:18:09 +01:00
|
|
|
|
2017-05-30 09:49:38 +09:00
|
|
|
// the first argument is the name of the hook or an array of functions
|
2016-01-02 18:41:45 +01:00
|
|
|
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);
|
2017-05-30 09:49:38 +09:00
|
|
|
|
|
|
|
const callbacks = Array.isArray(hook) ? hook : 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) {
|
2017-10-18 20:06:31 +09:00
|
|
|
debug(`// Running async callback [${callback.name}] on hook [${hook}]`);
|
2016-02-24 18:11:53 +09:00
|
|
|
callback.apply(this, args);
|
2015-05-04 10:19:50 +09:00
|
|
|
});
|
|
|
|
});
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2015-04-23 15:42:05 +09:00
|
|
|
}
|
2016-01-03 18:18:09 +01:00
|
|
|
|
2016-12-13 11:32:23 +09:00
|
|
|
};
|