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

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

Issue 12296011: Version 0.3.7.4 (Closed) Base URL: http://dart.googlecode.com/svn/trunk/
Patch Set: Created 7 years, 10 months 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 part of dart.async; 5 part of dart.async;
6 6
7 deprecatedFutureValue(_FutureImpl future) => 7 deprecatedFutureValue(_FutureImpl future) =>
8 future._isComplete ? future._resultOrListeners : null; 8 future._isComplete ? future._resultOrListeners : null;
9 9
10 10
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 AsyncError asyncError; 105 AsyncError asyncError;
106 if (error is AsyncError) { 106 if (error is AsyncError) {
107 asyncError = error; 107 asyncError = error;
108 } else { 108 } else {
109 asyncError = new AsyncError(error, stackTrace); 109 asyncError = new AsyncError(error, stackTrace);
110 } 110 }
111 _setError(asyncError); 111 _setError(asyncError);
112 } 112 }
113 113
114 factory _FutureImpl.wait(Iterable<Future> futures) { 114 factory _FutureImpl.wait(Iterable<Future> futures) {
115 // TODO(ajohnsen): can we do better wrt. the generic type T? 115 Completer completer;
116 if (futures.isEmpty) { 116 // List collecting values from the futures.
117 return new Future<List>.immediate(const []); 117 // Set to null if an error occurs.
118 List values;
119 void handleError(error) {
120 if (values != null) {
121 values = null;
122 completer.completeError(error.error, error.stackTrace);
123 }
118 } 124 }
119
120 Completer completer = new Completer<List>();
121 int remaining = futures.length;
122 List values = new List.fixedLength(futures.length);
123
124 // As each future completes, put its value into the corresponding 125 // As each future completes, put its value into the corresponding
125 // position in the list of values. 126 // position in the list of values.
126 int i = 0; 127 int remaining = 0;
127 bool completed = false;
128 for (Future future in futures) { 128 for (Future future in futures) {
129 int pos = i++; 129 int pos = remaining++;
130 future.then((Object value) { 130 future.catchError(handleError).then((Object value) {
131 if (values == null) return null;
131 values[pos] = value; 132 values[pos] = value;
132 if (--remaining == 0) { 133 remaining--;
134 if (remaining == 0) {
133 completer.complete(values); 135 completer.complete(values);
134 } 136 }
135 }).catchError((error) {
136 if (!completed) completer.completeError(error.error, error.stackTrace);
137 completed = true;
138 }); 137 });
139 } 138 }
140 139 if (remaining == 0) {
140 return new Future.immediate(const []);
141 }
142 values = new List.fixedLength(remaining);
143 completer = new Completer<List>();
141 return completer.future; 144 return completer.future;
142 } 145 }
143 146
144 Future then(f(T value), { onError(AsyncError error) }) { 147 Future then(f(T value), { onError(AsyncError error) }) {
145 if (!_isComplete) { 148 if (!_isComplete) {
146 if (onError == null) { 149 if (onError == null) {
147 return new _ThenFuture(f).._subscribeTo(this); 150 return new _ThenFuture(f).._subscribeTo(this);
148 } 151 }
149 return new _SubscribeFuture(f, onError).._subscribeTo(this); 152 return new _SubscribeFuture(f, onError).._subscribeTo(this);
150 } 153 }
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
521 Future catchError(function(AsyncError error), {bool test(var error)}) { 524 Future catchError(function(AsyncError error), {bool test(var error)}) {
522 return _future.catchError(function, test: test); 525 return _future.catchError(function, test: test);
523 } 526 }
524 527
525 Future<T> whenComplete(action()) { 528 Future<T> whenComplete(action()) {
526 return _future.whenComplete(action); 529 return _future.whenComplete(action);
527 } 530 }
528 531
529 Stream<T> asStream() => new Stream.fromFuture(_future); 532 Stream<T> asStream() => new Stream.fromFuture(_future);
530 } 533 }
OLDNEW
« no previous file with comments | « dart/sdk/lib/_internal/compiler/implementation/lib/string_helper.dart ('k') | dart/sdk/lib/async/stream.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698