2150 |
mathias |
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 |
var index = 0;
|
|
|
46 |
|
|
|
47 |
dojo.forEach(this.list, function(d, index) {
|
|
|
48 |
d.addCallback(this, function(r) { this._cbDeferred(index, true, r); return r; });
|
|
|
49 |
d.addErrback(this, function(r) { this._cbDeferred(index, false, r); return r; });
|
|
|
50 |
index++;
|
|
|
51 |
},this);
|
|
|
52 |
},
|
|
|
53 |
|
|
|
54 |
_cbDeferred: function (index, succeeded, result) {
|
|
|
55 |
// summary:
|
|
|
56 |
// The DeferredLists' callback handler
|
|
|
57 |
|
|
|
58 |
this.resultList[index] = [succeeded, result]; this.finishedCount += 1;
|
|
|
59 |
if (this.fired !== 0) {
|
|
|
60 |
if (succeeded && this.fireOnOneCallback) {
|
|
|
61 |
this.callback([index, result]);
|
|
|
62 |
} else if (!succeeded && this.fireOnOneErrback) {
|
|
|
63 |
this.errback(result);
|
|
|
64 |
} else if (this.finishedCount == this.list.length) {
|
|
|
65 |
this.callback(this.resultList);
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
if (!succeeded && this.consumeErrors) {
|
|
|
69 |
result = null;
|
|
|
70 |
}
|
|
|
71 |
return result;
|
|
|
72 |
},
|
|
|
73 |
|
|
|
74 |
gatherResults: function (deferredList) {
|
|
|
75 |
// summary:
|
|
|
76 |
// Gathers the results of the deferreds for packaging
|
|
|
77 |
// as the parameters to the Deferred Lists' callback
|
|
|
78 |
|
|
|
79 |
var d = new dojo.DeferedList(deferredList, false, true, false);
|
|
|
80 |
d.addCallback(function (results) {
|
|
|
81 |
var ret = [];
|
|
|
82 |
for (var i = 0; i < results.length; i++) {
|
|
|
83 |
ret.push(results[i][1]);
|
|
|
84 |
}
|
|
|
85 |
return ret;
|
|
|
86 |
});
|
|
|
87 |
return d;
|
|
|
88 |
}
|
|
|
89 |
});
|
|
|
90 |
|
|
|
91 |
}
|