impr: add binding resolver class.

This commit is contained in:
yarashevich_kv
2025-05-19 21:02:33 +03:00
committed by Sergey Penkovsky
parent 1682ed9c08
commit 2cba7f2675
7 changed files with 237 additions and 224 deletions

View File

@@ -1,5 +1,6 @@
import 'dart:async';
import 'package:cherrypick/cherrypick.dart';
import 'package:meta/meta.dart';
class AppModule extends Module {
@override
@@ -17,19 +18,28 @@ class FeatureModule extends Module {
@override
void builder(Scope currentScope) {
// Using toProvideAsync for async initialization
bind<DataRepository>().withName("networkRepo").toProvideAsync(() async {
bind<DataRepository>()
.withName("networkRepo")
.toProvideWithParams((params) async {
print('REPO PARAMS: $params');
final client = await Future.delayed(
Duration(milliseconds: 100),
() => currentScope.resolve<ApiClient>(
named: isMock ? "apiClientMock" : "apiClientImpl"));
Duration(milliseconds: 1000),
() => currentScope.resolve<ApiClient>(
named: isMock ? "apiClientMock" : "apiClientImpl",
),
);
return NetworkDataRepository(client);
}).singleton();
// Asynchronous initialization of DataBloc
bind<DataBloc>().toProvideAsync(
bind<DataBloc>().toProvide(
() async {
final repo = await currentScope.resolveAsync<DataRepository>(
named: "networkRepo");
named: "networkRepo",
params: 'Some params',
);
return DataBloc(repo);
},
);
@@ -38,18 +48,19 @@ class FeatureModule extends Module {
Future<void> main() async {
try {
final scope = openRootScope().installModules([
AppModule(),
]);
final scope = openRootScope().installModules([AppModule()]);
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();
} catch (e) {