Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
                })
20
            }
21
        });
22
    }
23
 
24
    stack.done(function() {
25
    });
26
 
27
@class Parallel
28
@param {Object} o A config object
29
@param {Object} [o.context=Y] The execution context of the callback to done
30
 
31
 
32
*/
33
 
34
Y.Parallel = function(o) {
35
    this.config = o || {};
36
    this.results = [];
37
    this.context = this.config.context || Y;
38
    this.total = 0;
39
    this.finished = 0;
40
};
41
 
42
Y.Parallel.prototype = {
43
    /**
44
    * An Array of results from all the callbacks in the stack
45
    * @property results
46
    * @type Array
47
    */
48
 
49
    results: null,
50
    /**
51
    * The total items in the stack
52
    * @property total
53
    * @type Number
54
    */
55
    total: null,
56
    /**
57
    * The number of stacked callbacks executed
58
    * @property finished
59
    * @type Number
60
    */
61
    finished: null,
62
    /**
63
    * Add a callback to the stack
64
    * @method add
65
    * @param {Function} fn The function callback we are waiting for
66
    */
67
    add: function (fn) {
68
        var self = this,
69
            index = self.total;
70
 
71
        self.total += 1;
72
 
73
        return function () {
74
            self.finished++;
75
            self.results[index] = (fn && fn.apply(self.context, arguments)) ||
76
                (arguments.length === 1 ? arguments[0] : Y.Array(arguments));
77
 
78
            self.test();
79
        };
80
    },
81
    /**
82
    * Test to see if all registered items in the stack have completed, if so call the callback to `done`
83
    * @method test
84
    */
85
    test: function () {
86
        var self = this;
87
        if (self.finished >= self.total && self.callback) {
88
            self.callback.call(self.context, self.results, self.data);
89
        }
90
    },
91
    /**
92
    * The method to call when all the items in the stack are complete.
93
    * @method done
94
    * @param {Function} callback The callback to execute on complete
95
    * @param {Mixed} callback.results The results of all the callbacks in the stack
96
    * @param {Mixed} [callback.data] The data given to the `done` method
97
    * @param {Mixed} data Mixed data to pass to the success callback
98
    */
99
    done: function (callback, data) {
100
        this.callback = callback;
101
        this.data = data;
102
        this.test();
103
    }
104
};
105
 
106
 
107
}, '3.18.1', {"requires": ["yui-base"]});