parallel.js revision ec01268259a4564a2562e1dec5809079af3dda5d
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews* A concurrent parallel processor to help in running several async functions.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews* @module parallel
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater* @main parallel
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsA concurrent parallel processor to help in running several async functions.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews var stack = new Y.Parallel();
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews for (var i = 0; i < 15; i++) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews Y.io('./api/json/' + i, {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews success: stack.add(function() {
e2e4d321999340802f77adaacd19c797d04b4b95Automatic Updater Y.log('Done!');
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews stack.done(function() {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews Y.log('All IO requests complete!');
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews@class Parallel
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews@param {Object} o A config object
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews@param {Object} [o.context=Y] The execution context of the callback to done
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark AndrewsY.Parallel = function(o) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews this.config = o || {};
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * An Array of results from all the callbacks in the stack
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @property results
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @type Array
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * The total items in the stack
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @property total
e2e4d321999340802f77adaacd19c797d04b4b95Automatic Updater * @type Number
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * The number of stacked callbacks executed
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @property finished
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @type Number
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Add a callback to the stack
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @method add
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @param {Function} fn The function callback we are waiting for
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews return function () {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews self.results.push(fn.apply(self.context, arguments));
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * Test to see if all registered items in the stack have completed, if so call the callback to `done`
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @method test
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews test: function () {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews if (self.finished >= self.total && self.callback) {
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews self.callback.call(self.context, self.results, self.data);
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * The method to call when all the items in the stack are complete.
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @method done
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @param {Function} callback The callback to execute on complete
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @param {Mixed} callback.results The results of all the callbacks in the stack
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @param {Mixed} [callback.data] The data given to the `done` method
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * @param {Mixed} data Mixed data to pass to the success callback