mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
feat: generate instance async code
This commit is contained in:
@@ -78,6 +78,8 @@ class BindSpec {
|
|||||||
|
|
||||||
final String bindingType; // 'instance' | 'provide'
|
final String bindingType; // 'instance' | 'provide'
|
||||||
|
|
||||||
|
final bool isAsyncInstance;
|
||||||
|
|
||||||
BindSpec({
|
BindSpec({
|
||||||
required this.returnType,
|
required this.returnType,
|
||||||
required this.methodName,
|
required this.methodName,
|
||||||
@@ -85,6 +87,7 @@ class BindSpec {
|
|||||||
required this.parameters,
|
required this.parameters,
|
||||||
this.named,
|
this.named,
|
||||||
required this.bindingType,
|
required this.bindingType,
|
||||||
|
required this.isAsyncInstance,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Формирует dart-код для биндинга, например:
|
/// Формирует dart-код для биндинга, например:
|
||||||
@@ -100,12 +103,19 @@ class BindSpec {
|
|||||||
// Если аргументов много или они длинные — разбиваем вызов на несколько строк
|
// Если аргументов много или они длинные — разбиваем вызов на несколько строк
|
||||||
//final needMultiline = argsStr.length > 60 || argsStr.contains('\n');
|
//final needMultiline = argsStr.length > 60 || argsStr.contains('\n');
|
||||||
|
|
||||||
// Важно: для .toInstance всегда просто method(), для .toProvide нужна лямбда
|
String provide;
|
||||||
final provide = bindingType == 'instance'
|
if (bindingType == 'instance') {
|
||||||
? '.toInstance($methodName($argsStr))'
|
// Добавляем async вариант для Future<T>
|
||||||
: (argsStr.length > 60 || argsStr.contains('\n')
|
if (isAsyncInstance) {
|
||||||
|
provide = '.toInstanceAsync($methodName($argsStr))';
|
||||||
|
} else {
|
||||||
|
provide = '.toInstance($methodName($argsStr))';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
provide = (argsStr.length > 60 || argsStr.contains('\n'))
|
||||||
? '.toProvide(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
|
? '.toProvide(\n${' ' * (indent + 2)}() => $methodName($argsStr))'
|
||||||
: '.toProvide(() => $methodName($argsStr))');
|
: '.toProvide(() => $methodName($argsStr))';
|
||||||
|
}
|
||||||
|
|
||||||
final namePart = named != null ? ".withName('$named')" : '';
|
final namePart = named != null ? ".withName('$named')" : '';
|
||||||
final singletonPart = isSingleton ? '.singleton()' : '';
|
final singletonPart = isSingleton ? '.singleton()' : '';
|
||||||
@@ -121,7 +131,7 @@ class BindSpec {
|
|||||||
|
|
||||||
/// Создаёт спецификацию биндинга (BindSpec) из метода класса-модуля
|
/// Создаёт спецификацию биндинга (BindSpec) из метода класса-модуля
|
||||||
static BindSpec fromMethod(MethodElement method) {
|
static BindSpec fromMethod(MethodElement method) {
|
||||||
final returnType = method.returnType.getDisplayString();
|
var returnType = method.returnType.getDisplayString();
|
||||||
|
|
||||||
final methodName = method.displayName;
|
final methodName = method.displayName;
|
||||||
// Проверим, помечен ли метод аннотацией @singleton
|
// Проверим, помечен ли метод аннотацией @singleton
|
||||||
@@ -149,6 +159,16 @@ class BindSpec {
|
|||||||
}
|
}
|
||||||
final bindingType = hasInstance ? 'instance' : 'provide';
|
final bindingType = hasInstance ? 'instance' : 'provide';
|
||||||
|
|
||||||
|
// Новый кусок — для async instance возвращаем базовый тип без Future<>
|
||||||
|
bool isAsyncInstance = false;
|
||||||
|
if (bindingType == 'instance' && returnType.startsWith('Future<')) {
|
||||||
|
final futureMatch = RegExp(r'^Future<(.+)>$').firstMatch(returnType);
|
||||||
|
if (futureMatch != null) {
|
||||||
|
returnType = futureMatch.group(1)!.trim();
|
||||||
|
isAsyncInstance = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return BindSpec(
|
return BindSpec(
|
||||||
returnType: returnType,
|
returnType: returnType,
|
||||||
methodName: methodName,
|
methodName: methodName,
|
||||||
@@ -156,6 +176,7 @@ class BindSpec {
|
|||||||
named: named,
|
named: named,
|
||||||
parameters: params,
|
parameters: params,
|
||||||
bindingType: bindingType,
|
bindingType: bindingType,
|
||||||
|
isAsyncInstance: isAsyncInstance,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,14 @@ abstract class AppModule extends Module {
|
|||||||
@instance()
|
@instance()
|
||||||
int timeout() => 1000;
|
int timeout() => 1000;
|
||||||
|
|
||||||
|
@instance()
|
||||||
|
@named('Delay')
|
||||||
|
Future<int> delay() => Future.value(1000);
|
||||||
|
|
||||||
|
@instance()
|
||||||
|
@named('Size')
|
||||||
|
Future<int> size() async => 10;
|
||||||
|
|
||||||
@instance()
|
@instance()
|
||||||
@named('baseUrl')
|
@named('baseUrl')
|
||||||
String baseUrl() => "https://google.com";
|
String baseUrl() => "https://google.com";
|
||||||
|
|||||||
Reference in New Issue
Block a user