Line data Source code
1 : // Copyright (c) 2015, 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 : import 'dart:async';
6 :
7 : import '../delegate/future.dart';
8 : import '../result.dart';
9 :
10 : /// A [Future] wrapper that provides synchronous access to the result of the
11 : /// wrapped [Future] once it's completed.
12 : class ResultFuture<T> extends DelegatingFuture<T> {
13 : /// Whether the future has fired and [result] is available.
14 0 : bool get isComplete => result != null;
15 :
16 : /// The result of the wrapped [Future], if it's completed.
17 : ///
18 : /// If it hasn't completed yet, this will be `null`.
19 0 : Result<T> get result => _result;
20 : Result<T> _result;
21 :
22 : factory ResultFuture(Future<T> future) {
23 : ResultFuture<T> resultFuture;
24 0 : resultFuture = new ResultFuture._(() async {
25 0 : var result = await Result.capture(future);
26 0 : resultFuture._result = result;
27 0 : return await result.asFuture;
28 0 : }());
29 : return resultFuture;
30 : }
31 :
32 0 : ResultFuture._(Future<T> future) : super(future);
33 : }
|