| 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 |
| 11 class _CompleterImpl<T> implements Completer<T> { | 11 class _CompleterImpl<T> implements Completer<T> { |
| 12 final Future<T> future; | 12 final Future<T> future; |
| 13 bool _isComplete = false; | 13 bool _isComplete = false; |
| 14 | 14 |
| 15 _CompleterImpl() : future = new _FutureImpl<T>(); | 15 _CompleterImpl() : future = new _FutureImpl<T>(); |
| 16 | 16 |
| 17 void complete([T value]) { | 17 void complete([T value]) { |
| 18 if (_isComplete) throw new StateError("Future already completed"); | 18 if (_isComplete) throw new StateError("Future already completed"); |
| 19 _isComplete = true; | 19 _isComplete = true; |
| 20 _FutureImpl future = this.future; | 20 _FutureImpl future = this.future; |
| 21 future._setValue(value); | 21 future._setValue(value); |
| 22 } | 22 } |
| 23 | 23 |
| 24 void completeError(Object error, [Object stackTrace = null]) { | 24 void completeError(Object error, [Object stackTrace = null]) { |
| 25 if (_isComplete) throw new StateError("Future already completed"); | 25 if (_isComplete) throw new StateError("Future already completed"); |
| 26 _isComplete = true; | 26 _isComplete = true; |
| 27 AsyncError asyncError; |
| 28 if (error is AsyncError) { |
| 29 asyncError = error; |
| 30 } else { |
| 31 asyncError = new AsyncError(error, stackTrace); |
| 32 } |
| 33 // Never complete an error in the same cycle. Otherwise users might |
| 34 // not have a chance to register their error-handlers. |
| 27 new Timer(0, (_) { | 35 new Timer(0, (_) { |
| 28 // Never complete an error in the same cycle. Otherwise users might | |
| 29 // not have a chance to register their error-handlers. | |
| 30 _FutureImpl future = this.future; | 36 _FutureImpl future = this.future; |
| 31 future._setError(new AsyncError(error, stackTrace)); | 37 future._setError(asyncError); |
| 32 }); | 38 }); |
| 33 } | 39 } |
| 34 } | 40 } |
| 35 | 41 |
| 36 /** | 42 /** |
| 37 * A listener on a future. | 43 * A listener on a future. |
| 38 * | 44 * |
| 39 * When the future completes, the [_sendValue] or [_sendError] method | 45 * When the future completes, the [_sendValue] or [_sendError] method |
| 40 * is invoked with the result. | 46 * is invoked with the result. |
| 41 * | 47 * |
| (...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 // checked mode. | 335 // checked mode. |
| 330 final /* _FutureOnValue<S> */ _onValue; | 336 final /* _FutureOnValue<S> */ _onValue; |
| 331 | 337 |
| 332 _ThenFuture(this._onValue); | 338 _ThenFuture(this._onValue); |
| 333 | 339 |
| 334 _sendValue(S value) { | 340 _sendValue(S value) { |
| 335 assert(_onValue != null); | 341 assert(_onValue != null); |
| 336 var result; | 342 var result; |
| 337 try { | 343 try { |
| 338 result = _onValue(value); | 344 result = _onValue(value); |
| 345 } on AsyncError catch (e) { |
| 346 _setError(e); |
| 347 return; |
| 339 } catch (e, s) { | 348 } catch (e, s) { |
| 340 _setError(new AsyncError(e, s)); | 349 _setError(new AsyncError(e, s)); |
| 341 return; | 350 return; |
| 342 } | 351 } |
| 343 _setOrChainValue(result); | 352 _setOrChainValue(result); |
| 344 } | 353 } |
| 345 | 354 |
| 346 void _sendError(AsyncError error) { | 355 void _sendError(AsyncError error) { |
| 347 _setError(error); | 356 _setError(error); |
| 348 } | 357 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 373 } | 382 } |
| 374 if (!matchesTest) { | 383 if (!matchesTest) { |
| 375 _setError(error); | 384 _setError(error); |
| 376 return; | 385 return; |
| 377 } | 386 } |
| 378 } | 387 } |
| 379 // Act on the error, and use the result as this future's result. | 388 // Act on the error, and use the result as this future's result. |
| 380 var result; | 389 var result; |
| 381 try { | 390 try { |
| 382 result = _onError(error); | 391 result = _onError(error); |
| 392 } on AsyncError catch (e) { |
| 393 _setError(e); |
| 394 return; |
| 383 } catch (e, s) { | 395 } catch (e, s) { |
| 384 _setError(new AsyncError.withCause(e, s, error)); | 396 _setError(new AsyncError.withCause(e, s, error)); |
| 385 return; | 397 return; |
| 386 } | 398 } |
| 387 _setOrChainValue(result); | 399 _setOrChainValue(result); |
| 388 } | 400 } |
| 389 } | 401 } |
| 390 | 402 |
| 391 /** Future returned by [Future.then] with an [:onError:] parameter. */ | 403 /** Future returned by [Future.then] with an [:onError:] parameter. */ |
| 392 class _SubscribeFuture<S, T> extends _ThenFuture<S, T> { | 404 class _SubscribeFuture<S, T> extends _ThenFuture<S, T> { |
| 393 final _FutureOnError _onError; | 405 final _FutureOnError _onError; |
| 394 | 406 |
| 395 _SubscribeFuture(onValue(S value), this._onError) : super(onValue); | 407 _SubscribeFuture(onValue(S value), this._onError) : super(onValue); |
| 396 | 408 |
| 397 // The _sendValue method is inherited from ThenFuture. | 409 // The _sendValue method is inherited from ThenFuture. |
| 398 | 410 |
| 399 void _sendError(AsyncError error) { | 411 void _sendError(AsyncError error) { |
| 400 assert(_onError != null); | 412 assert(_onError != null); |
| 401 var result; | 413 var result; |
| 402 try { | 414 try { |
| 403 result = _onError(error); | 415 result = _onError(error); |
| 416 } on AsyncError catch (e) { |
| 417 _setError(e); |
| 418 return; |
| 404 } catch (e, s) { | 419 } catch (e, s) { |
| 405 _setError(new AsyncError.withCause(e, s, error)); | 420 _setError(new AsyncError.withCause(e, s, error)); |
| 406 return; | 421 return; |
| 407 } | 422 } |
| 408 _setOrChainValue(result); | 423 _setOrChainValue(result); |
| 409 } | 424 } |
| 410 } | 425 } |
| 411 | 426 |
| 412 /** Future returned by [Future.whenComplete]. */ | 427 /** Future returned by [Future.whenComplete]. */ |
| 413 class _WhenFuture<T> extends _TransformFuture<T, T> { | 428 class _WhenFuture<T> extends _TransformFuture<T, T> { |
| 414 final _FutureAction _action; | 429 final _FutureAction _action; |
| 415 | 430 |
| 416 _WhenFuture(this._action); | 431 _WhenFuture(this._action); |
| 417 | 432 |
| 418 void _sendValue(T value) { | 433 void _sendValue(T value) { |
| 419 try { | 434 try { |
| 420 var result = _action(); | 435 var result = _action(); |
| 421 if (result is Future) { | 436 if (result is Future) { |
| 422 Future resultFuture = result; | 437 Future resultFuture = result; |
| 423 resultFuture.then((_) { | 438 resultFuture.then((_) { |
| 424 _setValue(value); | 439 _setValue(value); |
| 425 }, onError: _setError); | 440 }, onError: _setError); |
| 426 return; | 441 return; |
| 427 } | 442 } |
| 443 } on AsyncError catch (e) { |
| 444 _setError(e); |
| 445 return; |
| 428 } catch (e, s) { | 446 } catch (e, s) { |
| 429 _setError(new AsyncError(e, s)); | 447 _setError(new AsyncError(e, s)); |
| 430 return; | 448 return; |
| 431 } | 449 } |
| 432 | 450 |
| 433 _setValue(value); | 451 _setValue(value); |
| 434 } | 452 } |
| 435 | 453 |
| 436 void _sendError(AsyncError error) { | 454 void _sendError(AsyncError error) { |
| 437 try { | 455 try { |
| 438 var result = _action(); | 456 var result = _action(); |
| 439 if (result is Future) { | 457 if (result is Future) { |
| 440 Future resultFuture = result; | 458 Future resultFuture = result; |
| 441 // TODO(lrn): Find a way to combine [error] into [e]. | 459 // TODO(lrn): Find a way to combine [error] into [e]. |
| 442 resultFuture.then((_) { | 460 resultFuture.then((_) { |
| 443 _setError(error); | 461 _setError(error); |
| 444 }, onError: _setError); | 462 }, onError: _setError); |
| 445 return; | 463 return; |
| 446 } | 464 } |
| 465 } on AsyncError catch (e) { |
| 466 _setError(e); |
| 467 return; |
| 447 } catch (e, s) { | 468 } catch (e, s) { |
| 448 error = new AsyncError.withCause(e, s, error); | 469 error = new AsyncError.withCause(e, s, error); |
| 449 } | 470 } |
| 450 _setError(error); | 471 _setError(error); |
| 451 } | 472 } |
| 452 } | 473 } |
| 453 | 474 |
| 454 /** | 475 /** |
| 455 * Thin wrapper around a [Future]. | 476 * Thin wrapper around a [Future]. |
| 456 * | 477 * |
| (...skipping 13 matching lines...) Expand all Loading... |
| 470 Future catchError(function(AsyncError error), {bool test(var error)}) { | 491 Future catchError(function(AsyncError error), {bool test(var error)}) { |
| 471 return _future.catchError(function, test: test); | 492 return _future.catchError(function, test: test); |
| 472 } | 493 } |
| 473 | 494 |
| 474 Future<T> whenComplete(action()) { | 495 Future<T> whenComplete(action()) { |
| 475 return _future.whenComplete(action); | 496 return _future.whenComplete(action); |
| 476 } | 497 } |
| 477 | 498 |
| 478 Stream<T> asStream() => new Stream.fromFuture(_future); | 499 Stream<T> asStream() => new Stream.fromFuture(_future); |
| 479 } | 500 } |
| OLD | NEW |