OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library value_future; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 /// A [Future] wrapper that provides synchronous access to the value of the |
| 10 /// wrapped [Future] once it's completed. |
| 11 class ValueFuture<T> implements Future<T> { |
| 12 /// The wrapped [Future]. |
| 13 Future<T> _future; |
| 14 |
| 15 /// The [value] of the wrapped [Future], if it's completed succesfully. If it |
| 16 /// hasn't completed yet or has completed with an error, this will be `null`. |
| 17 T get value => _value; |
| 18 T _value; |
| 19 |
| 20 /// Whether the wrapped [Future] has completed successfully. |
| 21 bool get hasValue => _hasValue; |
| 22 var _hasValue = false; |
| 23 |
| 24 ValueFuture(Future<T> future) { |
| 25 _future = future.then((value) { |
| 26 _value = value; |
| 27 _hasValue = true; |
| 28 return value; |
| 29 }); |
| 30 } |
| 31 |
| 32 Stream<T> asStream() => _future.asStream(); |
| 33 Future catchError(onError(AsyncError asyncError), {bool test(error)}) => |
| 34 _future.catchError(onError, test: test); |
| 35 Future then(onValue(T value), {onError(AsyncError asyncError)}) => |
| 36 _future.then(onValue, onError: onError); |
| 37 Future<T> whenComplete(action()) => _future.whenComplete(action); |
| 38 } |
OLD | NEW |