# preInstantiateSingletons method

1. @override

Future<void> preInstantiateSingletons()
override

Pre-instantiates all singleton pods.

This method creates all singleton pods upfront, which can improve first-request performance at the cost of slower application startup.

## Usage Example:

    // During application startup for better runtime performance
    await factory.preInstantiateSingletons();
    print('All singletons have been instantiated');

@return A Future that completes when all singletons are instantiated

## Implementation

    @override
    Future<void> preInstantiateSingletons() async {
      if (_logger.getIsTraceEnabled()) {
        _logger.trace("Starting pre-instantiation of singleton pods in $runtimeType. Scanning ${getNumberOfPodDefinitions()} pod definitions.");
      }

      final podNames = List<String>.from(getDefinitionNames());

      for (final podName in podNames) {
        try {
          final def = getLocalMergedPodDefinition(podName);
          if (!def.isAbstractAndNoFactory() && def.scope.isSingleton) {
            final notLazy = def.lifecycle.isLazy != null ? !def.lifecycle.isLazy! : false;

            if (!notLazy) {
              try {
                if (await isPodProvider(podName)) {
                  final ppName = podProviderName(podName);
                  await getPod(ppName);
                } else {
                  await getPod(podName);
                }
              } on PodCurrentlyInCreationException catch (_) {
                if (_logger.getIsTraceEnabled()) {
                  _logger.trace("Pod '$podName' is currently in creation by another thread. Skipping pre-instantiation");
                }
              } catch (e) {
                if (_logger.getIsWarnEnabled()) {
                  _logger.warn("Failed to pre-instantiate singleton '$podName'", error: e);
                }

                rethrow;
              }
            } else {
              if (_logger.getIsTraceEnabled()) {
                _logger.trace("Pod '$podName' is marked as a lazy singleton. Skipping pre-instantiation");
              }
            }
          } else {
            if (_logger.getIsTraceEnabled()) {
              _logger.trace(
                "Skipping pod '$podName': definition is not a singleton, it's abstract and has no factory constructor "
                "or factory method. Scope='${def.scope}', lazy='${def.lifecycle.isLazy}'. "
                "If this pod should be created, provide a factory method/constructor or mark it non-abstract."
              );
            }

            continue;
          }
        } on NoSuchPodDefinitionException catch (_) {
          if(_logger.getIsWarnEnabled()) {
            _logger.warn("Could not find pod definition '$podName' while pre-instantiating singletons");
          }
        }
      }

      // Call on ready when singletons are initialized
      for (final name in podNames) {
        final instance = await getSingleton(name);

        if (instance is SmartInitializingSingleton) {
          if (logger.getIsTraceEnabled()) {
            logger.trace("Invoking onSingletonReady() for '$name'");
          }

          await instance.onSingletonReady();

          if (logger.getIsTraceEnabled()) {
            logger.trace("Successfully invoked onSingletonReady() for '$name'");
          }
        }
      }
    }