parallel.js revision e42975a1b0ef7ebbf876cdf8dc626f86c3fc0c9a
36983956d7c3d9e294903eeda29548f67ac17daeBob Halley
40d01ce8f3a1889f5799d9b22b26d5398fa75a1bBob Halley/**
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley* A concurrent parallel processor to help in running several async functions.
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson* @module parallel
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson* @main parallel
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson*/
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson/**
16803617e47c83272013e45ba8eb83a3b11983edAndreas GustafssonA concurrent parallel processor to help in running several async functions.
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson var stack = new Y.Parallel();
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
fd9b6f253eac9dae2e1ad19d49aaa922d5d4f274Mark Andrews for (var i = 0; i < 15; i++) {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.io('./api/json/' + i, {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson on: {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson success: stack.add(function() {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.log('Done!');
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson })
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson }
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson });
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson }
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson stack.done(function() {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson Y.log('All IO requests complete!');
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson });
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
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
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley
b90dd6c0a9df584619d3c47be7c9417f55d5ccf6Bob Halley*/
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson
16803617e47c83272013e45ba8eb83a3b11983edAndreas GustafssonY.Parallel = function(o) {
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.config = o || {};
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.results = [];
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.context = this.config.context || Y;
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.total = 0;
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson this.finished = 0;
16803617e47c83272013e45ba8eb83a3b11983edAndreas Gustafsson};
70680fa51b0147c726b939b72b2420249429756aBob Halley
70680fa51b0147c726b939b72b2420249429756aBob HalleyY.Parallel.prototype = {
276a77c22af98c78403883b16a82646a0d5b29abPaul Vixie /**
40d01ce8f3a1889f5799d9b22b26d5398fa75a1bBob Halley * An Array of results from all the callbacks in the stack
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property results
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Array
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt */
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt results: null,
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt /**
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * The total items in the stack
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property total
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Number
bcfb2cead57dcc6b678abbf0161c1cab989d6de1Mark Andrews */
bcfb2cead57dcc6b678abbf0161c1cab989d6de1Mark Andrews total: null,
bcfb2cead57dcc6b678abbf0161c1cab989d6de1Mark Andrews /**
bcfb2cead57dcc6b678abbf0161c1cab989d6de1Mark Andrews * The number of stacked callbacks executed
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @property finished
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt * @type Number
6cdaeb94d4d12b72b919f3c7099f7c47c172b59bEvan Hunt */
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews finished: null,
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews /**
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 */
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews add: function (fn) {
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews var self = this;
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews self.total += 1;
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews return function () {
5a8bebe00df211d4fdac3edc36cf35e1d5af42e0Mark Andrews self.finished++;
2fff8b8280779a25fbdb891b2d3d9b435d2084f0Tatuya JINMEI 神明達哉 self.results.push(fn && fn.apply(self.context, arguments));
2fff8b8280779a25fbdb891b2d3d9b435d2084f0Tatuya JINMEI 神明達哉 self.test();
19bcb91965916ed8f0a47da2284ddaecce70bc69Evan Hunt };
19bcb91965916ed8f0a47da2284ddaecce70bc69Evan Hunt },
9c114f36dab6bc6fc024b46680cfdd246efc880cEvan Hunt /**
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
ffc65cc90db78a67171e3d91f63f2e92d09d2d38Evan Hunt */
767c53c304b86460d72eeec7d3304172cdd904bdEvan Hunt test: function () {
767c53c304b86460d72eeec7d3304172cdd904bdEvan Hunt var self = this;
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews if (self.finished >= self.total && self.callback) {
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews self.callback.call(self.context, self.results, self.data);
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews }
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews },
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews /**
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
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews */
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews done: function (callback, data) {
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews this.callback = callback;
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews this.data = data;
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews this.test();
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews }
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews};
309a3b5808b3e7666d219665c28768e5c0997f14Mark Andrews