register<T> method

T register <T>(T object, FutureOr onClose(T object))

Register object to be destroyed when your application is stopped.

When close is invoked on this instance, onClose will be invoked with object. The registered object is removed so that subsequent invocations of close do not attempt to release a resource again.

This method returns object. This allows for concise registration and allocation:

Example: var controller = ServiceRegistry.defaultInstance.register( new StreamController(), (c) => c.close());

If object has already been registered, this method does nothing and onClose will only be invoked once.

Implementation

T register<T>(T object, FutureOr onClose(T object)) {
  if (_registrations.any((r) => identical(r.object, object))) {
    return object;
  }
  _registrations.add(_ServiceRegistration<T>(object, onClose));
  return object;
}