mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-24 05:25:19 +00:00
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:
@@ -37,28 +37,32 @@ class FeatureModule extends Module {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() async {
|
Future<void> main() async {
|
||||||
final scope = openRootScope().installModules([
|
try {
|
||||||
AppModule(),
|
final scope = openRootScope().installModules([
|
||||||
]);
|
AppModule(),
|
||||||
|
]);
|
||||||
|
|
||||||
final subScope = scope
|
final subScope = scope
|
||||||
.openSubScope("featureScope")
|
.openSubScope("featureScope")
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
// Asynchronous instance resolution
|
// Asynchronous instance resolution
|
||||||
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
final dataBloc = await subScope.resolveAsync<DataBloc>();
|
||||||
dataBloc.data.listen((d) => print('Received data: $d'),
|
dataBloc.data.listen((d) => print('Received data: $d'),
|
||||||
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
||||||
|
|
||||||
await dataBloc.fetchData();
|
await dataBloc.fetchData();
|
||||||
|
} catch (e) {
|
||||||
|
print('Error resolving dependency: $e');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class DataBloc {
|
class DataBloc {
|
||||||
final DataRepository _dataRepository;
|
final DataRepository _dataRepository;
|
||||||
|
|
||||||
Stream<String> get data => _dataController.stream;
|
|
||||||
final StreamController<String> _dataController = StreamController.broadcast();
|
final StreamController<String> _dataController = StreamController.broadcast();
|
||||||
|
Stream<String> get data => _dataController.stream;
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
DataBloc(this._dataRepository);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user