root / trunk / web / dojo / dojox / lang / async.js @ 12
History | View | Annotate | Download (2.59 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.lang.async"]){ |
9 |
dojo._hasResource["dojox.lang.async"]=true; |
10 |
dojo.provide("dojox.lang.async");
|
11 |
(function(){
|
12 |
var d=dojo,_1=d.Deferred,_2=d.forEach,_3=d.some,_4=dojox.lang.async,_5=Array.prototype.slice,_6=Object.prototype.toString;
|
13 |
_4.seq=function(x){ |
14 |
var fs=_6.call(x)=="[object Array]"?x:arguments; |
15 |
return function(_7){ |
16 |
var x=new _1(); |
17 |
_2(fs,function(f){
|
18 |
x.addCallback(f); |
19 |
}); |
20 |
x.callback(_7); |
21 |
return x;
|
22 |
}; |
23 |
}; |
24 |
_4.par=function(x){ |
25 |
var fs=_6.call(x)=="[object Array]"?x:arguments; |
26 |
return function(_8){ |
27 |
var _9=new Array(fs.length),_a=function(){ |
28 |
_2(_9,function(v){
|
29 |
if(v instanceof _1&&v.fired<0){ |
30 |
v.cancel(); |
31 |
} |
32 |
}); |
33 |
},x=new _1(_a),_b=fs.length;
|
34 |
_2(fs,function(f,i){
|
35 |
var x;
|
36 |
try{
|
37 |
x=f(_8); |
38 |
} |
39 |
catch(e){
|
40 |
x=e; |
41 |
} |
42 |
_9[i]=x; |
43 |
}); |
44 |
var _c=_3(_9,function(v){ |
45 |
if(v instanceof Error){ |
46 |
_a(); |
47 |
x.errback(v); |
48 |
return true; |
49 |
} |
50 |
return false; |
51 |
}); |
52 |
if(!_c){
|
53 |
_2(_9,function(v,i){
|
54 |
if(v instanceof _1){ |
55 |
v.addCallbacks(function(v){
|
56 |
_9[i]=v; |
57 |
if(!--_b){
|
58 |
x.callback(_9); |
59 |
} |
60 |
},function(v){
|
61 |
_a(); |
62 |
x.errback(v); |
63 |
}); |
64 |
}else{
|
65 |
--_b; |
66 |
} |
67 |
}); |
68 |
} |
69 |
if(!_b){
|
70 |
x.callback(_9); |
71 |
} |
72 |
return x;
|
73 |
}; |
74 |
}; |
75 |
_4.any=function(x){ |
76 |
var fs=_6.call(x)=="[object Array]"?x:arguments; |
77 |
return function(_d){ |
78 |
var _e=new Array(fs.length),_f=true; |
79 |
cancel=function(_10){ |
80 |
_2(_e,function(v,i){
|
81 |
if(i!=_10&&v instanceof _1&&v.fired<0){ |
82 |
v.cancel(); |
83 |
} |
84 |
}); |
85 |
},x=new _1(cancel);
|
86 |
_2(fs,function(f,i){
|
87 |
var x;
|
88 |
try{
|
89 |
x=f(_d); |
90 |
} |
91 |
catch(e){
|
92 |
x=e; |
93 |
} |
94 |
_e[i]=x; |
95 |
}); |
96 |
var _11=_3(_e,function(v,i){ |
97 |
if(!(v instanceof _1)){ |
98 |
cancel(i); |
99 |
x.callback(v); |
100 |
return true; |
101 |
} |
102 |
return false; |
103 |
}); |
104 |
if(!_11){
|
105 |
_2(_e,function(v,i){
|
106 |
v.addBoth(function(v){
|
107 |
if(_f){
|
108 |
_f=false;
|
109 |
cancel(i); |
110 |
x.callback(v); |
111 |
} |
112 |
}); |
113 |
}); |
114 |
} |
115 |
return x;
|
116 |
}; |
117 |
}; |
118 |
_4.select=function(_12,x){ |
119 |
var fs=_6.call(x)=="[object Array]"?x:_5.call(arguments,1); |
120 |
return function(_13){ |
121 |
return new _1().addCallback(_12).addCallback(function(v){ |
122 |
if(typeof v=="number"&&v>=0&&v<fs.length){ |
123 |
return fs[v](_13);
|
124 |
}else{
|
125 |
return new Error("async.select: out of range"); |
126 |
} |
127 |
}).callback(_13); |
128 |
}; |
129 |
}; |
130 |
_4.ifThen=function(_14,_15,_16){ |
131 |
return function(_17){ |
132 |
return new _1().addCallback(_14).addCallback(function(v){ |
133 |
return (v?_15:_16)(_17);
|
134 |
}).callback(_17); |
135 |
}; |
136 |
}; |
137 |
_4.loop=function(_18,_19){ |
138 |
return function(_1a){ |
139 |
var x,y=new _1(function(){ |
140 |
x.cancel(); |
141 |
}); |
142 |
function _1b(v){ |
143 |
y.errback(v); |
144 |
}; |
145 |
function _1c(v){ |
146 |
if(v){
|
147 |
x.addCallback(_19).addCallback(_1d); |
148 |
}else{
|
149 |
y.callback(v); |
150 |
} |
151 |
return v;
|
152 |
}; |
153 |
function _1d(_1e){ |
154 |
x=new _1().addCallback(_18).addCallback(_1c).addErrback(_1b);
|
155 |
x.callback(_1e); |
156 |
}; |
157 |
_1d(_1a); |
158 |
return y;
|
159 |
}; |
160 |
}; |
161 |
})(); |
162 |
} |