registerLazySingleton<T> method

void registerLazySingleton<T>(
  1. T factory(), {
  2. String? moduleId,
  3. bool allowOverwrite = false,
})

Registers a Lazy Singleton.

The factory function will only be executed the first time the dependency is requested.

factory A function that creates the instance. moduleId Optional ID of the module that owns this registration. allowOverwrite If true, allows replacing an existing registration.

Implementation

void registerLazySingleton<T>(
  T Function() factory, {
  String? moduleId,
  bool allowOverwrite = false,
}) {
  if (_registrations.containsKey(T) && !allowOverwrite) {
    AirLogger.warning(
      'Dependency already registered',
      context: {'type': T.toString()},
    );
    throw DependencyAlreadyRegisteredException(T);
  }

  _registrations[T] = _LazySingletonDependency<T>(factory);
  _registrationOwners[T] = moduleId;

  AirLogger.debug(
    'Registered LazySingleton',
    context: {'type': T.toString(), 'module': moduleId},
  );
}