From 1bdcc71534ed222a0ce6bb5672dbc23ad18195b4 Mon Sep 17 00:00:00 2001 From: Sergey Penkovsky Date: Wed, 21 May 2025 11:05:18 +0300 Subject: [PATCH] feat: implement async mode for instance/provide annotations --- .../lib/module_generator.dart | 27 ++++++++++++++----- examples/postly/lib/di/app_module.dart | 8 ++++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/cherrypick_generator/lib/module_generator.dart b/cherrypick_generator/lib/module_generator.dart index 06d98d3..d8aeac7 100644 --- a/cherrypick_generator/lib/module_generator.dart +++ b/cherrypick_generator/lib/module_generator.dart @@ -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-код для биндинга, например: @@ -112,9 +115,17 @@ class BindSpec { provide = '.toInstance($methodName($argsStr))'; } } else { - provide = (argsStr.length > 60 || argsStr.contains('\n')) - ? '.toProvide(\n${' ' * (indent + 2)}() => $methodName($argsStr))' - : '.toProvide(() => $methodName($argsStr))'; + // 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')" : ''; @@ -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, ); } } diff --git a/examples/postly/lib/di/app_module.dart b/examples/postly/lib/di/app_module.dart index 6ceb8d1..6b45cd7 100644 --- a/examples/postly/lib/di/app_module.dart +++ b/examples/postly/lib/di/app_module.dart @@ -24,6 +24,14 @@ abstract class AppModule extends Module { @named('baseUrl') String baseUrl() => "https://google.com"; + @provide() + @named('Delay1') + Future delay1() => Future.value(1000); + + @provide() + @named('Size1') + Future size1() async => 10; + @provide() @singleton() @named('dio')