manageAndReturnTypedDisposable<T extends Disposable> method

  1. @mustCallSuper
  2. @override
T manageAndReturnTypedDisposable <T extends Disposable>(T disposable)
@mustCallSuper, override

Automatically dispose another object when this object is disposed.

This method is an extension to manageAndReturnDisposable and returns the passed in Disposable as its original type in addition to handling its disposal. The method should be used when a variable is set and should conditionally be managed for disposal. The most common case will be dealing with optional parameters:

 class MyDisposable extends Disposable {
   // This object also extends disposable
   MyObject _internal;

   MyDisposable({MyObject optional}) {
     // If optional is injected, we should not manage it.
     // If we create our own internal reference we should manage it.
     _internal = optional ??
         manageAndReturnTypedDisposable(new MyObject());
   }

   // ...
 }

The parameter may not be null.

Implementation

@mustCallSuper
@override
T manageAndReturnTypedDisposable<T extends Disposable>(T disposable) {
  _throwOnInvalidCall('manageAndReturnDisposable', 'disposable', disposable);
  manageDisposable(disposable);

  return disposable;
}