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

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

Issue 2790663003: Use FutureOr more and make Future.sync return the resulting Future directly. (Closed)
Patch Set: State that doWhile completes with null. Created 3 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
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 /** The onValue and onError handlers return either a value or a future */ 7 /** The onValue and onError handlers return either a value or a future */
8 typedef FutureOr<T> _FutureOnValue<S, T>(S value); 8 typedef FutureOr<T> _FutureOnValue<S, T>(S value);
9 /** Test used by [Future.catchError] to handle skip some errors. */ 9 /** Test used by [Future.catchError] to handle skip some errors. */
10 typedef bool _FutureErrorTest(Object error); 10 typedef bool _FutureErrorTest(Object error);
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 * The future is another future that his future is chained to. This future 200 * The future is another future that his future is chained to. This future
201 * is waiting for the other future to complete, and when it does, this future 201 * is waiting for the other future to complete, and when it does, this future
202 * will complete with the same result. 202 * will complete with the same result.
203 * All listeners are forwarded to the other future. 203 * All listeners are forwarded to the other future.
204 */ 204 */
205 var _resultOrListeners; 205 var _resultOrListeners;
206 206
207 // This constructor is used by async/await. 207 // This constructor is used by async/await.
208 _Future(); 208 _Future();
209 209
210 /// Valid types for value: `T` or `Future<T>`. 210 _Future.immediate(FutureOr<T> result) {
211 _Future.immediate(value) { 211 _asyncComplete(result);
212 _asyncComplete(value);
213 } 212 }
214 213
215 _Future.immediateError(var error, [StackTrace stackTrace]) { 214 _Future.immediateError(var error, [StackTrace stackTrace]) {
216 _asyncCompleteError(error, stackTrace); 215 _asyncCompleteError(error, stackTrace);
217 } 216 }
218 217
218 /** Creates a future that is already completed with the value. */
219 _Future.value(T value) {
220 _setValue(value);
221 }
222
219 bool get _mayComplete => _state == _INCOMPLETE; 223 bool get _mayComplete => _state == _INCOMPLETE;
220 bool get _isPendingComplete => _state == _PENDING_COMPLETE; 224 bool get _isPendingComplete => _state == _PENDING_COMPLETE;
221 bool get _mayAddListener => _state <= _PENDING_COMPLETE; 225 bool get _mayAddListener => _state <= _PENDING_COMPLETE;
222 bool get _isChained => _state == _CHAINED; 226 bool get _isChained => _state == _CHAINED;
223 bool get _isComplete => _state >= _VALUE; 227 bool get _isComplete => _state >= _VALUE;
224 bool get _hasError => _state == _ERROR; 228 bool get _hasError => _state == _ERROR;
225 229
226 void _setChained(_Future source) { 230 void _setChained(_Future source) {
227 assert(_mayAddListener); 231 assert(_mayAddListener);
228 _state = _CHAINED; 232 _state = _CHAINED;
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // 2. the future is not yet completed but might become an error. 493 // 2. the future is not yet completed but might become an error.
490 // The first case means that we must not immediately complete the Future, 494 // The first case means that we must not immediately complete the Future,
491 // as our code would immediately start propagating the error without 495 // as our code would immediately start propagating the error without
492 // giving the time to install error-handlers. 496 // giving the time to install error-handlers.
493 // However the second case requires us to deal with the value immediately. 497 // However the second case requires us to deal with the value immediately.
494 // Otherwise the value could complete with an error and report an 498 // Otherwise the value could complete with an error and report an
495 // unhandled error, even though we know we are already going to listen to 499 // unhandled error, even though we know we are already going to listen to
496 // it. 500 // it.
497 501
498 if (value is Future<T>) { 502 if (value is Future<T>) {
499 if (value is _Future<T>) { 503 _chainFuture(value);
500 if (value._hasError) {
501 // Case 1 from above. Delay completion to enable the user to register
502 // callbacks.
503 _setPendingComplete();
504 _zone.scheduleMicrotask(() {
505 _chainCoreFuture(value, this);
506 });
507 } else {
508 _chainCoreFuture(value, this);
509 }
510 } else {
511 // Case 2 from above. Chain the future immediately.
512 // Note that we are still completing asynchronously (through
513 // _chainForeignFuture).
514 _chainForeignFuture(value, this);
515 }
516 return; 504 return;
517 } 505 }
518 T typedValue = value as Object/*=T*/; 506 T typedValue = value as Object/*=T*/;
519 507
520 _setPendingComplete(); 508 _setPendingComplete();
521 _zone.scheduleMicrotask(() { 509 _zone.scheduleMicrotask(() {
522 _completeWithValue(typedValue); 510 _completeWithValue(typedValue);
523 }); 511 });
524 } 512 }
525 513
514 void _chainFuture(Future<T> value) {
515 if (value is _Future<T>) {
516 if (value._hasError) {
517 // Delay completion to allow the user to register callbacks.
518 _setPendingComplete();
519 _zone.scheduleMicrotask(() {
520 _chainCoreFuture(value, this);
521 });
522 } else {
523 _chainCoreFuture(value, this);
524 }
525 return;
526 }
527 // Just listen on the foreign future. This guarantees an async delay.
528 _chainForeignFuture(value, this);
529 }
530
526 void _asyncCompleteError(error, StackTrace stackTrace) { 531 void _asyncCompleteError(error, StackTrace stackTrace) {
527 assert(!_isComplete); 532 assert(!_isComplete);
528 533
529 _setPendingComplete(); 534 _setPendingComplete();
530 _zone.scheduleMicrotask(() { 535 _zone.scheduleMicrotask(() {
531 _completeError(error, stackTrace); 536 _completeError(error, stackTrace);
532 }); 537 });
533 } 538 }
534 539
535 /** 540 /**
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
730 } 735 }
731 }, onError: (e, s) { 736 }, onError: (e, s) {
732 if (timer.isActive) { 737 if (timer.isActive) {
733 timer.cancel(); 738 timer.cancel();
734 result._completeError(e, s); 739 result._completeError(e, s);
735 } 740 }
736 }); 741 });
737 return result; 742 return result;
738 } 743 }
739 } 744 }
OLDNEW
« no previous file with comments | « sdk/lib/async/future.dart ('k') | tests/lib/async/future_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698