resetLazySingleton<T> method

void resetLazySingleton<T>(
  1. {Object instance,
  2. String instanceName,
  3. void disposingFunction(
    1. T
    )}
)

Clears the instance of a lazy singleton registered type, being able to call the factory function on the first call of get on that type.

Implementation

void resetLazySingleton<T>(
    {Object instance,
    String instanceName,
    void Function(T) disposingFunction}) {
  if (instance != null) {
    var registeredInstance = _factories.values
        .followedBy(_factoriesByName.values)
        .where((x) => identical(x.instance, instance));

    throwIf(
      registeredInstance.isEmpty,
      ArgumentError.value(instance,
          'There is no object type ${instance.runtimeType} registered in GetIt'),
    );

    assert(registeredInstance.length == 1,
        'One Instance more than once in getIt registered');

    throwIf(
      registeredInstance.first.factoryType != _ServiceFactoryType.lazy,
      ArgumentError.value(instance,
          'There is no type ${instance.runtimeType} registered as LazySingleton in GetIt'),
    );

    var _factory = registeredInstance.first;
    disposingFunction?.call(_factory.instance);
    _factory.instance = null;
  } else {
    throwIf(
      (((const Object() is! T) && instanceName != null)),
      ArgumentError(
          'GetIt: You have to provide either a type OR a name not both.'),
    );

    var registeredFactory = _factoriesByName[instanceName] ?? _factories[T];

    throwIf(
      (registeredFactory == null),
      ArgumentError(
          'No Type registered ${T.toString()} or instance Name must not be null'),
    );
    throwIfNot(registeredFactory.factoryType == _ServiceFactoryType.lazy,
      ArgumentError.value(instance,
          'There is no type ${T.toString()} registered as LazySingleton in GetIt'),
    );
    if (instanceName == null) {
      disposingFunction?.call(get<T>());
      _factories[T]?.instance = null;
    } else {
      disposingFunction?.call(get(instanceName));
      _factoriesByName[T]?.instance = null;
    }
  }
}