File:CombinedCallback.js

/**
 * @module Core
 * @namespace springroll
 */
(function()
{

	/**
	 * CombinedCallback is a utility class that creates a function to be passed to multiple
	 * asynchronous functions as a callback, and will call your callback on the last time it
	 * is called.
	 *
	 * @class CombinedCallback
	 */
	var CombinedCallback = {};

	/**
	 * Creates a callback function for use.
	 *
	 * @method create
	 * @static
	 * @param {Function} call The callback to call when everything is complete.
	 * @param {int} [callCount=2] The number of times this function should expect to be called.
	 * @return {Function} The callback to pass to your asynchronous actions. For reuse,
	 *                    this function has a reset() function.
	 */
	CombinedCallback.create = function(call, callCount)
	{
		if (!call) return null;

		if (typeof callCount != "number" || callCount < 1)
			callCount = 2;
		//create a function that can be called multiple times
		var rtn = function()
		{
			if (++rtn.currentCallCount >= callCount)
				call();
		};
		//set some properties on said function to make it reusable
		rtn.currentCallCount = 0;
		rtn.reset = reset;

		return rtn;
	};

	function reset()
	{
		this.currentCallCount = 0;
	}

	namespace('springroll').CombinedCallback = CombinedCallback;
}());