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