feat: Add async dependency resolution and enhance example

- Implemented async provider methods `toProvideAsync` and `toProvideAsyncWithParams` in `Binding` class, allowing asynchronous initialization with dynamic parameters.
- Added typedefs `AsyncProvider<T>` and `AsyncProviderWithParams<T>` for better type clarity with async operations.
- Introduced async resolution methods `resolveAsync` and `tryResolveAsync` in `Scope` for resolving asynchronous dependencies.
- Updated example in `main.dart` to demonstrate async dependency resolution capabilities.
  - Modified `FeatureModule` to utilize async providers for `DataRepository` and `DataBloc`.
  - Replaced synchronous resolution with `resolveAsync` where applicable.
  - Handled potential errors in dependency resolution with try-catch.
- Removed unnecessary whitespace for cleaner code formatting.
This commit is contained in:
Sergey Penkovsky
2025-05-06 15:54:35 +03:00
parent de995228a5
commit 7a5880e436

View File

@@ -37,28 +37,32 @@ class FeatureModule extends Module {
}
}
void main() async {
final scope = openRootScope().installModules([
AppModule(),
]);
Future<void> main() async {
try {
final scope = openRootScope().installModules([
AppModule(),
]);
final subScope = scope
.openSubScope("featureScope")
.installModules([FeatureModule(isMock: true)]);
final subScope = scope
.openSubScope("featureScope")
.installModules([FeatureModule(isMock: true)]);
// Asynchronous instance resolution
final dataBloc = await subScope.resolveAsync<DataBloc>();
dataBloc.data.listen((d) => print('Received data: $d'),
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
// Asynchronous instance resolution
final dataBloc = await subScope.resolveAsync<DataBloc>();
dataBloc.data.listen((d) => print('Received data: $d'),
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
await dataBloc.fetchData();
await dataBloc.fetchData();
} catch (e) {
print('Error resolving dependency: $e');
}
}
class DataBloc {
final DataRepository _dataRepository;
Stream<String> get data => _dataController.stream;
final StreamController<String> _dataController = StreamController.broadcast();
Stream<String> get data => _dataController.stream;
DataBloc(this._dataRepository);