mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
feat: implement async mode for instance/provide annotations
This commit is contained in:
@@ -80,6 +80,8 @@ class BindSpec {
|
||||
|
||||
final bool isAsyncInstance;
|
||||
|
||||
final bool isAsyncProvide;
|
||||
|
||||
BindSpec({
|
||||
required this.returnType,
|
||||
required this.methodName,
|
||||
@@ -88,6 +90,7 @@ class BindSpec {
|
||||
this.named,
|
||||
required this.bindingType,
|
||||
required this.isAsyncInstance,
|
||||
required this.isAsyncProvide,
|
||||
});
|
||||
|
||||
/// Формирует dart-код для биндинга, например:
|
||||
@@ -111,11 +114,19 @@ class BindSpec {
|
||||
} else {
|
||||
provide = '.toInstance($methodName($argsStr))';
|
||||
}
|
||||
} else {
|
||||
// provide
|
||||
if (isAsyncProvide) {
|
||||
// Асинхронная фабрика
|
||||
provide = (argsStr.length > 60 || argsStr.contains('\n'))
|
||||
? '.toProvideAsync(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
|
||||
: '.toProvideAsync(() => $methodName($argsStr))';
|
||||
} else {
|
||||
provide = (argsStr.length > 60 || argsStr.contains('\n'))
|
||||
? '.toProvide(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
|
||||
: '.toProvide(() => $methodName($argsStr))';
|
||||
}
|
||||
}
|
||||
|
||||
final namePart = named != null ? ".withName('$named')" : '';
|
||||
final singletonPart = isSingleton ? '.singleton()' : '';
|
||||
@@ -159,13 +170,16 @@ class BindSpec {
|
||||
}
|
||||
final bindingType = hasInstance ? 'instance' : 'provide';
|
||||
|
||||
// Новый кусок — для async instance возвращаем базовый тип без Future<>
|
||||
// --- Новый участок: извлекаем внутренний тип из Future<> и выставляем флаги
|
||||
bool isAsyncInstance = false;
|
||||
if (bindingType == 'instance' && returnType.startsWith('Future<')) {
|
||||
bool isAsyncProvide = false;
|
||||
|
||||
if (returnType.startsWith('Future<')) {
|
||||
final futureMatch = RegExp(r'^Future<(.+)>$').firstMatch(returnType);
|
||||
if (futureMatch != null) {
|
||||
returnType = futureMatch.group(1)!.trim();
|
||||
isAsyncInstance = true;
|
||||
if (bindingType == 'instance') isAsyncInstance = true;
|
||||
if (bindingType == 'provide') isAsyncProvide = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +191,7 @@ class BindSpec {
|
||||
parameters: params,
|
||||
bindingType: bindingType,
|
||||
isAsyncInstance: isAsyncInstance,
|
||||
isAsyncProvide: isAsyncProvide,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,6 +24,14 @@ abstract class AppModule extends Module {
|
||||
@named('baseUrl')
|
||||
String baseUrl() => "https://google.com";
|
||||
|
||||
@provide()
|
||||
@named('Delay1')
|
||||
Future<int> delay1() => Future.value(1000);
|
||||
|
||||
@provide()
|
||||
@named('Size1')
|
||||
Future<int> size1() async => 10;
|
||||
|
||||
@provide()
|
||||
@singleton()
|
||||
@named('dio')
|
||||
|
||||
Reference in New Issue
Block a user