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 : /// A wrapper that forwards calls to a [Future]. 8 : class DelegatingFuture<T> implements Future<T> { 9 : /// The wrapped [Future]. 10 : final Future<T> _future; 11 : 12 0 : DelegatingFuture(this._future); 13 : 14 : /// Creates a wrapper which throws if [future]'s value isn't an instance of 15 : /// `T`. 16 : /// 17 : /// This soundly converts a [Future] to a `Future<T>`, regardless of its 18 : /// original generic type, by asserting that its value is an instance of `T` 19 : /// whenever it's provided. If it's not, the future throws a [TypeError]. 20 0 : @Deprecated('Use future.then((v) => v as T) instead.') 21 : static Future<T> typed<T>(Future future) => 22 0 : future is Future<T> ? future : future.then((v) => v as T); 23 : 24 0 : @override 25 0 : Stream<T> asStream() => _future.asStream(); 26 : 27 0 : @override 28 : Future<T> catchError(Function onError, {bool Function(Object error)? test}) => 29 0 : _future.catchError(onError, test: test); 30 : 31 0 : @override 32 : Future<S> then<S>(FutureOr<S> Function(T) onValue, {Function? onError}) => 33 0 : _future.then(onValue, onError: onError); 34 : 35 0 : @override 36 : Future<T> whenComplete(FutureOr Function() action) => 37 0 : _future.whenComplete(action); 38 : 39 0 : @override 40 : Future<T> timeout(Duration timeLimit, {FutureOr<T> Function()? onTimeout}) => 41 0 : _future.timeout(timeLimit, onTimeout: onTimeout); 42 : }