mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
Add parameter to provider
This commit is contained in:
@@ -22,15 +22,17 @@ class FeatureModule extends Module {
|
|||||||
.withName('networkRepo')
|
.withName('networkRepo')
|
||||||
.toProvide(
|
.toProvide(
|
||||||
() => NetworkDataRepository(
|
() => NetworkDataRepository(
|
||||||
currentScope.resolve<ApiClient>(
|
currentScope.resolve<ApiClient>(
|
||||||
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
named: isMock ? 'apiClientMock' : 'apiClientImpl',
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.singleton();
|
.singleton();
|
||||||
bind<DataBloc>().toProvide(
|
|
||||||
() => DataBloc(
|
bind<DataBloc>().toProvideWithParam(
|
||||||
|
(param) => DataBloc(
|
||||||
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
currentScope.resolve<DataRepository>(named: 'networkRepo'),
|
||||||
|
param,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -45,7 +47,7 @@ void main() async {
|
|||||||
.openSubScope('featureScope')
|
.openSubScope('featureScope')
|
||||||
.installModules([FeatureModule(isMock: true)]);
|
.installModules([FeatureModule(isMock: true)]);
|
||||||
|
|
||||||
final dataBloc = subScope.resolve<DataBloc>();
|
final dataBloc = subScope.resolve<DataBloc>(param: 'PARAMETER');
|
||||||
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'));
|
||||||
|
|
||||||
@@ -58,11 +60,13 @@ class DataBloc {
|
|||||||
Stream<String> get data => _dataController.stream;
|
Stream<String> get data => _dataController.stream;
|
||||||
final StreamController<String> _dataController = StreamController.broadcast();
|
final StreamController<String> _dataController = StreamController.broadcast();
|
||||||
|
|
||||||
DataBloc(this._dataRepository);
|
final String param;
|
||||||
|
|
||||||
|
DataBloc(this._dataRepository, this.param);
|
||||||
|
|
||||||
Future<void> fetchData() async {
|
Future<void> fetchData() async {
|
||||||
try {
|
try {
|
||||||
_dataController.sink.add(await _dataRepository.getData());
|
_dataController.sink.add(await _dataRepository.getData(param));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_dataController.sink.addError(e);
|
_dataController.sink.addError(e);
|
||||||
}
|
}
|
||||||
@@ -74,7 +78,7 @@ class DataBloc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
abstract class DataRepository {
|
abstract class DataRepository {
|
||||||
Future<String> getData();
|
Future<String> getData(String param);
|
||||||
}
|
}
|
||||||
|
|
||||||
class NetworkDataRepository implements DataRepository {
|
class NetworkDataRepository implements DataRepository {
|
||||||
@@ -84,26 +88,42 @@ class NetworkDataRepository implements DataRepository {
|
|||||||
NetworkDataRepository(this._apiClient);
|
NetworkDataRepository(this._apiClient);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> getData() async => await _apiClient.sendRequest(
|
Future<String> getData(String param) async => await _apiClient.sendRequest(
|
||||||
url: 'www.google.com', token: _token, requestBody: {'type': 'data'});
|
url: 'www.google.com',
|
||||||
|
token: _token,
|
||||||
|
requestBody: {'type': 'data'},
|
||||||
|
param: param);
|
||||||
}
|
}
|
||||||
|
|
||||||
abstract class ApiClient {
|
abstract class ApiClient {
|
||||||
Future sendRequest({@required String url, String token, Map requestBody});
|
Future sendRequest({
|
||||||
|
@required String url,
|
||||||
|
String token,
|
||||||
|
Map requestBody,
|
||||||
|
String param,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiClientMock implements ApiClient {
|
class ApiClientMock implements ApiClient {
|
||||||
@override
|
@override
|
||||||
Future sendRequest(
|
Future sendRequest({
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
@required String? url,
|
||||||
return 'Local Data';
|
String? token,
|
||||||
|
Map? requestBody,
|
||||||
|
String? param,
|
||||||
|
}) async {
|
||||||
|
return 'Local Data $param';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ApiClientImpl implements ApiClient {
|
class ApiClientImpl implements ApiClient {
|
||||||
@override
|
@override
|
||||||
Future sendRequest(
|
Future sendRequest({
|
||||||
{@required String? url, String? token, Map? requestBody}) async {
|
@required String? url,
|
||||||
return 'Network data';
|
String? token,
|
||||||
|
Map? requestBody,
|
||||||
|
String? param,
|
||||||
|
}) async {
|
||||||
|
return 'Network data $param';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
/// limitations under the License.
|
/// limitations under the License.
|
||||||
///
|
///
|
||||||
|
|
||||||
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE }
|
enum Mode { SIMPLE, INSTANCE, PROVIDER_INSTANCE, PROVIDER_WITH_PARAMS_INSTANCE }
|
||||||
|
|
||||||
|
typedef ProviderWithParam<T> = T Function(dynamic param);
|
||||||
|
|
||||||
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
/// RU: Класс Binding<T> настраивает параметры экземпляра.
|
||||||
/// ENG: The Binding<T> class configures the settings for the instance.
|
/// ENG: The Binding<T> class configures the settings for the instance.
|
||||||
@@ -22,6 +24,7 @@ class Binding<T> {
|
|||||||
late String _name;
|
late String _name;
|
||||||
T? _instance;
|
T? _instance;
|
||||||
T? Function()? _provider;
|
T? Function()? _provider;
|
||||||
|
ProviderWithParam<T>? _providerWithParam;
|
||||||
late bool _isSingleton = false;
|
late bool _isSingleton = false;
|
||||||
late bool _isNamed = false;
|
late bool _isNamed = false;
|
||||||
|
|
||||||
@@ -91,6 +94,16 @@ class Binding<T> {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RU: Инициализация экземляпяра через провайдер [value] c динамическим параметром.
|
||||||
|
/// ENG: Initialization instance via provider [value] with a dynamic param.
|
||||||
|
///
|
||||||
|
/// return [Binding]
|
||||||
|
Binding<T> toProvideWithParam(ProviderWithParam<T> value) {
|
||||||
|
_mode = Mode.PROVIDER_WITH_PARAMS_INSTANCE;
|
||||||
|
_providerWithParam = value;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// RU: Инициализация экземляпяра как сингелтон [value].
|
/// RU: Инициализация экземляпяра как сингелтон [value].
|
||||||
/// ENG: Initialization instance as a singelton [value].
|
/// ENG: Initialization instance as a singelton [value].
|
||||||
///
|
///
|
||||||
@@ -117,4 +130,12 @@ class Binding<T> {
|
|||||||
}
|
}
|
||||||
return _provider?.call();
|
return _provider?.call();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// RU: Поиск экземпляра с параметром.
|
||||||
|
/// ENG: Resolve instance with param.
|
||||||
|
///
|
||||||
|
/// return [T]
|
||||||
|
T? providerWithParam(dynamic param) {
|
||||||
|
return _providerWithParam?.call(param);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,8 +86,8 @@ class Scope {
|
|||||||
/// If you want to get [null] if the dependency cannot be found then use [tryResolve] instead
|
/// If you want to get [null] if the dependency cannot be found then use [tryResolve] instead
|
||||||
/// return - returns an object of type [T] or [StateError]
|
/// return - returns an object of type [T] or [StateError]
|
||||||
///
|
///
|
||||||
T resolve<T>({String? named}) {
|
T resolve<T>({String? named, dynamic param}) {
|
||||||
var resolved = tryResolve<T>(named: named);
|
var resolved = tryResolve<T>(named: named, param: param);
|
||||||
if (resolved != null) {
|
if (resolved != null) {
|
||||||
return resolved;
|
return resolved;
|
||||||
} else {
|
} else {
|
||||||
@@ -99,7 +99,7 @@ class Scope {
|
|||||||
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
/// RU: Возвращает найденную зависимость типа [T] или null, если она не может быть найдена.
|
||||||
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
/// ENG: Returns found dependency of type [T] or null if it cannot be found.
|
||||||
///
|
///
|
||||||
T? tryResolve<T>({String? named}) {
|
T? tryResolve<T>({String? named, dynamic param}) {
|
||||||
// 1 Поиск зависимости по всем модулям текущего скоупа
|
// 1 Поиск зависимости по всем модулям текущего скоупа
|
||||||
if (_modulesList.isNotEmpty) {
|
if (_modulesList.isNotEmpty) {
|
||||||
for (var module in _modulesList) {
|
for (var module in _modulesList) {
|
||||||
@@ -112,6 +112,12 @@ class Scope {
|
|||||||
return binding.instance;
|
return binding.instance;
|
||||||
case Mode.PROVIDER_INSTANCE:
|
case Mode.PROVIDER_INSTANCE:
|
||||||
return binding.provider;
|
return binding.provider;
|
||||||
|
case Mode.PROVIDER_WITH_PARAMS_INSTANCE:
|
||||||
|
if (param == null) {
|
||||||
|
throw StateError(
|
||||||
|
'Param is null. Maybe you forget pass it');
|
||||||
|
}
|
||||||
|
return binding.providerWithParam(param);
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user