getManagedDelayedFuture<T> method

  1. @mustCallSuper
  2. @override
Future<T> getManagedDelayedFuture <T>(Duration duration, T callback())
@mustCallSuper, override

Creates a Future that will complete, with the value returned by callback, after the given amount of time has elapsed.

If the object is disposed before the time has elapsed the future will complete with an ObjectDisposedException error.

Implementation

@mustCallSuper
@override
Future<T> getManagedDelayedFuture<T>(Duration duration, T callback()) {
  _throwOnInvalidCall2(
      'getManagedDelayedFuture', 'duration', 'callback', duration, callback);
  var completer = new Completer<T>();
  var timer =
      new _ObservableTimer(duration, () => completer.complete(callback()));
  var disposable = new ManagedDisposer(() async {
    timer.cancel();
    completer.completeError(new ObjectDisposedException());
  });
  _logManageMessage(completer.future);
  _internalDisposables.add(disposable);
  timer.didConclude.then((Null _) {
    // ignore: deprecated_member_use
    if (!isDisposedOrDisposing) {
      _logUnmanageMessage(completer.future);
      _internalDisposables.remove(disposable);
    }
  });
  return completer.future;
}