From 7a5880e4362d03eb67028a531db7b21333f1ea8c Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Tue, 6 May 2025 15:54:35 +0300 Subject: [PATCH] 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` and `AsyncProviderWithParams` 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. --- cherrypick/example/bin/main.dart | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cherrypick/example/bin/main.dart b/cherrypick/example/bin/main.dart index d797948..2703065 100644 --- a/cherrypick/example/bin/main.dart +++ b/cherrypick/example/bin/main.dart @@ -37,28 +37,32 @@ class FeatureModule extends Module { } } -void main() async { - final scope = openRootScope().installModules([ - AppModule(), - ]); +Future 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.data.listen((d) => print('Received data: $d'), - onError: (e) => print('Error: $e'), onDone: () => print('DONE')); + // Asynchronous instance resolution + final dataBloc = await subScope.resolveAsync(); + 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 get data => _dataController.stream; final StreamController _dataController = StreamController.broadcast(); + Stream get data => _dataController.stream; DataBloc(this._dataRepository);