Skip to content

Commit

Permalink
fix: ensure EnsembleConfigService is initialized before accessing config
Browse files Browse the repository at this point in the history
  • Loading branch information
sharjeelyunus committed Feb 21, 2025
1 parent b026253 commit 1f8df89
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
8 changes: 5 additions & 3 deletions modules/ensemble/lib/ensemble.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class Ensemble extends WithEnsemble with EnsembleRouteObserver {
try {
// this code block is guaranteed to run at most once
await StorageManager().init();
await SecretsStore().initialize();
Device().initDeviceInfo();
AppInfo().initPackageInfo(_config);
_completer!.complete();
Expand Down Expand Up @@ -139,9 +140,10 @@ class Ensemble extends WithEnsemble with EnsembleRouteObserver {
if (_config != null) {
return Future<EnsembleConfig>.value(_config);
}
// Intialize the config service to get `ensemble-config.yaml` file to access the configuration using static property as `EnsembleConfigService.config`
await EnsembleConfigService.initialize();
await SecretsStore().initialize();
// Initialize the config service to get `ensemble-config.yaml` file to access the configuration using static property as `EnsembleConfigService.config`
if (!EnsembleConfigService.isInitialized) {
await EnsembleConfigService.initialize();
}

// get the config YAML
final YamlMap yamlMap = EnsembleConfigService.config;
Expand Down
12 changes: 11 additions & 1 deletion modules/ensemble/lib/framework/ensemble_config_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ import 'package:yaml/yaml.dart';
// EnsembleConfigService is a service that provides access to the ensemble-config.yaml file through static property once initialized
class EnsembleConfigService {
static YamlMap? _config;
static bool _isInitialized = false;

static Future<void> initialize() async {
final yamlString = await rootBundle.loadString('ensemble/ensemble-config.yaml');
_config = loadYaml(yamlString);
_isInitialized = true;
}

static YamlMap get config {
if (_config == null) {
throw StateError('EnsembleConfig not initialized');
// if config is not available, load the default config
_config = loadYaml('''
definitions:
from: 'ensemble'
''');
_isInitialized =
true;
}
return _config!;
}

static bool get isInitialized => _isInitialized;
}
3 changes: 3 additions & 0 deletions modules/ensemble/lib/framework/secrets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class SecretsStore with Invokable {

// add local overrides
try {
if (!EnsembleConfigService.isInitialized) {
await EnsembleConfigService.initialize();
}
String provider = EnsembleConfigService.config["definitions"]?['from'];
if (provider == 'local') {
String path =
Expand Down

0 comments on commit 1f8df89

Please sign in to comment.