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 isAsyncInstance;
|
||||||
|
|
||||||
|
final bool isAsyncProvide;
|
||||||
|
|
||||||
BindSpec({
|
BindSpec({
|
||||||
required this.returnType,
|
required this.returnType,
|
||||||
required this.methodName,
|
required this.methodName,
|
||||||
@@ -88,6 +90,7 @@ class BindSpec {
|
|||||||
this.named,
|
this.named,
|
||||||
required this.bindingType,
|
required this.bindingType,
|
||||||
required this.isAsyncInstance,
|
required this.isAsyncInstance,
|
||||||
|
required this.isAsyncProvide,
|
||||||
});
|
});
|
||||||
|
|
||||||
/// Формирует dart-код для биндинга, например:
|
/// Формирует dart-код для биндинга, например:
|
||||||
@@ -111,11 +114,19 @@ class BindSpec {
|
|||||||
} else {
|
} else {
|
||||||
provide = '.toInstance($methodName($argsStr))';
|
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 {
|
} else {
|
||||||
provide = (argsStr.length > 60 || argsStr.contains('\n'))
|
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()' : '';
|
||||||
@@ -159,13 +170,16 @@ class BindSpec {
|
|||||||
}
|
}
|
||||||
final bindingType = hasInstance ? 'instance' : 'provide';
|
final bindingType = hasInstance ? 'instance' : 'provide';
|
||||||
|
|
||||||
// Новый кусок — для async instance возвращаем базовый тип без Future<>
|
// --- Новый участок: извлекаем внутренний тип из Future<> и выставляем флаги
|
||||||
bool isAsyncInstance = false;
|
bool isAsyncInstance = false;
|
||||||
if (bindingType == 'instance' && returnType.startsWith('Future<')) {
|
bool isAsyncProvide = false;
|
||||||
|
|
||||||
|
if (returnType.startsWith('Future<')) {
|
||||||
final futureMatch = RegExp(r'^Future<(.+)>$').firstMatch(returnType);
|
final futureMatch = RegExp(r'^Future<(.+)>$').firstMatch(returnType);
|
||||||
if (futureMatch != null) {
|
if (futureMatch != null) {
|
||||||
returnType = futureMatch.group(1)!.trim();
|
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,
|
parameters: params,
|
||||||
bindingType: bindingType,
|
bindingType: bindingType,
|
||||||
isAsyncInstance: isAsyncInstance,
|
isAsyncInstance: isAsyncInstance,
|
||||||
|
isAsyncProvide: isAsyncProvide,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,6 +24,14 @@ abstract class AppModule extends Module {
|
|||||||
@named('baseUrl')
|
@named('baseUrl')
|
||||||
String baseUrl() => "https://google.com";
|
String baseUrl() => "https://google.com";
|
||||||
|
|
||||||
|
@provide()
|
||||||
|
@named('Delay1')
|
||||||
|
Future<int> delay1() => Future.value(1000);
|
||||||
|
|
||||||
|
@provide()
|
||||||
|
@named('Size1')
|
||||||
|
Future<int> size1() async => 10;
|
||||||
|
|
||||||
@provide()
|
@provide()
|
||||||
@singleton()
|
@singleton()
|
||||||
@named('dio')
|
@named('dio')
|
||||||
|
|||||||
Reference in New Issue
Block a user