HHive constructor

HHive({
  1. HHImmutableConfig? config,
  2. String? env,
})

Creates or retrieves an HHive instance for the given environment. Either config or env must be provided.

Implementation

factory HHive({HHImmutableConfig? config, String? env}) {
  if (config == null && env == null) {
    throw ArgumentError(
      'Either config or env must be provided to create HHive instance.',
    );
  }

  if (config != null && env != null) {
    if (config.env != env) {
      throw ArgumentError(
        'Provided config env (${config.env}) does not match provided env ($env).',
      );
    }
  }

  final targetEnv = config?.env ?? env!;

  // Check if HHive instance already exists for this env
  if (_instances.containsKey(targetEnv)) {
    return _instances[targetEnv]!;
  }

  // If only env is provided, get or create config
  final finalConfig = config ?? HHImmutableConfig.instances[targetEnv];

  if (finalConfig == null) {
    throw ArgumentError(
      'No config found for env "$targetEnv". Please create one first by providing a config.',
    );
  }

  // Create new HHive instance
  final instance = HHive._internal(finalConfig);
  _instances[targetEnv] = instance;
  return instance;
}