synchronous_future.dart 1.96 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:async';

/// A [Future] whose [then] implementation calls the callback immediately.
///
/// This is similar to [new Future.value], except that the value is available in
/// the same event-loop iteration.
///
/// ⚠ This class is useful in cases where you want to expose a single API, where
/// you normally want to have everything execute synchronously, but where on
/// rare occasions you want the ability to switch to an asynchronous model. **In
15 16
/// general use of this class should be avoided as it is very difficult to debug
/// such bimodal behavior.**
17 18 19
class SynchronousFuture<T> implements Future<T> {
  /// Creates a synchronous future.
  ///
20 21 22 23
  /// See also:
  ///
  ///  * [new Future.value] for information about creating a regular
  ///    [Future] that completes with a value.
24 25 26 27 28 29
  SynchronousFuture(this._value);

  final T _value;

  @override
  Stream<T> asStream() {
30
    final StreamController<T> controller = StreamController<T>();
31 32 33 34 35 36
    controller.add(_value);
    controller.close();
    return controller.stream;
  }

  @override
37
  Future<T> catchError(Function onError, { bool test(Object error) }) => Completer<T>().future;
38 39

  @override
40
  Future<E> then<E>(FutureOr<E> f(T value), { Function onError }) {
41
    final dynamic result = f(_value);
42
    if (result is Future<E>)
43
      return result;
44
    return SynchronousFuture<E>(result as E);
45 46 47
  }

  @override
48
  Future<T> timeout(Duration timeLimit, { FutureOr<T> onTimeout() }) {
49
    return Future<T>.value(_value).timeout(timeLimit, onTimeout: onTimeout);
50
  }
51 52

  @override
53
  Future<T> whenComplete(FutureOr<dynamic> action()) {
54
    try {
55
      final FutureOr<dynamic> result = action();
56
      if (result is Future)
57
        return result.then<T>((dynamic value) => _value);
58 59
      return this;
    } catch (e, stack) {
60
      return Future<T>.error(e, stack);
61 62
    }
  }
63
}