|
602
|
1 |
YUI.add('parallel', function (Y, NAME) { |
|
|
2 |
|
|
|
3 |
|
|
|
4 |
/** |
|
|
5 |
* A concurrent parallel processor to help in running several async functions. |
|
|
6 |
* @module parallel |
|
|
7 |
* @main parallel |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
/** |
|
|
11 |
A concurrent parallel processor to help in running several async functions. |
|
|
12 |
|
|
|
13 |
var stack = new Y.Parallel(); |
|
|
14 |
|
|
|
15 |
for (var i = 0; i < 15; i++) { |
|
|
16 |
Y.io('./api/json/' + i, { |
|
|
17 |
on: { |
|
|
18 |
success: stack.add(function() { |
|
|
19 |
Y.log('Done!'); |
|
|
20 |
}) |
|
|
21 |
} |
|
|
22 |
}); |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
stack.done(function() { |
|
|
26 |
Y.log('All IO requests complete!'); |
|
|
27 |
}); |
|
|
28 |
|
|
|
29 |
@class Parallel |
|
|
30 |
@param {Object} o A config object |
|
|
31 |
@param {Object} [o.context=Y] The execution context of the callback to done |
|
|
32 |
|
|
|
33 |
|
|
|
34 |
*/ |
|
|
35 |
|
|
|
36 |
Y.Parallel = function(o) { |
|
|
37 |
this.config = o || {}; |
|
|
38 |
this.results = []; |
|
|
39 |
this.context = this.config.context || Y; |
|
|
40 |
this.total = 0; |
|
|
41 |
this.finished = 0; |
|
|
42 |
}; |
|
|
43 |
|
|
|
44 |
Y.Parallel.prototype = { |
|
|
45 |
/** |
|
|
46 |
* An Array of results from all the callbacks in the stack |
|
|
47 |
* @property results |
|
|
48 |
* @type Array |
|
|
49 |
*/ |
|
|
50 |
|
|
|
51 |
results: null, |
|
|
52 |
/** |
|
|
53 |
* The total items in the stack |
|
|
54 |
* @property total |
|
|
55 |
* @type Number |
|
|
56 |
*/ |
|
|
57 |
total: null, |
|
|
58 |
/** |
|
|
59 |
* The number of stacked callbacks executed |
|
|
60 |
* @property finished |
|
|
61 |
* @type Number |
|
|
62 |
*/ |
|
|
63 |
finished: null, |
|
|
64 |
/** |
|
|
65 |
* Add a callback to the stack |
|
|
66 |
* @method add |
|
|
67 |
* @param {Function} fn The function callback we are waiting for |
|
|
68 |
*/ |
|
|
69 |
add: function (fn) { |
|
|
70 |
var self = this, |
|
|
71 |
index = self.total; |
|
|
72 |
|
|
|
73 |
self.total += 1; |
|
|
74 |
|
|
|
75 |
return function () { |
|
|
76 |
self.finished++; |
|
|
77 |
self.results[index] = (fn && fn.apply(self.context, arguments)) || |
|
|
78 |
(arguments.length === 1 ? arguments[0] : Y.Array(arguments)); |
|
|
79 |
|
|
|
80 |
self.test(); |
|
|
81 |
}; |
|
|
82 |
}, |
|
|
83 |
/** |
|
|
84 |
* Test to see if all registered items in the stack have completed, if so call the callback to `done` |
|
|
85 |
* @method test |
|
|
86 |
*/ |
|
|
87 |
test: function () { |
|
|
88 |
var self = this; |
|
|
89 |
if (self.finished >= self.total && self.callback) { |
|
|
90 |
self.callback.call(self.context, self.results, self.data); |
|
|
91 |
} |
|
|
92 |
}, |
|
|
93 |
/** |
|
|
94 |
* The method to call when all the items in the stack are complete. |
|
|
95 |
* @method done |
|
|
96 |
* @param {Function} callback The callback to execute on complete |
|
|
97 |
* @param {Mixed} callback.results The results of all the callbacks in the stack |
|
|
98 |
* @param {Mixed} [callback.data] The data given to the `done` method |
|
|
99 |
* @param {Mixed} data Mixed data to pass to the success callback |
|
|
100 |
*/ |
|
|
101 |
done: function (callback, data) { |
|
|
102 |
this.callback = callback; |
|
|
103 |
this.data = data; |
|
|
104 |
this.test(); |
|
|
105 |
} |
|
|
106 |
}; |
|
|
107 |
|
|
|
108 |
|
|
|
109 |
}, '@VERSION@', {"requires": ["yui-base"]}); |