OLD | NEW |
1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
2 // Dart core library. | 2 // Dart core library. |
3 | 3 |
4 class FutureImpl<T> implements Future<T> { | 4 class FutureImpl<T> implements Future<T> { |
5 | 5 |
6 bool _isComplete = false; | 6 bool _isComplete = false; |
7 | 7 |
8 /** | 8 /** |
9 * Value that was provided to this Future by the Completer | 9 * Value that was provided to this Future by the Completer |
10 */ | 10 */ |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 throw new IllegalArgumentException(null); | 162 throw new IllegalArgumentException(null); |
163 } | 163 } |
164 if (_isComplete) { | 164 if (_isComplete) { |
165 throw new FutureAlreadyCompleteException(); | 165 throw new FutureAlreadyCompleteException(); |
166 } | 166 } |
167 _exception = exception; | 167 _exception = exception; |
168 _stackTrace = stackTrace; | 168 _stackTrace = stackTrace; |
169 _complete(); | 169 _complete(); |
170 } | 170 } |
171 | 171 |
172 Future transform(Function transformation) { | 172 Future transform(Function transformation, [onException(Object exception)]) { |
173 final completer = new Completer(); | 173 final completer = new Completer(); |
174 handleException((e) { | 174 |
175 completer.completeException(e, this.stackTrace); | 175 transformException(ex, stackTrace) { |
| 176 var result = false; |
| 177 if (onException != null) { |
| 178 try { |
| 179 result = onException(ex); |
| 180 } catch (innerException) { |
| 181 ex = innerException; |
| 182 } |
| 183 } |
| 184 |
| 185 if (result != true) { |
| 186 completer.completeException(ex, stackTrace); |
| 187 } |
| 188 } |
| 189 |
| 190 handleException((ex) { |
| 191 transformException(ex, this.stackTrace); |
176 return true; | 192 return true; |
177 }); | 193 }); |
| 194 |
178 then((v) { | 195 then((v) { |
179 var transformed = null; | 196 var transformed = null; |
180 try { | 197 try { |
181 transformed = transformation(v); | 198 transformed = transformation(v); |
182 } catch (ex, stackTrace) { | 199 } catch (ex, stackTrace) { |
183 completer.completeException(ex, stackTrace); | 200 transformException(ex, stackTrace); |
184 return; | 201 return; |
185 } | 202 } |
186 completer.complete(transformed); | 203 completer.complete(transformed); |
187 }); | 204 }); |
| 205 |
188 return completer.future; | 206 return completer.future; |
189 } | 207 } |
190 | 208 |
191 Future chain(Function transformation) { | 209 Future chain(Function transformation) { |
192 final completer = new Completer(); | 210 final completer = new Completer(); |
193 handleException((e) { | 211 handleException((e) { |
194 completer.completeException(e, this.stackTrace); | 212 completer.completeException(e, this.stackTrace); |
195 return true; | 213 return true; |
196 }); | 214 }); |
197 then((v) { | 215 then((v) { |
(...skipping 25 matching lines...) Expand all Loading... |
223 } | 241 } |
224 | 242 |
225 void complete(T value) { | 243 void complete(T value) { |
226 _futureImpl._setValue(value); | 244 _futureImpl._setValue(value); |
227 } | 245 } |
228 | 246 |
229 void completeException(Object exception, [Object stackTrace]) { | 247 void completeException(Object exception, [Object stackTrace]) { |
230 _futureImpl._setException(exception, stackTrace); | 248 _futureImpl._setException(exception, stackTrace); |
231 } | 249 } |
232 } | 250 } |
OLD | NEW |