]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojo/DeferredList.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojo / DeferredList.js
1 if(!dojo._hasResource["dojo.DeferredList"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.DeferredList"] = true;
3 dojo.provide("dojo.DeferredList");
4 dojo.declare("dojo.DeferredList", dojo.Deferred, {
5         constructor: function(/*Array*/ list, /*Boolean?*/ fireOnOneCallback, /*Boolean?*/ fireOnOneErrback, /*Boolean?*/ consumeErrors, /*Function?*/ canceller){
6                 // summary:
7                 //              Provides event handling for a group of Deferred objects.
8                 // description:
9                 //              DeferredList takes an array of existing deferreds and returns a new deferred of its own
10                 //              this new deferred will typically have its callback fired when all of the deferreds in
11                 //              the given list have fired their own deferreds.  The parameters `fireOnOneCallback` and
12                 //              fireOnOneErrback, will fire before all the deferreds as appropriate
13                 //
14                 //      list:
15                 //              The list of deferreds to be synchronizied with this DeferredList
16                 //      fireOnOneCallback:
17                 //              Will cause the DeferredLists callback to be fired as soon as any
18                 //              of the deferreds in its list have been fired instead of waiting until
19                 //              the entire list has finished
20                 //      fireonOneErrback:
21                 //              Will cause the errback to fire upon any of the deferreds errback
22                 //      canceller:
23                 //              A deferred canceller function, see dojo.Deferred
24                 this.list = list;
25                 this.resultList = new Array(this.list.length);
26
27                 // Deferred init
28                 this.chain = [];
29                 this.id = this._nextId();
30                 this.fired = -1;
31                 this.paused = 0;
32                 this.results = [null, null];
33                 this.canceller = canceller;
34                 this.silentlyCancelled = false;
35
36                 if(this.list.length === 0 && !fireOnOneCallback){
37                         this.callback(this.resultList);
38                 }
39
40                 this.finishedCount = 0;
41                 this.fireOnOneCallback = fireOnOneCallback;
42                 this.fireOnOneErrback = fireOnOneErrback;
43                 this.consumeErrors = consumeErrors;
44
45                 dojo.forEach(this.list, function(d, index){
46                         d.addCallback(this, function(r){ this._cbDeferred(index, true, r); return r; });
47                         d.addErrback(this, function(r){ this._cbDeferred(index, false, r); return r; });
48                 }, this);
49         },
50
51         _cbDeferred: function(index, succeeded, result){
52                 // summary:
53                 //      The DeferredLists' callback handler
54
55                 this.resultList[index] = [succeeded, result]; this.finishedCount += 1;
56                 if(this.fired !== 0){
57                         if(succeeded && this.fireOnOneCallback){
58                                 this.callback([index, result]);
59                         }else if(!succeeded && this.fireOnOneErrback){
60                                 this.errback(result);
61                         }else if(this.finishedCount == this.list.length){
62                                 this.callback(this.resultList);
63                         }
64                 }
65                 if(!succeeded && this.consumeErrors){
66                         result = null;
67                 }
68                 return result;
69         },
70
71         gatherResults: function(deferredList){
72                 // summary:     
73                 //      Gathers the results of the deferreds for packaging
74                 //      as the parameters to the Deferred Lists' callback
75
76                 var d = new dojo.DeferredList(deferredList, false, true, false);
77                 d.addCallback(function(results){
78                         var ret = [];
79                         dojo.forEach(results, function(result){
80                                 ret.push(result[1]);
81                         });
82                         return ret;
83                 });
84                 return d;
85         }
86 });
87
88 }