locate method

T locate()

Implementation

T locate() {
  if (type == SpotType.asyncSingleton) {
    throw SpotException(
      'Cannot synchronously resolve async singleton $T. '
      'Use await Spot.spotAsync<$T>() instead.'
    );
  }

  if (type == SpotType.factory) {
    return locator!(_spot);
  }

  // Thread-safe singleton initialization
  // Note: Dart is single-threaded per isolate, but this prevents re-entrant
  // initialization issues and prepares for potential multi-isolate scenarios

  // Fast path: check if already initialized
  if (instance != null) return instance!;

  // Guard against re-entrant initialization (circular dependencies)
  if (_initializing) {
    throw SpotException(
      'Re-entrant initialization detected for $T. '
      'This usually indicates a circular dependency.'
    );
  }

  // Mark as initializing and create instance
  _initializing = true;
  try {
    instance = locator!(_spot);
    return instance!;
  } finally {
    _initializing = false;
  }
}