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

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

Issue 11414069: Make mappedBy lazy. (Closed) Base URL: https://dart.googlecode.com/svn/experimental/lib_v2/dart
Patch Set: Reupload due to error. Created 8 years, 1 month 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
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 /** 5 /**
6 * A [Future] is used to obtain a value sometime in the future. Receivers of a 6 * A [Future] is used to obtain a value sometime in the future. Receivers of a
7 * [Future] can obtain the value by passing a callback to [then]. For example: 7 * [Future] can obtain the value by passing a callback to [then]. For example:
8 * 8 *
9 * Future<int> future = getFutureFromSomewhere(); 9 * Future<int> future = getFutureFromSomewhere();
10 * future.then((value) { 10 * future.then((value) {
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 * [Futures] holds additional utility functions that operate on [Future]s (for 220 * [Futures] holds additional utility functions that operate on [Future]s (for
221 * example, waiting for a collection of Futures to complete). 221 * example, waiting for a collection of Futures to complete).
222 */ 222 */
223 class Futures { 223 class Futures {
224 /** 224 /**
225 * Returns a future which will complete once all the futures in a list are 225 * Returns a future which will complete once all the futures in a list are
226 * complete. If any of the futures in the list completes with an exception, 226 * complete. If any of the futures in the list completes with an exception,
227 * the resulting future also completes with an exception. (The value of the 227 * the resulting future also completes with an exception. (The value of the
228 * returned future will be a list of all the values that were produced.) 228 * returned future will be a list of all the values that were produced.)
229 */ 229 */
230 static Future<List> wait(List<Future> futures) { 230 static Future<List> wait(Iterable<Future> futures) {
231 if (futures.isEmpty) { 231 if (futures.isEmpty) {
232 return new Future<List>.immediate(const []); 232 return new Future<List>.immediate(const []);
233 } 233 }
234 234
235 Completer completer = new Completer<List>(); 235 Completer completer = new Completer<List>();
236 Future<List> result = completer.future; 236 Future<List> result = completer.future;
237 int remaining = futures.length; 237 int remaining = 0;
238 List values = new List(futures.length); 238 int seen = 0;
239 bool initializing = true;
240 List values = [];
239 241
240 // As each future completes, put its value into the corresponding 242 // As each future completes, put its value into the corresponding
241 // position in the list of values. 243 // position in the list of values.
242 for (int i = 0; i < futures.length; i++) { 244 for (Future future in futures) {
243 // TODO(mattsh) - remove this after bug 245 int pos = seen;
244 // http://code.google.com/p/dart/issues/detail?id=333 is fixed. 246 remaining++;
245 int pos = i; 247 seen++;
246 Future future = futures[pos]; 248 values.length = seen;
247 future.then((Object value) { 249 future.then((Object value) {
248 values[pos] = value; 250 values[pos] = value;
249 if (--remaining == 0 && !result.isComplete) { 251 if (--remaining == 0 && !result.isComplete && !initializing) {
250 completer.complete(values); 252 completer.complete(values);
251 } 253 }
252 }); 254 });
253 future.handleException((exception) { 255 future.handleException((exception) {
254 if (!result.isComplete) { 256 if (!result.isComplete) {
255 completer.completeException(exception, future.stackTrace); 257 completer.completeException(exception, future.stackTrace);
256 } 258 }
257 return true; 259 return true;
258 }); 260 });
259 } 261 }
262 initializing = false;
263 if (remaining == 0 && !result.isComplete) {
264 // All futures already had their values set.
265 completer.complete(values);
266 }
260 return result; 267 return result;
261 } 268 }
262 } 269 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698