locateAsync method
Implementation
Future<T> locateAsync() async {
if (type == SpotType.factory) {
return locator!(_spot);
}
if (type == SpotType.singleton) {
return locate(); // Delegate to sync method
}
// Async singleton
if (instance != null) return instance!;
// Guard against re-entrant initialization
if (_initializing) {
// If already initializing, wait for the initialization future
if (_initializationFuture != null) {
return await _initializationFuture!;
}
throw SpotException(
'Re-entrant async initialization detected for $T. '
'This usually indicates a circular dependency.'
);
}
// Mark as initializing and create instance
_initializing = true;
try {
// Start initialization
_initializationFuture = asyncLocator!(_spot);
instance = await _initializationFuture!;
_initializationFuture = null;
return instance!;
} finally {
_initializing = false;
}
}