3
Copyright 2012 Yahoo! Inc. All rights reserved.
4
Licensed under the BSD License.
5
http://yuilibrary.com/license/
7
YUI.add('parallel', function(Y) {
11
* A concurrent parallel processor to help in running several async functions.
17
A concurrent parallel processor to help in running several async functions.
19
var stack = new Y.Parallel();
21
for (var i = 0; i < 15; i++) {
22
Y.io('./api/json/' + i, {
24
success: stack.add(function() {
31
stack.done(function() {
32
Y.log('All IO requests complete!');
36
@param {Object} o A config object
37
@param {Object} [o.context=Y] The execution context of the callback to done
42
Y.Parallel = function(o) {
43
this.config = o || {};
45
this.context = this.config.context || Y;
50
Y.Parallel.prototype = {
52
* An Array of results from all the callbacks in the stack
59
* The total items in the stack
65
* The number of stacked callbacks executed
71
* Add a callback to the stack
73
* @param {Function} fn The function callback we are waiting for
80
self.results.push(fn && fn.apply(self.context, arguments));
85
* Test to see if all registered items in the stack have completed, if so call the callback to `done`
90
if (self.finished >= self.total && self.callback) {
91
self.callback.call(self.context, self.results, self.data);
95
* The method to call when all the items in the stack are complete.
97
* @param {Function} callback The callback to execute on complete
98
* @param {Mixed} callback.results The results of all the callbacks in the stack
99
* @param {Mixed} [callback.data] The data given to the `done` method
100
* @param {Mixed} data Mixed data to pass to the success callback
102
done: function (callback, data) {
103
this.callback = callback;
110
}, '3.5.1' ,{requires:['yui-base']});