| 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 deprecatedFutureValue(_FutureImpl future) => | 7 deprecatedFutureValue(_FutureImpl future) => |
| 8 future._isComplete ? future._resultOrListeners : null; | 8 future._isComplete ? future._resultOrListeners : null; |
| 9 | 9 |
| 10 | 10 |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 if (_hasValue) { | 150 if (_hasValue) { |
| 151 return new _FutureWrapper(this); | 151 return new _FutureWrapper(this); |
| 152 } | 152 } |
| 153 if (!_isComplete) { | 153 if (!_isComplete) { |
| 154 return new _CatchErrorFuture(f, test).._subscribeTo(this); | 154 return new _CatchErrorFuture(f, test).._subscribeTo(this); |
| 155 } else { | 155 } else { |
| 156 return _handleError(f, test); | 156 return _handleError(f, test); |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 Future<T> whenComplete(void action()) { | 160 Future<T> whenComplete(action()) { |
| 161 _WhenFuture<T> whenFuture = new _WhenFuture<T>(action); | 161 _WhenFuture<T> whenFuture = new _WhenFuture<T>(action); |
| 162 if (!_isComplete) { | 162 if (!_isComplete) { |
| 163 _addListener(whenFuture); | 163 _addListener(whenFuture); |
| 164 } else if (_hasValue) { | 164 } else if (_hasValue) { |
| 165 new Timer(0, (_) { | 165 new Timer(0, (_) { |
| 166 T value = _resultOrListeners; | 166 T value = _resultOrListeners; |
| 167 whenFuture._sendValue(value); | 167 whenFuture._sendValue(value); |
| 168 }); | 168 }); |
| 169 } else { | 169 } else { |
| 170 assert(_hasError); | 170 assert(_hasError); |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 } | 409 } |
| 410 | 410 |
| 411 /** Future returned by [Future.whenComplete]. */ | 411 /** Future returned by [Future.whenComplete]. */ |
| 412 class _WhenFuture<T> extends _TransformFuture<T, T> { | 412 class _WhenFuture<T> extends _TransformFuture<T, T> { |
| 413 final _FutureAction _action; | 413 final _FutureAction _action; |
| 414 | 414 |
| 415 _WhenFuture(this._action); | 415 _WhenFuture(this._action); |
| 416 | 416 |
| 417 void _sendValue(T value) { | 417 void _sendValue(T value) { |
| 418 try { | 418 try { |
| 419 _action(); | 419 var result = _action(); |
| 420 if (result is Future) { |
| 421 Future resultFuture = result; |
| 422 result.then((_) { |
| 423 _setValue(value); |
| 424 }, onError: (AsyncError e) { |
| 425 _setError(e); |
| 426 }); |
| 427 return; |
| 428 } |
| 420 } catch (e, s) { | 429 } catch (e, s) { |
| 421 _setError(new AsyncError(e, s)); | 430 _setError(new AsyncError(e, s)); |
| 422 return; | 431 return; |
| 423 } | 432 } |
| 424 _setValue(value); | 433 _setValue(value); |
| 425 } | 434 } |
| 426 | 435 |
| 427 void _sendError(AsyncError error) { | 436 void _sendError(AsyncError error) { |
| 428 try { | 437 try { |
| 429 _action(); | 438 var result = _action(); |
| 439 if (result is Future) { |
| 440 Future resultFuture = result; |
| 441 result.then((_) { |
| 442 _setError(error); |
| 443 }, onError: (AsyncError e) { |
| 444 // TODO(lrn): Find a way to combine error into the |
| 445 // resulting error. |
| 446 _setError(e); |
| 447 }); |
| 448 return; |
| 449 } |
| 430 } catch (e, s) { | 450 } catch (e, s) { |
| 431 error = new AsyncError.withCause(e, s, error); | 451 error = new AsyncError.withCause(e, s, error); |
| 432 } | 452 } |
| 433 _setError(error); | 453 _setError(error); |
| 434 } | 454 } |
| 435 } | 455 } |
| 436 | 456 |
| 437 /** | 457 /** |
| 438 * Thin wrapper around a [Future]. | 458 * Thin wrapper around a [Future]. |
| 439 * | 459 * |
| 440 * This is used to return a "new" [Future] that effectively work just | 460 * This is used to return a "new" [Future] that effectively work just |
| 441 * as an existing [Future], without making this discoverable by comparing | 461 * as an existing [Future], without making this discoverable by comparing |
| 442 * identities. | 462 * identities. |
| 443 */ | 463 */ |
| 444 class _FutureWrapper<T> implements Future<T> { | 464 class _FutureWrapper<T> implements Future<T> { |
| 445 final Future<T> _future; | 465 final Future<T> _future; |
| 446 | 466 |
| 447 _FutureWrapper(this._future); | 467 _FutureWrapper(this._future); |
| 448 | 468 |
| 449 Future then(function(T value), { onError(AsyncError error) }) { | 469 Future then(function(T value), { onError(AsyncError error) }) { |
| 450 return _future.then(function, onError: onError); | 470 return _future.then(function, onError: onError); |
| 451 } | 471 } |
| 452 | 472 |
| 453 Future catchError(function(AsyncError error), {bool test(var error)}) { | 473 Future catchError(function(AsyncError error), {bool test(var error)}) { |
| 454 return _future.catchError(function, test: test); | 474 return _future.catchError(function, test: test); |
| 455 } | 475 } |
| 456 | 476 |
| 457 Future whenComplete(void action()) { | 477 Future whenComplete(action()) { |
| 458 return _future.whenComplete(action); | 478 return _future.whenComplete(action); |
| 459 } | 479 } |
| 460 | 480 |
| 461 Stream<T> asStream() => new Stream.fromFuture(this); | 481 Stream<T> asStream() => new Stream.fromFuture(this); |
| 462 } | 482 } |
| OLD | NEW |