parallel.js revision e42975a1b0ef7ebbf876cdf8dc626f86c3fc0c9a
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley* A concurrent parallel processor to help in running several async functions.
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson* @module parallel
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson* @main parallel
16803617e47c83272013e45ba8eb83a3b11983edAndreas GustafssonA concurrent parallel processor to help in running several async functions.
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson var stack = new Y.Parallel();
fd9b6f253eac9dae2e1ad19d49aaa922d5d4f274Mark Andrews for (var i = 0; i < 15; i++) {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.io('./api/json/' + i, {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson success: stack.add(function() {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.log('Done!');
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson stack.done(function() {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.log('All IO requests complete!');
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson@class Parallel
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson@param {Object} o A config object
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley@param {Object} [o.context=Y] The execution context of the callback to done
16803617e47c83272013e45ba8eb83a3b11983edAndreas GustafssonY.Parallel = function(o) {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.config = o || {};
40d01ce8f3a1889f5799d9b22b26d5398fa75a1bBob Halley * An Array of results from all the callbacks in the stack
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property results
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Array
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * The total items in the stack
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property total
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Number
bcfb2cead57dcc6b678abbf0161c1cab989d6de1Mark Andrews * The number of stacked callbacks executed
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property finished
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Number
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews * Add a callback to the stack
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews * @method add
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews * @param {Function} fn The function callback we are waiting for
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews return function () {
2fff8b8280779a25fbdb891b2d3d9b435d2084f0Tatuya JINMEI 神明達哉 self.results.push(fn && fn.apply(self.context, arguments));
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews * Test to see if all registered items in the stack have completed, if so call the callback to `done`
ffc65cc90db78a67171e3d91f63f2e92d09d2d38Evan Hunt * @method test
767c53c304b86460d72eeec7d3304172cdd904bdEvan Hunt test: function () {
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews if (self.finished >= self.total && self.callback) {
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews self.callback.call(self.context, self.results, self.data);
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * The method to call when all the items in the stack are complete.
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * @method done
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * @param {Function} callback The callback to execute on complete
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * @param {Mixed} callback.results The results of all the callbacks in the stack
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * @param {Mixed} [callback.data] The data given to the `done` method
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews * @param {Mixed} data Mixed data to pass to the success callback