| OLD | NEW |
| 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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 * completed with the error. If a thrown value is an [AsyncError], it is used | 93 * completed with the error. If a thrown value is an [AsyncError], it is used |
| 94 * directly, instead of wrapping this error again in another [AsyncError]. | 94 * directly, instead of wrapping this error again in another [AsyncError]. |
| 95 * | 95 * |
| 96 * If the returned value is itself a [Future], completion of | 96 * If the returned value is itself a [Future], completion of |
| 97 * the created future will wait until the returned future completes, | 97 * the created future will wait until the returned future completes, |
| 98 * and will then complete with the same result. | 98 * and will then complete with the same result. |
| 99 * | 99 * |
| 100 * If a value is returned, it becomes the result of the created future. | 100 * If a value is returned, it becomes the result of the created future. |
| 101 */ | 101 */ |
| 102 factory Future(computation()) { | 102 factory Future(computation()) { |
| 103 _Future result = new _Future<T>(); | 103 _ThenFuture<dynamic, T> future = |
| 104 Timer.run(() { | 104 new _ThenFuture<dynamic, T>((_) => computation()); |
| 105 try { | 105 Timer.run(() => future._sendValue(null)); |
| 106 result._complete(computation()); | 106 return future; |
| 107 } catch (e, s) { | |
| 108 result._completeError(e, s); | |
| 109 } | |
| 110 }); | |
| 111 return result; | |
| 112 } | 107 } |
| 113 | 108 |
| 114 /** | 109 /** |
| 115 * Creates a future containing the result of immediately calling | 110 * Creates a future containing the result of immediately calling |
| 116 * [computation]. | 111 * [computation]. |
| 117 * | 112 * |
| 118 * If calling [computation] throws, the returned future is completed with the | 113 * if the result of executing [computation] throws, the returned future is |
| 119 * error. If a thrown value is an [AsyncError], it is used | 114 * completed with the error. If a thrown value is an [AsyncError], it is used |
| 120 * directly, instead of wrapping this error again in another [AsyncError]. | 115 * directly, instead of wrapping this error again in another [AsyncError]. |
| 121 * | 116 * |
| 122 * If the returned value is itself a [Future], completion of | 117 * If the returned value is itself a [Future], completion of |
| 123 * the created future will wait until the returned future completes, | 118 * the created future will wait until the returned future completes, |
| 124 * and will then complete with the same result. | 119 * and will then complete with the same result. |
| 125 */ | 120 */ |
| 126 factory Future.sync(computation()) { | 121 factory Future.sync(computation()) { |
| 127 try { | 122 try { |
| 128 var result = computation(); | 123 var result = computation(); |
| 129 return new Future<T>.value(result); | 124 return new _FutureImpl<T>().._setOrChainValue(result); |
| 130 } catch (error, stackTrace) { | 125 } catch (error, stackTrace) { |
| 131 return new Future<T>.error(error, stackTrace); | 126 return new _FutureImpl<T>.immediateError(error, stackTrace); |
| 132 } | 127 } |
| 133 } | 128 } |
| 134 | 129 |
| 135 /** | 130 /** |
| 136 * A future whose value is available in the next event-loop iteration. | 131 * A future whose value is available in the next event-loop iteration. |
| 137 * | 132 * |
| 138 * If [value] is not a [Future], using this constructor is equivalent | 133 * If [value] is not a [Future], using this constructor is equivalent |
| 139 * to [:new Future.sync(() => value):]. | 134 * to [:new Future.sync(() => value):]. |
| 140 * | 135 * |
| 141 * See [Completer] to create a Future and complete it later. | 136 * See [Completer] to create a Future and complete it later. |
| 142 */ | 137 */ |
| 143 factory Future.value([T value]) { | 138 factory Future.value([T value]) => new _FutureImpl<T>.immediate(value); |
| 144 return new _Future<T>.immediate(value); | |
| 145 } | |
| 146 | 139 |
| 147 /** | 140 /** |
| 148 * A future that completes with an error in the next event-loop iteration. | 141 * A future that completes with an error in the next event-loop iteration. |
| 149 * | 142 * |
| 150 * See [Completer] to create a Future and complete it later. | 143 * See [Completer] to create a Future and complete it later. |
| 151 */ | 144 */ |
| 152 factory Future.error(var error, [Object stackTrace]) { | 145 factory Future.error(var error, [Object stackTrace]) { |
| 153 return new _Future<T>.immediateError(error, stackTrace); | 146 return new _FutureImpl<T>.immediateError(error, stackTrace); |
| 154 } | 147 } |
| 155 | 148 |
| 156 /** | 149 /** |
| 157 * Creates a future that completes after a delay. | 150 * Creates a future that completes after a delay. |
| 158 * | 151 * |
| 159 * The future will be completed after the given [duration] has passed with | 152 * The future will be completed after the given [duration] has passed with |
| 160 * the result of calling [computation]. If the duration is 0 or less, it | 153 * the result of calling [computation]. If the duration is 0 or less, it |
| 161 * completes no sooner than in the next event-loop iteration. | 154 * completes no sooner than in the next event-loop iteration. |
| 162 * | 155 * |
| 163 * If [computation] is not given or [:null:] then it will behave as if | 156 * If [computation] is not given or [:null:] then it will behave as if |
| 164 * [computation] was set to [:() => null:]. That is, it will complete with | 157 * [computation] was set to [:() => null:]. That is, it will complete with |
| 165 * [:null:]. | 158 * [:null:]. |
| 166 * | 159 * |
| 167 * If calling [computation] throws, the created future will complete with the | 160 * If calling [computation] throws, the created future will complete with the |
| 168 * error. | 161 * error. |
| 169 * | 162 * |
| 170 * See [Completer]s, for futures with values that are computed asynchronously. | 163 * See [Completer]s, for futures with values that are computed asynchronously. |
| 171 */ | 164 */ |
| 172 factory Future.delayed(Duration duration, [T computation()]) { | 165 factory Future.delayed(Duration duration, [T computation()]) { |
| 173 Completer completer = new Completer.sync(); | 166 // TODO(floitsch): no need to allocate a ThenFuture when the computation is |
| 174 Future result = completer.future; | 167 // null. |
| 175 if (computation != null) { | 168 if (computation == null) computation = (() => null); |
| 176 result = result.then((ignored) => computation()); | 169 _ThenFuture<dynamic, T> future = |
| 177 } | 170 new _ThenFuture<dynamic, T>((_) => computation()); |
| 178 new Timer(duration, () { completer.complete(null); }); | 171 new Timer(duration, () => future._sendValue(null)); |
| 179 return result; | 172 return future; |
| 180 } | 173 } |
| 181 | 174 |
| 182 /** | 175 /** |
| 183 * Wait for all the given futures to complete and collect their values. | 176 * Wait for all the given futures to complete and collect their values. |
| 184 * | 177 * |
| 185 * Returns a future which will complete once all the futures in a list are | 178 * Returns a future which will complete once all the futures in a list are |
| 186 * complete. If any of the futures in the list completes with an error, | 179 * complete. If any of the futures in the list completes with an error, |
| 187 * the resulting future also completes with an error. Otherwise the value | 180 * the resulting future also completes with an error. Otherwise the value |
| 188 * of the returned future will be a list of all the values that were produced. | 181 * of the returned future will be a list of all the values that were produced. |
| 189 */ | 182 */ |
| 190 static Future<List> wait(Iterable<Future> futures) { | 183 static Future<List> wait(Iterable<Future> futures) { |
| 191 Completer completer; | 184 return new _FutureImpl<List>.wait(futures); |
| 192 // List collecting values from the futures. | |
| 193 // Set to null if an error occurs. | |
| 194 List values; | |
| 195 void handleError(error) { | |
| 196 if (values != null) { | |
| 197 values = null; | |
| 198 completer.completeError(error); | |
| 199 } | |
| 200 } | |
| 201 // As each future completes, put its value into the corresponding | |
| 202 // position in the list of values. | |
| 203 int remaining = 0; | |
| 204 for (Future future in futures) { | |
| 205 int pos = remaining++; | |
| 206 future.catchError(handleError).then((Object value) { | |
| 207 if (values == null) return null; | |
| 208 values[pos] = value; | |
| 209 remaining--; | |
| 210 if (remaining == 0) { | |
| 211 completer.complete(values); | |
| 212 } | |
| 213 }); | |
| 214 } | |
| 215 if (remaining == 0) { | |
| 216 return new Future.value(const []); | |
| 217 } | |
| 218 values = new List(remaining); | |
| 219 completer = new Completer<List>(); | |
| 220 return completer.future; | |
| 221 } | 185 } |
| 222 | 186 |
| 223 /** | 187 /** |
| 224 * Perform an async operation for each element of the iterable, in turn. | 188 * Perform an async operation for each element of the iterable, in turn. |
| 225 * | 189 * |
| 226 * Runs [f] for each element in [input] in order, moving to the next element | 190 * Runs [f] for each element in [input] in order, moving to the next element |
| 227 * only when the [Future] returned by [f] completes. Returns a [Future] that | 191 * only when the [Future] returned by [f] completes. Returns a [Future] that |
| 228 * completes when all elements have been processed. | 192 * completes when all elements have been processed. |
| 229 * | 193 * |
| 230 * The return values of all [Future]s are discarded. Any errors will cause the | 194 * The return values of all [Future]s are discarded. Any errors will cause the |
| 231 * iteration to stop and will be piped through the returned [Future]. | 195 * iteration to stop and will be piped through the returned [Future]. |
| 232 */ | 196 */ |
| 233 static Future forEach(Iterable input, Future f(element)) { | 197 static Future forEach(Iterable input, Future f(element)) { |
| 234 _Future doneSignal = new _Future(); | 198 _FutureImpl doneSignal = new _FutureImpl(); |
| 235 Iterator iterator = input.iterator; | 199 Iterator iterator = input.iterator; |
| 236 void nextElement(_) { | 200 void nextElement(_) { |
| 237 if (iterator.moveNext()) { | 201 if (iterator.moveNext()) { |
| 238 new Future.sync(() => f(iterator.current)) | 202 new Future.sync(() => f(iterator.current)) |
| 239 .then(nextElement, onError: doneSignal._completeError); | 203 .then(nextElement, onError: doneSignal._setError); |
| 240 } else { | 204 } else { |
| 241 doneSignal._complete(null); | 205 doneSignal._setValue(null); |
| 242 } | 206 } |
| 243 } | 207 } |
| 244 nextElement(null); | 208 nextElement(null); |
| 245 return doneSignal; | 209 return doneSignal; |
| 246 } | 210 } |
| 247 | 211 |
| 248 /** | 212 /** |
| 249 * When this future completes with a value, then [onValue] is called with this | 213 * When this future completes with a value, then [onValue] is called with this |
| 250 * value. If [this] future is already completed then the invocation of | 214 * value. If [this] future is already completed then the invocation of |
| 251 * [onValue] is delayed until the next event-loop iteration. | 215 * [onValue] is delayed until the next event-loop iteration. |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 * | 418 * |
| 455 * The argument [exception] must not be `null`. | 419 * The argument [exception] must not be `null`. |
| 456 */ | 420 */ |
| 457 void completeError(Object exception, [Object stackTrace]); | 421 void completeError(Object exception, [Object stackTrace]); |
| 458 | 422 |
| 459 /** | 423 /** |
| 460 * Whether the future has been completed. | 424 * Whether the future has been completed. |
| 461 */ | 425 */ |
| 462 bool get isCompleted; | 426 bool get isCompleted; |
| 463 } | 427 } |
| OLD | NEW |