~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/parallel/parallel-debug.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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