NOBUG: Fixed file access permissions
[moodle.git] / lib / yuilib / 3.13.0 / parallel / parallel.js
blob895d9b7bf8346554d64400bd902924d4603315e9
1 /*
2 YUI 3.13.0 (build 508226d)
3 Copyright 2013 Yahoo! Inc. All rights reserved.
4 Licensed under the BSD License.
5 http://yuilibrary.com/license/
6 */
8 YUI.add('parallel', function (Y, NAME) {
11 /**
12 * A concurrent parallel processor to help in running several async functions.
13 * @module parallel
14 * @main parallel
17 /**
18 A concurrent parallel processor to help in running several async functions.
20     var stack = new Y.Parallel();
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     }
31     stack.done(function() {
32     });
34 @class Parallel
35 @param {Object} o A config object
36 @param {Object} [o.context=Y] The execution context of the callback to done
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;
49 Y.Parallel.prototype = {
50     /**
51     * An Array of results from all the callbacks in the stack
52     * @property results
53     * @type Array
54     */
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;
78         self.total += 1;
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));
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     }
114 }, '3.13.0', {"requires": ["yui-base"]});