OLD | NEW |
| (Empty) |
1 part of dart.async; | |
2 abstract class Future<T> {static final _Future _nullFuture = new _Future.immedi
ate(null); | |
3 factory Future(computation()) { | |
4 _Future result = new _Future<T>(); | |
5 Timer.run(() { | |
6 try { | |
7 result._complete(computation()); | |
8 } | |
9 catch (e, s) { | |
10 _completeWithErrorCallback(result, e, s); | |
11 } | |
12 } | |
13 ); | |
14 return DEVC$RT.cast(result, DEVC$RT.type((_Future<dynamic> _) { | |
15 } | |
16 ), DEVC$RT.type((Future<T> _) { | |
17 } | |
18 ), "CompositeCast", """line 123, column 12 of dart:async/future.dart: """, res
ult is Future<T>, false); | |
19 } | |
20 factory Future.microtask(computation()) { | |
21 _Future result = new _Future<T>(); | |
22 scheduleMicrotask(() { | |
23 try { | |
24 result._complete(computation()); | |
25 } | |
26 catch (e, s) { | |
27 _completeWithErrorCallback(result, e, s); | |
28 } | |
29 } | |
30 ); | |
31 return DEVC$RT.cast(result, DEVC$RT.type((_Future<dynamic> _) { | |
32 } | |
33 ), DEVC$RT.type((Future<T> _) { | |
34 } | |
35 ), "CompositeCast", """line 149, column 12 of dart:async/future.dart: """, res
ult is Future<T>, false); | |
36 } | |
37 factory Future.sync(computation()) { | |
38 try { | |
39 var result = computation(); | |
40 return new Future<T>.value(result); | |
41 } | |
42 catch (error, stackTrace) { | |
43 return new Future<T>.error(error, stackTrace); | |
44 } | |
45 } | |
46 factory Future.value([value]) { | |
47 return new _Future<T>.immediate(value); | |
48 } | |
49 factory Future.error(Object error, [StackTrace stackTrace]) { | |
50 error = _nonNullError(error); | |
51 if (!identical(Zone.current, _ROOT_ZONE)) { | |
52 AsyncError replacement = Zone.current.errorCallback(error, stackTrace); | |
53 if (replacement != null) { | |
54 error = _nonNullError(replacement.error); | |
55 stackTrace = replacement.stackTrace; | |
56 } | |
57 } | |
58 return new _Future<T>.immediateError(error, stackTrace); | |
59 } | |
60 factory Future.delayed(Duration duration, [T computation()]) { | |
61 _Future result = new _Future<T>(); | |
62 new Timer(duration, () { | |
63 try { | |
64 result._complete(computation == null ? null : computation()); | |
65 } | |
66 catch (e, s) { | |
67 _completeWithErrorCallback(result, e, s); | |
68 } | |
69 } | |
70 ); | |
71 return DEVC$RT.cast(result, DEVC$RT.type((_Future<dynamic> _) { | |
72 } | |
73 ), DEVC$RT.type((Future<T> _) { | |
74 } | |
75 ), "CompositeCast", """line 233, column 12 of dart:async/future.dart: """, res
ult is Future<T>, false); | |
76 } | |
77 static Future<List> wait(Iterable<Future> futures, { | |
78 bool eagerError : false, void cleanUp(successValue)} | |
79 ) { | |
80 final _Future<List> result = new _Future<List>(); | |
81 List values; | |
82 int remaining = 0; | |
83 var error; | |
84 StackTrace stackTrace; | |
85 void handleError(theError, theStackTrace) { | |
86 remaining--; | |
87 if (values != null) { | |
88 if (cleanUp != null) { | |
89 for (var value in values) { | |
90 if (value != null) { | |
91 new Future.sync(() { | |
92 cleanUp(value); | |
93 } | |
94 ); | |
95 } | |
96 } | |
97 } | |
98 values = null; | |
99 if (remaining == 0 || eagerError) { | |
100 result._completeError(theError, DEVC$RT.cast(theStackTrace, dynamic, Sta
ckTrace, "DynamicCast", """line 280, column 43 of dart:async/future.dart: """, t
heStackTrace is StackTrace, true)); | |
101 } | |
102 else { | |
103 error = theError; | |
104 stackTrace = DEVC$RT.cast(theStackTrace, dynamic, StackTrace, "DynamicC
ast", """line 283, column 24 of dart:async/future.dart: """, theStackTrace is St
ackTrace, true); | |
105 } | |
106 } | |
107 else if (remaining == 0 && !eagerError) { | |
108 result._completeError(error, stackTrace); | |
109 } | |
110 } | |
111 for (Future future in futures) { | |
112 int pos = remaining++; | |
113 future.then((Object value) { | |
114 remaining--; | |
115 if (values != null) { | |
116 values[pos] = value; | |
117 if (remaining == 0) { | |
118 result._completeWithValue(values); | |
119 } | |
120 } | |
121 else { | |
122 if (cleanUp != null && value != null) { | |
123 new Future.sync(() { | |
124 cleanUp(value); | |
125 } | |
126 ); | |
127 } | |
128 if (remaining == 0 && !eagerError) { | |
129 result._completeError(error, stackTrace); | |
130 } | |
131 } | |
132 } | |
133 , onError: handleError); | |
134 } | |
135 if (remaining == 0) { | |
136 return new Future<List<dynamic>>.value(const []); | |
137 } | |
138 values = new List(remaining); | |
139 return result; | |
140 } | |
141 static Future forEach(Iterable input, f(element)) { | |
142 Iterator iterator = input.iterator; | |
143 return doWhile(() { | |
144 if (!iterator.moveNext()) return false; | |
145 return new Future.sync(() => f(iterator.current)).then((_) => true); | |
146 } | |
147 ); | |
148 } | |
149 static Future doWhile(f()) { | |
150 _Future doneSignal = new _Future(); | |
151 var nextIteration; | |
152 nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) { | |
153 if (keepGoing) { | |
154 new Future.sync(f).then(DEVC$RT.cast(nextIteration, dynamic, __CastType8,
"DynamicCast", """line 361, column 33 of dart:async/future.dart: """, nextIterat
ion is __CastType8, true), onError: doneSignal._completeError); | |
155 } | |
156 else { | |
157 doneSignal._complete(null); | |
158 } | |
159 } | |
160 , runGuarded: true); | |
161 nextIteration(true); | |
162 return doneSignal; | |
163 } | |
164 Future then(onValue(T value), { | |
165 Function onError} | |
166 ); | |
167 Future catchError(Function onError, { | |
168 bool test(Object error)} | |
169 ); | |
170 Future<T> whenComplete(action()); | |
171 Stream<T> asStream(); | |
172 Future timeout(Duration timeLimit, { | |
173 onTimeout()} | |
174 ); | |
175 } | |
176 class TimeoutException implements Exception {final String message; | |
177 final Duration duration; | |
178 TimeoutException(this.message, [this.duration]); | |
179 String toString() { | |
180 String result = "TimeoutException"; | |
181 if (duration != null) result = "TimeoutException after $duration"; | |
182 if (message != null) result = "$result: $message"; | |
183 return result; | |
184 } | |
185 } | |
186 abstract class Completer<T> {factory Completer() => new _AsyncCompleter<T>(); | |
187 factory Completer.sync() => new _SyncCompleter<T>(); | |
188 Future<T> get future; | |
189 void complete([value]); | |
190 void completeError(Object error, [StackTrace stackTrace]); | |
191 bool get isCompleted; | |
192 } | |
193 void _completeWithErrorCallback(_Future result, error, stackTrace) { | |
194 AsyncError replacement = Zone.current.errorCallback(error, DEVC$RT.cast(stackTra
ce, dynamic, StackTrace, "DynamicCast", """line 719, column 62 of dart:async/fut
ure.dart: """, stackTrace is StackTrace, true)); | |
195 if (replacement != null) { | |
196 error = _nonNullError(replacement.error); | |
197 stackTrace = replacement.stackTrace; | |
198 } | |
199 result._completeError(error, DEVC$RT.cast(stackTrace, dynamic, StackTrace, "Dyn
amicCast", """line 724, column 32 of dart:async/future.dart: """, stackTrace is
StackTrace, true)); | |
200 } | |
201 Object _nonNullError(Object error) => (error != null) ? error : new NullThrownE
rror(); | |
202 typedef dynamic __CastType8(dynamic __u9); | |
OLD | NEW |