clear method

Future clear()

Removes all instances and builders from the container.

After this, the container is empty.

Implementation

Future clear() async {
  int count = 0;
  forEachProvider((_, __) => count++);
  if (count > 0) {
    log.info("Clearing container");
    forEachProvider((type, provider) => log.fine("Clearing $type"));
  }
  final values = [..._namedProviders.values];
  _namedProviders.clear();
  _state = ContainerState.Destroying;

  for (final instances in values) {
    for (final instance in instances.values) {
      if (instance.object is LifecycleAware) {
        log.fine(
            "\t - Destroying singleton: ${instance.runtimeType}, lifecycle: ${instance.object is LifecycleAware}");
        try {
          await (instance.object as LifecycleAware)
              .onLifecycleEvent(LifecycleEvent.stop);
        } catch (e) {
          log.severe("Error shutting down ${instance.object}");
          // not going to rethrow because we don't want to mess with other providers
        }
      }
    }
  }
  _state = ContainerState.Building;
}