mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +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:
@@ -1,5 +1,4 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:cherrypick/cherrypick.dart';
|
||||
import 'package:meta/meta.dart';
|
||||
|
||||
@@ -12,33 +11,34 @@ class AppModule extends Module {
|
||||
}
|
||||
|
||||
class FeatureModule extends Module {
|
||||
bool isMock;
|
||||
final bool isMock;
|
||||
|
||||
FeatureModule({required this.isMock});
|
||||
|
||||
@override
|
||||
void builder(Scope currentScope) {
|
||||
bind<DataRepository>()
|
||||
.withName('networkRepo')
|
||||
.toProvide(
|
||||
() => NetworkDataRepository(
|
||||
currentScope.resolve<ApiClient>(
|
||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
||||
),
|
||||
),
|
||||
)
|
||||
.singleton();
|
||||
// DataRepository remains async for demonstration
|
||||
bind<DataRepository>().withName('networkRepo').toProvideAsync(
|
||||
() async {
|
||||
// Using synchronous resolve for ApiClient
|
||||
final apiClient = currentScope.resolve<ApiClient>(
|
||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
||||
);
|
||||
return NetworkDataRepository(apiClient);
|
||||
},
|
||||
).singleton();
|
||||
|
||||
bind<DataBloc>().toProvideWithParams(
|
||||
(param) => DataBloc(
|
||||
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
||||
param,
|
||||
),
|
||||
bind<DataBloc>().toProvideAsyncWithParams(
|
||||
(param) async {
|
||||
final dataRepository = await currentScope.resolveAsync<DataRepository>(
|
||||
named: 'networkRepo');
|
||||
return DataBloc(dataRepository, param);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
Future<void> main() async {
|
||||
final scope = openRootScope().installModules([
|
||||
AppModule(),
|
||||
]);
|
||||
@@ -47,18 +47,22 @@ void main() async {
|
||||
.openSubScope('featureScope')
|
||||
.installModules([FeatureModule(isMock: true)]);
|
||||
|
||||
final dataBloc = subScope.resolve<DataBloc>(params: 'PARAMETER');
|
||||
dataBloc.data.listen((d) => print('Received data: $d'),
|
||||
onError: (e) => print('Error: $e'), onDone: () => print('DONE'));
|
||||
try {
|
||||
final dataBloc = await subScope.resolveAsync<DataBloc>(params: 'PARAMETER');
|
||||
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;
|
||||
|
||||
final String param;
|
||||
|
||||
@@ -105,7 +109,6 @@ abstract class ApiClient {
|
||||
}
|
||||
|
||||
class ApiClientMock implements ApiClient {
|
||||
@override
|
||||
Future sendRequest({
|
||||
@required String? url,
|
||||
String? token,
|
||||
@@ -117,7 +120,6 @@ class ApiClientMock implements ApiClient {
|
||||
}
|
||||
|
||||
class ApiClientImpl implements ApiClient {
|
||||
@override
|
||||
Future sendRequest({
|
||||
@required String? url,
|
||||
String? token,
|
||||
|
||||
@@ -15,6 +15,10 @@ enum Mode { simple, instance, providerInstance, providerInstanceWithParams }
|
||||
|
||||
typedef ProviderWithParams<T> = T Function(dynamic params);
|
||||
|
||||
typedef AsyncProvider<T> = Future<T> Function();
|
||||
|
||||
typedef AsyncProviderWithParams<T> = Future<T> Function(dynamic params);
|
||||
|
||||
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
||||
/// ENG: The Binding<T> class configures the settings for the instance.
|
||||
///
|
||||
@@ -24,6 +28,9 @@ class Binding<T> {
|
||||
late String _name;
|
||||
T? _instance;
|
||||
T? Function()? _provider;
|
||||
AsyncProvider<T>? asyncProvider;
|
||||
AsyncProviderWithParams<T>? asyncProviderWithParams;
|
||||
|
||||
ProviderWithParams<T>? _providerWithParams;
|
||||
late bool _isSingleton = false;
|
||||
late bool _isNamed = false;
|
||||
@@ -94,6 +101,16 @@ class Binding<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра через провайдер [value].
|
||||
/// ENG: Initialization instance via provider [value].
|
||||
///
|
||||
/// return [Binding]
|
||||
Binding<T> toProvideAsync(AsyncProvider<T> provider) {
|
||||
_mode = Mode.providerInstance;
|
||||
asyncProvider = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра через провайдер [value] c динамическим параметром.
|
||||
/// ENG: Initialization instance via provider [value] with a dynamic param.
|
||||
///
|
||||
@@ -104,6 +121,16 @@ class Binding<T> {
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляра через асинхронный провайдер [value] с динамическим параметром.
|
||||
/// ENG: Initializes the instance via async provider [value] with a dynamic param.
|
||||
///
|
||||
/// return [Binding]
|
||||
Binding<T> toProvideAsyncWithParams(AsyncProviderWithParams<T> provider) {
|
||||
_mode = Mode.providerInstanceWithParams;
|
||||
asyncProviderWithParams = provider;
|
||||
return this;
|
||||
}
|
||||
|
||||
/// RU: Инициализация экземляпяра как сингелтон [value].
|
||||
/// ENG: Initialization instance as a singelton [value].
|
||||
///
|
||||
|
||||
@@ -132,4 +132,45 @@ class Scope {
|
||||
// 2 Поиск зависимостей в родительском скоупе
|
||||
return _parentScope?.tryResolve(named: named, params: params);
|
||||
}
|
||||
|
||||
/// RU: Асинхронно возвращает найденную зависимость, определенную параметром типа [T].
|
||||
/// Выдает [StateError], если зависимость не может быть разрешена.
|
||||
/// Если хотите получить [null], если зависимость не может быть найдена, используйте [tryResolveAsync].
|
||||
/// return - возвращает объект типа [T] or [StateError]
|
||||
///
|
||||
/// ENG: Asynchronously returns the found dependency specified by the type parameter [T].
|
||||
/// Throws [StateError] if the dependency cannot be resolved.
|
||||
/// If you want to get [null] if the dependency cannot be found, use [tryResolveAsync] instead.
|
||||
/// return - returns an object of type [T] or [StateError]
|
||||
///
|
||||
Future<T> resolveAsync<T>({String? named, dynamic params}) async {
|
||||
var resolved = await tryResolveAsync<T>(named: named, params: params);
|
||||
if (resolved != null) {
|
||||
return resolved;
|
||||
} else {
|
||||
throw StateError(
|
||||
'Can\'t resolve async dependency `$T`. Maybe you forget register it?');
|
||||
}
|
||||
}
|
||||
|
||||
Future<T?> tryResolveAsync<T>({String? named, dynamic params}) async {
|
||||
if (_modulesList.isNotEmpty) {
|
||||
for (var module in _modulesList) {
|
||||
for (var binding in module.bindingSet) {
|
||||
if (binding.key == T &&
|
||||
((!binding.isNamed && named == null) ||
|
||||
(binding.isNamed && named == binding.name))) {
|
||||
if (binding.asyncProvider != null) {
|
||||
return await binding.asyncProvider?.call();
|
||||
}
|
||||
|
||||
if (binding.asyncProviderWithParams != null) {
|
||||
return await binding.asyncProviderWithParams!(params);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return _parentScope?.tryResolveAsync(named: named, params: params);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user