~bcsaller/juju-gui/charmFind

« back to all changes in this revision

Viewing changes to lib/yui/build/parallel/parallel-debug.js

  • Committer: kapil.foss at gmail
  • Date: 2012-07-13 18:45:59 UTC
  • Revision ID: kapil.foss@gmail.com-20120713184559-2xl7be17egsrz0c9
reshape

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
YUI 3.5.1 (build 22)
3
 
Copyright 2012 Yahoo! Inc. All rights reserved.
4
 
Licensed under the BSD License.
5
 
http://yuilibrary.com/license/
6
 
*/
7
 
YUI.add('parallel', function(Y) {
8
 
 
9
 
 
10
 
/**
11
 
* A concurrent parallel processor to help in running several async functions.
12
 
* @module parallel
13
 
* @main parallel
14
 
*/
15
 
 
16
 
/**
17
 
A concurrent parallel processor to help in running several async functions.
18
 
 
19
 
    var stack = new Y.Parallel();
20
 
 
21
 
    for (var i = 0; i < 15; i++) {
22
 
        Y.io('./api/json/' + i, {
23
 
            on: {
24
 
                success: stack.add(function() {
25
 
                    Y.log('Done!');
26
 
                })
27
 
            }
28
 
        });
29
 
    }
30
 
 
31
 
    stack.done(function() {
32
 
        Y.log('All IO requests complete!');
33
 
    });
34
 
 
35
 
@class Parallel
36
 
@param {Object} o A config object
37
 
@param {Object} [o.context=Y] The execution context of the callback to done
38
 
 
39
 
 
40
 
*/
41
 
 
42
 
Y.Parallel = function(o) {
43
 
    this.config = o || {};
44
 
    this.results = [];
45
 
    this.context = this.config.context || Y;
46
 
    this.total = 0;
47
 
    this.finished = 0;
48
 
};
49
 
 
50
 
Y.Parallel.prototype = {
51
 
    /**
52
 
    * An Array of results from all the callbacks in the stack
53
 
    * @property results
54
 
    * @type Array
55
 
    */
56
 
 
57
 
    results: null,
58
 
    /**
59
 
    * The total items in the stack
60
 
    * @property total
61
 
    * @type Number
62
 
    */
63
 
    total: null,
64
 
    /**
65
 
    * The number of stacked callbacks executed
66
 
    * @property finished
67
 
    * @type Number
68
 
    */
69
 
    finished: null,
70
 
    /**
71
 
    * Add a callback to the stack
72
 
    * @method add
73
 
    * @param {Function} fn The function callback we are waiting for
74
 
    */
75
 
    add: function (fn) {
76
 
        var self = this;
77
 
        self.total += 1;
78
 
        return function () {
79
 
            self.finished++;
80
 
            self.results.push(fn && fn.apply(self.context, arguments));
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.5.1' ,{requires:['yui-base']});