buildLazyRegisterFun method
Implementation
Code buildLazyRegisterFun(DependencyConfig dep) {
var funcReferName;
Map<String, Reference> factoryParams = {};
final hasAsyncDep = hasAsyncDependency(dep, _dependencies);
final isOrHasAsyncDep = dep.isAsync || hasAsyncDep;
if (dep.injectableType == InjectableType.factory) {
final hasFactoryParams = dep.dependencies.any((d) => d.isFactoryParam);
if (hasFactoryParams) {
funcReferName = isOrHasAsyncDep ? 'factoryParamAsync' : 'factoryParam';
factoryParams.addAll(_resolveFactoryParams(dep));
} else {
funcReferName = isOrHasAsyncDep ? 'factoryAsync' : 'factory';
}
} else if (dep.injectableType == InjectableType.lazySingleton) {
funcReferName = isOrHasAsyncDep ? 'lazySingletonAsync' : 'lazySingleton';
}
throwIf(funcReferName == null, 'Injectable type is not supported');
final registerExpression = _ghRefer.property(funcReferName).call([
Method(
(b) => b
..lambda = true
..modifier = hasAsyncDep ? MethodModifier.async : null
..requiredParameters.addAll(
factoryParams.keys.map(
(name) => Parameter((b) => b.name = name),
),
)
..body = dep.isFromModule
? _buildInstanceForModule(dep).code
: _buildInstance(dep).code,
).closure
], {
if (dep.instanceName != null)
'instanceName': literalString(dep.instanceName!),
if (dep.environments.isNotEmpty == true)
'registerFor': literalSet(
dep.environments.map((e) => refer('_$e')),
),
if (dep.preResolve == true) 'preResolve': literalBool(true),
if (dep.disposeFunction != null)
'dispose': _getDisposeFunctionAssignment(dep.disposeFunction!)
}, [
typeRefer(dep.type, _targetFile),
...factoryParams.values.map((p) => p.type)
]);
return dep.preResolve
? registerExpression.awaited.statement
: registerExpression.statement;
}