Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(543)

Side by Side Diff: sdk/lib/async/future.dart

Issue 815773002: Add cleanUp function to Future.wait. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of dart.async; 5 part of dart.async;
6 6
7 /** 7 /**
8 * An object representing a delayed computation. 8 * An object representing a delayed computation.
9 * 9 *
10 * A [Future] is used to represent a potential value, or error, 10 * A [Future] is used to represent a potential value, or error,
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
226 new Timer(duration, () { 226 new Timer(duration, () {
227 try { 227 try {
228 result._complete(computation == null ? null : computation()); 228 result._complete(computation == null ? null : computation());
229 } catch (e, s) { 229 } catch (e, s) {
230 _completeWithErrorCallback(result, e, s); 230 _completeWithErrorCallback(result, e, s);
231 } 231 }
232 }); 232 });
233 return result; 233 return result;
234 } 234 }
235 235
236 /** Helper function that moves try/catch out of the main wait function. */
237 static void _safeCleanUp(var value, void cleanUp(value)) {
238 try {
239 cleanUp(value);
240 } on Object {
241 // Swallow any error. We are already reporting an earlier error.
242 }
243 }
244
236 /** 245 /**
237 * Wait for all the given futures to complete and collect their values. 246 * Wait for all the given futures to complete and collect their values.
238 * 247 *
239 * Returns a future which will complete once all the futures in a list are 248 * Returns a future which will complete once all the futures in a list are
240 * complete. If any of the futures in the list completes with an error, 249 * complete. If any of the futures in the list completes with an error,
241 * the resulting future also completes with an error. Otherwise the value 250 * the resulting future also completes with an error. Otherwise the value
242 * of the returned future will be a list of all the values that were produced. 251 * of the returned future will be a list of all the values that were
252 * produced.
243 * 253 *
244 * If `eagerError` is true, the future completes with an error immediately on 254 * If `eagerError` is true, the future completes with an error immediately on
245 * the first error from one of the futures. Otherwise all futures must 255 * the first error from one of the futures. Otherwise all futures must
246 * complete before the returned future is completed (still with the first 256 * complete before the returned future is completed (still with the first
247 * error to occur, the remaining errors are silently dropped). 257 * error to occur, the remaining errors are silently dropped).
258 *
259 * If [cleanUp] is provided, in the case of an error, any non-null result of
kevmoo 2014/12/18 23:17:50 This could be a bit more clear. ...in case of an
Lasse Reichstein Nielsen 2014/12/19 06:54:11 Any futures completing successfully after the firs
260 * a successful future is passed to `cleanUp`, which can then release any
261 * resources that the successful operation allocated.
248 */ 262 */
249 static Future<List> wait(Iterable<Future> futures, {bool eagerError: false}) { 263 static Future<List> wait(Iterable<Future> futures,
264 {bool eagerError: false,
265 void cleanUp(successValue)}) {
250 final _Future<List> result = new _Future<List>(); 266 final _Future<List> result = new _Future<List>();
251 List values; // Collects the values. Set to null on error. 267 List values; // Collects the values. Set to null on error.
252 int remaining = 0; // How many futures are we waiting for. 268 int remaining = 0; // How many futures are we waiting for.
253 var error; // The first error from a future. 269 var error; // The first error from a future.
254 StackTrace stackTrace; // The stackTrace that came with the error. 270 StackTrace stackTrace; // The stackTrace that came with the error.
255 271
256 // Handle an error from any of the futures. 272 // Handle an error from any of the futures.
257 handleError(theError, theStackTrace) { 273 void handleError(theError, theStackTrace) {
258 final bool isFirstError = (values != null);
259 values = null;
260 remaining--; 274 remaining--;
261 if (isFirstError) { 275 if (values != null) {
276 if (cleanUp != null) {
277 for (var value in values) {
278 if (value != null) _safeCleanUp(value, cleanUp);
279 }
280 }
281 values = null;
262 if (remaining == 0 || eagerError) { 282 if (remaining == 0 || eagerError) {
263 result._completeError(theError, theStackTrace); 283 result._completeError(theError, theStackTrace);
264 } else { 284 } else {
265 error = theError; 285 error = theError;
266 stackTrace = theStackTrace; 286 stackTrace = theStackTrace;
267 } 287 }
268 } else if (remaining == 0 && !eagerError) { 288 } else if (remaining == 0 && !eagerError) {
269 result._completeError(error, stackTrace); 289 result._completeError(error, stackTrace);
270 } 290 }
271 } 291 }
272 292
273 // As each future completes, put its value into the corresponding 293 // As each future completes, put its value into the corresponding
274 // position in the list of values. 294 // position in the list of values.
275 for (Future future in futures) { 295 for (Future future in futures) {
276 int pos = remaining++; 296 int pos = remaining++;
277 future.then((Object value) { 297 future.then((Object value) {
278 remaining--; 298 remaining--;
279 if (values != null) { 299 if (values != null) {
280 values[pos] = value; 300 values[pos] = value;
281 if (remaining == 0) { 301 if (remaining == 0) {
282 result._completeWithValue(values); 302 result._completeWithValue(values);
283 } 303 }
284 } else if (remaining == 0 && !eagerError) { 304 } else {
285 result._completeError(error, stackTrace); 305 if (cleanUp != null && value != null) {
306 _safeCleanUp(value, cleanUp);
307 }
308 if (remaining == 0 && !eagerError) {
309 result._completeError(error, stackTrace);
310 }
286 } 311 }
287 }, onError: handleError); 312 }, onError: handleError);
288 } 313 }
289 if (remaining == 0) { 314 if (remaining == 0) {
290 return new Future.value(const []); 315 return new Future.value(const []);
291 } 316 }
292 values = new List(remaining); 317 values = new List(remaining);
293 return result; 318 return result;
294 } 319 }
295 320
(...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after
697 if (replacement != null) { 722 if (replacement != null) {
698 error = _nonNullError(replacement.error); 723 error = _nonNullError(replacement.error);
699 stackTrace = replacement.stackTrace; 724 stackTrace = replacement.stackTrace;
700 } 725 }
701 result._completeError(error, stackTrace); 726 result._completeError(error, stackTrace);
702 } 727 }
703 728
704 /** Helper function that converts `null` to a [NullThrownError]. */ 729 /** Helper function that converts `null` to a [NullThrownError]. */
705 Object _nonNullError(Object error) => 730 Object _nonNullError(Object error) =>
706 (error != null) ? error : new NullThrownError(); 731 (error != null) ? error : new NullThrownError();
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698