register<T> method

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

Adds an object to the registry, registered objects are closed when close is invoked.

When close is invoked on this instance, onClose will be invoked with object and object will be removed. This method returns object. This allows for concise registration and allocation:

Example: 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.


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