mirror of
https://github.com/pese-git/cherrypick.git
synced 2026-01-23 21:13:35 +00:00
refactor code
This commit is contained in:
@@ -17,6 +17,11 @@ import 'package:source_gen/source_gen.dart';
|
|||||||
import 'bind_parameters_spec.dart';
|
import 'bind_parameters_spec.dart';
|
||||||
import 'metadata_utils.dart';
|
import 'metadata_utils.dart';
|
||||||
|
|
||||||
|
enum BindingType {
|
||||||
|
instance,
|
||||||
|
provide;
|
||||||
|
}
|
||||||
|
|
||||||
/// ---------------------------------------------------------------------------
|
/// ---------------------------------------------------------------------------
|
||||||
/// BindSpec -- describes a binding specification generated for a dependency.
|
/// BindSpec -- describes a binding specification generated for a dependency.
|
||||||
///
|
///
|
||||||
@@ -59,7 +64,7 @@ class BindSpec {
|
|||||||
final List<BindParameterSpec> parameters;
|
final List<BindParameterSpec> parameters;
|
||||||
|
|
||||||
/// Binding type: 'instance' or 'provide' (@instance or @provide)
|
/// Binding type: 'instance' or 'provide' (@instance or @provide)
|
||||||
final String bindingType; // 'instance' | 'provide'
|
final BindingType bindingType; // 'instance' | 'provide'
|
||||||
|
|
||||||
/// True if the method is asynchronous and uses instance binding (Future)
|
/// True if the method is asynchronous and uses instance binding (Future)
|
||||||
final bool isAsyncInstance;
|
final bool isAsyncInstance;
|
||||||
@@ -119,11 +124,11 @@ class BindSpec {
|
|||||||
final fnArgs = parameters.map((p) => p.generateArg(paramVar)).join(', ');
|
final fnArgs = parameters.map((p) => p.generateArg(paramVar)).join(', ');
|
||||||
final multiLine = fnArgs.length > 60 || fnArgs.contains('\n');
|
final multiLine = fnArgs.length > 60 || fnArgs.contains('\n');
|
||||||
switch (bindingType) {
|
switch (bindingType) {
|
||||||
case 'instance':
|
case BindingType.instance:
|
||||||
return isAsyncInstance
|
return isAsyncInstance
|
||||||
? '.toInstanceAsync(($fnArgs) => $methodName($fnArgs))'
|
? '.toInstanceAsync(($fnArgs) => $methodName($fnArgs))'
|
||||||
: '.toInstance(($fnArgs) => $methodName($fnArgs))';
|
: '.toInstance(($fnArgs) => $methodName($fnArgs))';
|
||||||
case 'provide':
|
case BindingType.provide:
|
||||||
default:
|
default:
|
||||||
if (isAsyncProvide) {
|
if (isAsyncProvide) {
|
||||||
return multiLine
|
return multiLine
|
||||||
@@ -142,11 +147,11 @@ class BindSpec {
|
|||||||
final argsStr = parameters.map((p) => p.generateArg()).join(', ');
|
final argsStr = parameters.map((p) => p.generateArg()).join(', ');
|
||||||
final multiLine = argsStr.length > 60 || argsStr.contains('\n');
|
final multiLine = argsStr.length > 60 || argsStr.contains('\n');
|
||||||
switch (bindingType) {
|
switch (bindingType) {
|
||||||
case 'instance':
|
case BindingType.instance:
|
||||||
return isAsyncInstance
|
return isAsyncInstance
|
||||||
? '.toInstanceAsync($methodName($argsStr))'
|
? '.toInstanceAsync($methodName($argsStr))'
|
||||||
: '.toInstance($methodName($argsStr))';
|
: '.toInstance($methodName($argsStr))';
|
||||||
case 'provide':
|
case BindingType.provide:
|
||||||
default:
|
default:
|
||||||
if (isAsyncProvide) {
|
if (isAsyncProvide) {
|
||||||
return multiLine
|
return multiLine
|
||||||
@@ -210,7 +215,8 @@ class BindSpec {
|
|||||||
element: method,
|
element: method,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
final bindingType = hasInstance ? 'instance' : 'provide';
|
final bindingType =
|
||||||
|
hasInstance ? BindingType.instance : BindingType.provide;
|
||||||
|
|
||||||
// -- Extract inner type for Future<T> and set async flags.
|
// -- Extract inner type for Future<T> and set async flags.
|
||||||
bool isAsyncInstance = false;
|
bool isAsyncInstance = false;
|
||||||
@@ -218,8 +224,8 @@ class BindSpec {
|
|||||||
final futureInnerType = _extractFutureInnerType(returnType);
|
final futureInnerType = _extractFutureInnerType(returnType);
|
||||||
if (futureInnerType != null) {
|
if (futureInnerType != null) {
|
||||||
returnType = futureInnerType;
|
returnType = futureInnerType;
|
||||||
if (bindingType == 'instance') isAsyncInstance = true;
|
if (bindingType == BindingType.instance) isAsyncInstance = true;
|
||||||
if (bindingType == 'provide') isAsyncProvide = true;
|
if (bindingType == BindingType.provide) isAsyncProvide = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return BindSpec(
|
return BindSpec(
|
||||||
|
|||||||
Reference in New Issue
Block a user