manageCompleter<T> method

  1. @mustCallSuper
  2. @override
Completer<T> manageCompleter<T>(
  1. Completer<T> completer
)
inherited

Ensure that a completer is completed when the object is disposed.

If the completer has not been completed by the time the object is disposed, it will be completed with an ObjectDisposedException error.

Implementation

@mustCallSuper
@override
Completer<T> manageCompleter<T>(Completer<T> completer) {
  _throwOnInvalidCall('manageCompleter', 'completer', completer);
  _logManageMessage(completer);

  var disposable = ManagedDisposer(() async {
    if (!completer.isCompleted) {
      completer.completeError(ObjectDisposedException());
    }
  });
  _internalDisposables.add(disposable);

  completer.future
      // Log and remove disposable regardless of the outcome of the future.
      .whenComplete(() {
        if (!_isDisposedOrDisposing) {
          _logUnmanageMessage(completer);
          _internalDisposables.remove(disposable);
        }
      })
      // Transform the stream from Future<T> to Future<Null> to allow catchError to exist.
      // Without this, catchError would throw a TypeError at runtime.
      .then((value) => null)
      // Avoid uncaught errors in the event loop spawned by the whenComplete.
      // Without this we will see runtime errors in tests and the developer console.
      .catchError((_) => null);

  return completer;
}