Vulcan/packages/vulcan-lib/lib/modules/callbacks.js

174 lines
6.1 KiB
JavaScript
Raw Normal View History

2017-09-14 19:47:35 +02:00
import { debug } from './debug.js';
2018-05-10 13:13:49 +02:00
import { Utils } from './utils';
2017-09-14 19:47:35 +02:00
2017-10-21 15:58:02 +09:00
/**
* @summary A list of all registered callback hooks
*/
export const CallbackHooks = [];
2015-05-07 18:00:23 +09:00
/**
* @summary Callback hooks provide an easy way to add extra steps to common operations.
* @namespace Callbacks
2015-05-07 18:00:23 +09:00
*/
export const Callbacks = {};
2017-10-21 15:58:02 +09:00
/**
* @summary Register a callback
* @param {String} hook - The name of the hook
* @param {Function} callback - The callback function
*/
export const registerCallback = function (callback) {
CallbackHooks.push(callback);
};
/**
* @summary Add a callback function to a hook
* @param {String} hook - The name of the hook
* @param {Function} callback - The callback function
*/
export const addCallback = function (hook, callback) {
if (!callback.name) {
2018-01-25 15:03:03 -06:00
// eslint-disable-next-line no-console
console.log(`// Warning! You are adding an unnamed callback to ${hook}. Please use the function foo () {} syntax.`);
}
// if callback array doesn't exist yet, initialize it
if (typeof Callbacks[hook] === "undefined") {
Callbacks[hook] = [];
}
Callbacks[hook].push(callback);
};
/**
* @summary Remove a callback from a hook
* @param {string} hook - The name of the hook
* @param {string} functionName - The name of the function to remove
*/
export const removeCallback = function (hookName, callbackName) {
Callbacks[hookName] = _.reject(Callbacks[hookName], function (callback) {
return callback.name === callbackName;
});
};
/**
* @summary Successively run all of a hook's callbacks on an item
* @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
*/
export const runCallbacks = function () {
// the first argument is the name of the hook or an array of functions
const hook = arguments[0];
2018-05-14 20:24:12 +02:00
// find registered hook
const registeredHook = CallbackHooks.find(({ name }) => name === hook);
// the second argument is the item on which to iterate; wrap it with promise if hook is async
const item = registeredHook && registeredHook.runs === 'async'
? Promise.resolve(arguments[1])
: arguments[1];
// successive arguments are passed to each iteration
const args = Array.prototype.slice.call(arguments).slice(2);
2018-05-10 13:13:49 +02:00
// flag used to detect the callback that initiated the async context
2018-05-14 20:24:12 +02:00
let asyncContext = Utils.isPromise(item);
const callbacks = Array.isArray(hook) ? hook : Callbacks[hook];
if (typeof callbacks !== "undefined" && !!callbacks.length) { // if the hook exists, and contains callbacks to run
2018-05-10 13:13:49 +02:00
const runCallback = (accumulator, callback) => {
debug(`\x1b[32m>> Running callback [${callback.name}] on hook [${hook}]\x1b[0m`);
const newArguments = [accumulator].concat(args);
try {
const result = callback.apply(this, newArguments);
// if callback is only supposed to run once, remove it
if (callback.runOnce) {
removeCallback(hook, callback.name);
}
if (typeof result === 'undefined') {
// if result of current iteration is undefined, don't pass it on
2017-12-27 09:55:06 +09:00
// debug(`// Warning: Sync callback [${callback.name}] in hook [${hook}] didn't return a result!`)
2018-05-10 13:13:49 +02:00
return accumulator;
} else if (Utils.isPromise(result)) {
if (registeredHook && registeredHook.runs === 'sync') {
console.log(`// Warning! Async callback [${callback.name}] executed in sync hook [${hook}], its value is being skipped.`);
return accumulator;
}
}
return result;
} catch (error) {
2018-01-25 15:03:03 -06:00
// eslint-disable-next-line no-console
console.log(`\x1b[31m// error at callback [${callback.name}] in hook [${hook}]\x1b[0m`);
2018-01-25 15:03:03 -06:00
// eslint-disable-next-line no-console
console.log(error);
if (error.break || error.data && error.data.break) {
throw error;
}
// pass the unchanged accumulator to the next iteration of the loop
return accumulator;
}
2018-05-10 13:13:49 +02:00
};
return callbacks.reduce(function (accumulator, callback, index) {
if (Utils.isPromise(accumulator)) {
if (!asyncContext) {
debug(`\x1b[32m>> Started async context in hook [${hook}] by [${callbacks[index-1].name}]\x1b[0m`);
asyncContext = true;
}
return new Promise((resolve, reject) => {
accumulator
.then(result => {
try {
// run this callback once we have the previous value
resolve(runCallback(result, callback));
} catch (error) {
// error will be thrown only for breaking errors, so throw it up in the promise chain
reject(error);
}
})
.catch(reject);
});
} else {
return runCallback(accumulator, callback);
}
}, item);
} else { // else, just return the item unchanged
return item;
}
};
/**
* @summary Successively run all of a hook's callbacks on an item, in async mode (only works on server)
* @param {String} hook - First argument: the name of the hook
* @param {Any} args - Other arguments will be passed to each successive iteration
*/
export const runCallbacksAsync = function () {
2016-01-03 18:18:09 +01: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];
// successive arguments are passed to each iteration
2016-01-02 18:41:45 +01:00
var args = Array.prototype.slice.call(arguments).slice(1);
const callbacks = Array.isArray(hook) ? hook : 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) {
debug(`\x1b[32m>> Running async callback [${callback.name}] on hook [${hook}]\x1b[0m`);
callback.apply(this, args);
});
});
}
2016-01-03 18:18:09 +01:00
2016-12-13 11:32:23 +09:00
};