1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
//>>built
// wrapped by build app
define("dojox/lang/async", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
dojo.provide("dojox.lang.async");
(function(){
var d = dojo, Deferred = d.Deferred, each = d.forEach, some = d.some,
async = dojox.lang.async, aps = Array.prototype.slice,
opts = Object.prototype.toString;
async.seq = function(x){
// summary:
// Executes functions sequentially. Waits if any of them returns Deferred.
var fs = opts.call(x) == "[object Array]" ? x : arguments;
return function(init){
var x = new Deferred();
each(fs, function(f){ x.addCallback(f); });
x.callback(init);
return x;
};
};
async.par = function(x){
// summary:
// Executes functions in parallel. Waits for all of them to finish.
var fs = opts.call(x) == "[object Array]" ? x : arguments;
return function(init){
var results = new Array(fs.length),
cancel = function(){
each(results, function(v){
if(v instanceof Deferred && v.fired < 0){
v.cancel();
}
});
},
x = new Deferred(cancel),
ready = fs.length;
each(fs, function(f, i){
var x;
try {
x = f(init);
}catch(e){
x = e;
}
results[i] = x;
});
var failed = some(results, function(v){
if(v instanceof Error){
cancel();
x.errback(v);
return true;
}
return false;
});
if(!failed){
each(results, function(v, i){
if(v instanceof Deferred){
v.addCallbacks(
function(v){
results[i] = v;
if(!--ready){
x.callback(results);
}
},
function(v){
cancel();
x.errback(v);
}
);
}else{
--ready;
}
});
}
if(!ready){
x.callback(results);
}
return x;
};
};
async.any = function(x){
// summary:
// Executes functions in parallel. As soon as one of them finishes
// cancels the rest.
var fs = opts.call(x) == "[object Array]" ? x : arguments;
return function(init){
var results = new Array(fs.length), noResult = true;
cancel = function(index){
each(results, function(v, i){
if(i != index && v instanceof Deferred && v.fired < 0){
v.cancel();
}
});
},
x = new Deferred(cancel);
each(fs, function(f, i){
var x;
try {
x = f(init);
}catch(e){
x = e;
}
results[i] = x;
});
var done = some(results, function(v, i){
if(!(v instanceof Deferred)){
cancel(i);
x.callback(v);
return true;
}
return false;
});
if(!done){
each(results, function(v, i){
v.addBoth(
function(v){
if(noResult){
noResult = false;
cancel(i);
x.callback(v);
}
}
);
});
}
return x;
};
};
async.select = function(cond, x){
// summary:
// Executes a condition, waits for it if necessary, and executes
// Nth function from list.
var fs = opts.call(x) == "[object Array]" ? x : aps.call(arguments, 1);
return function(init){
return new Deferred().addCallback(cond).addCallback(function(v){
if(typeof v == "number" && v >= 0 && v < fs.length){
return fs[v](init);
}else{
return new Error("async.select: out of range");
}
}).callback(init);
};
};
async.ifThen = function(cond, ifTrue, ifFalse){
// summary:
// Executes a condition, waits for it if necessary, and executes
// one of two functions.
return function(init){
return new Deferred().addCallback(cond).addCallback(function(v){
return (v ? ifTrue : ifFalse)(init);
}).callback(init);
};
};
async.loop = function(cond, body){
// summary:
// Executes a condition, waits for it if necessary, and executes
// the body, if truthy value was returned.
// Then it repeats the cycle until the condition function returns
// a falsy value.
return function(init){
var x, y = new Deferred(function(){ x.cancel(); });
function ifErr(v){ y.errback(v); }
function loop(v){
if(v){
x.addCallback(body).addCallback(setUp);
}else{
y.callback(v);
}
return v;
}
function setUp(init){
x = new Deferred().
addCallback(cond).
addCallback(loop).
addErrback(ifErr);
x.callback(init);
}
setUp(init);
return y;
};
};
})();
/*
Design decisions:
seq() - behaves like the normal Deferred callback chain.
par() - if error, all pending Deferreds are cancelled and the error is signaled,
otherwise return an array of all results.
any() - just like par() but only one result is returned.
select() - any error is returned, otherwise the selected result is returned.
loop() - any error is returned, otherwise the last result is returned.
*/
});
|