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

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

Issue 14070010: Refactor Future constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 8 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 /** 7 /**
8 * A [Future] represents a delayed computation. It is used to obtain a not-yet 8 * A [Future] represents a delayed computation. It is used to obtain a not-yet
9 * available value, or error, sometime in the future. Receivers of a 9 * available value, or error, sometime in the future. Receivers of a
10 * [Future] can register callbacks that handle the value or error once it is 10 * [Future] can register callbacks that handle the value or error once it is
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
77 * Similar to the synchronous code, the error handler (registered with 77 * Similar to the synchronous code, the error handler (registered with
78 * [catchError]) is handling the errors for exceptions coming from calls to 78 * [catchError]) is handling the errors for exceptions coming from calls to
79 * 'foo', as well as 'bar'. This would not be the case if the error-handler was 79 * 'foo', as well as 'bar'. This would not be the case if the error-handler was
80 * registered at the same time as the value-handler. 80 * registered at the same time as the value-handler.
81 * 81 *
82 * Futures can have more than one callback-pairs registered. Each successor is 82 * Futures can have more than one callback-pairs registered. Each successor is
83 * treated independently and is handled as if it was the only successor. 83 * treated independently and is handled as if it was the only successor.
84 */ 84 */
85 // TODO(floitsch): document chaining. 85 // TODO(floitsch): document chaining.
86 abstract class Future<T> { 86 abstract class Future<T> {
87
87 /** 88 /**
88 * Creates a future containing the result of calling [function]. 89 * Creates a future containing the result of calling [computation]
90 * asynchronously with [runAsync].
89 * 91 *
90 * The result of computing [:function():] is either a returned value or 92 * if the result of executing [computation] throws, the returned future is
91 * a throw. 93 * completed with the error. If a thrown value is an [AsyncError], it is used
92 * 94 * directly, instead of wrapping this error again in another [AsyncError].
93 * If a value is returned, it becomes the result of the created future.
94 *
95 * If calling [function] throws, the created [Future] will be completed
96 * with an async error containing the thrown value and a captured
97 * stacktrace.
98 *
99 * However, if the result of calling [function] is already an asynchronous
100 * result, we treat it specially.
101 * 95 *
102 * If the returned value is itself a [Future], completion of 96 * If the returned value is itself a [Future], completion of
103 * the created future will wait until the returned future completes, 97 * the created future will wait until the returned future completes,
104 * and will then complete with the same result. 98 * and will then complete with the same result.
105 * 99 *
106 * If a thrown value is an [AsyncError], it is used directly as the result 100 * If a value is returned, it becomes the result of the created future.
107 * of the created future.
108 */ 101 */
109 factory Future.of(function()) { 102 factory Future(computation()) {
103 _ThenFuture<dynamic, T> future =
104 new _ThenFuture<dynamic, T>((_) => computation());
Lasse Reichstein Nielsen 2013/04/15 10:27:46 I'd prefer using _FutureImpl directly instead of _
floitsch 2013/04/15 20:30:11 Falls under "optimizations for later" category.
105 runAsync(() => future._sendValue(null));
106 return future;
107 }
108
109 /**
110 * Creates a future containing the result of immediately calling
111 * [computation].
112 *
113 * if the result of executing [computation] throws, the returned future is
114 * completed with the error. If a thrown value is an [AsyncError], it is used
115 * directly, instead of wrapping this error again in another [AsyncError].
116 *
117 * If the returned value is itself a [Future], completion of
118 * the created future will wait until the returned future completes,
119 * and will then complete with the same result.
120 *
121 * If a value is returned, it becomes the result of the created future.
122 */
123 factory Future.sync(computation()) {
Lasse Reichstein Nielsen 2013/04/15 10:27:46 I prefer Future.of. The "sync" name (apart from be
floitsch 2013/04/15 20:30:11 A lot of users got this confused and thought that
110 try { 124 try {
111 var result = function(); 125 var result = computation();
112 return new _FutureImpl<T>().._setOrChainValue(result); 126 return new _FutureImpl<T>().._setOrChainValue(result);
113 } catch (error, stackTrace) { 127 } catch (error, stackTrace) {
114 return new _FutureImpl<T>.immediateError(error, stackTrace); 128 return new _FutureImpl<T>.immediateError(error, stackTrace);
115 } 129 }
116 } 130 }
117 131
118 /** 132 /**
119 * A future whose value is available in the next event-loop iteration. 133 * A future whose value is available in the next event-loop iteration.
120 * 134 *
121 * If [value] is not a [Future], using this constructor is equivalent 135 * If [value] is not a [Future], using this constructor is equivalent
122 * to [:new Future.of(() => value):]. 136 * to [:new Future.sync(() => value):].
123 * 137 *
124 * See [Completer] to create a Future and complete it later. 138 * See [Completer] to create a Future and complete it later.
125 */ 139 */
126 factory Future.immediate(T value) => new _FutureImpl<T>.immediate(value); 140 factory Future.value([T value]) => new _FutureImpl<T>.immediate(value);
127 141
128 /** 142 /**
129 * A future that completes with an error in the next event-loop iteration. 143 * A future that completes with an error in the next event-loop iteration.
Anders Johnsen 2013/04/15 08:59:06 Write it's the same as new Future(() => throw new
Lasse Reichstein Nielsen 2013/04/15 10:27:46 I hope not. You should never throw an AsyncError (
floitsch 2013/04/15 20:30:11 AsyncError is gone.
130 * 144 *
131 * See [Completer] to create a Future and complete it later. 145 * See [Completer] to create a Future and complete it later.
132 */ 146 */
133 factory Future.immediateError(var error, [Object stackTrace]) { 147 factory Future.error(var error, [Object stackTrace]) {
134 return new _FutureImpl<T>.immediateError(error, stackTrace); 148 return new _FutureImpl<T>.immediateError(error, stackTrace);
135 } 149 }
136 150
137 /** 151 /**
138 * Creates a future that completes after a delay. 152 * Creates a future that completes after a delay.
139 * 153 *
140 * The future will be completed after the given [duration] has passed with 154 * The future will be completed after the given [duration] has passed with
141 * the result of calling [computation]. If the duration is 0 or less, it 155 * the result of calling [computation]. If the duration is 0 or less, it
142 * completes no sooner than in the next event-loop iteration. 156 * completes no sooner than in the next event-loop iteration.
143 * 157 *
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 * completes when all elements have been processed. 194 * completes when all elements have been processed.
181 * 195 *
182 * The return values of all [Future]s are discarded. Any errors will cause the 196 * The return values of all [Future]s are discarded. Any errors will cause the
183 * iteration to stop and will be piped through the returned [Future]. 197 * iteration to stop and will be piped through the returned [Future].
184 */ 198 */
185 static Future forEach(Iterable input, Future f(element)) { 199 static Future forEach(Iterable input, Future f(element)) {
186 _FutureImpl doneSignal = new _FutureImpl(); 200 _FutureImpl doneSignal = new _FutureImpl();
187 Iterator iterator = input.iterator; 201 Iterator iterator = input.iterator;
188 void nextElement(_) { 202 void nextElement(_) {
189 if (iterator.moveNext()) { 203 if (iterator.moveNext()) {
190 new Future.of(() => f(iterator.current)) 204 new Future.sync(() => f(iterator.current))
191 .then(nextElement, onError: doneSignal._setError); 205 .then(nextElement, onError: doneSignal._setError);
192 } else { 206 } else {
193 doneSignal._setValue(null); 207 doneSignal._setValue(null);
194 } 208 }
195 } 209 }
196 nextElement(null); 210 nextElement(null);
197 return doneSignal; 211 return doneSignal;
198 } 212 }
199 213
200 /** 214 /**
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 * Otherwise the [exception] and an optional [stackTrace] is combined into an 373 * Otherwise the [exception] and an optional [stackTrace] is combined into an
360 * [AsyncError] and sent to this future's listeners. 374 * [AsyncError] and sent to this future's listeners.
361 */ 375 */
362 void completeError(Object exception, [Object stackTrace]); 376 void completeError(Object exception, [Object stackTrace]);
363 377
364 /** 378 /**
365 * Whether the future has been completed. 379 * Whether the future has been completed.
366 */ 380 */
367 bool get isCompleted; 381 bool get isCompleted;
368 } 382 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698