buildSingletonRegisterFun method
Implementation
Code buildSingletonRegisterFun(DependencyConfig dep) {
var funcReferName;
var asFactory = true;
final hasAsyncDep = hasAsyncDependency(dep, _dependencies);
if (dep.isAsync || hasAsyncDep) {
funcReferName = 'singletonAsync';
} else if (dep.dependsOn.isNotEmpty) {
funcReferName = 'singletonWithDependencies';
} else {
asFactory = false;
funcReferName = 'singleton';
}
final instanceBuilder =
dep.isFromModule ? _buildInstanceForModule(dep) : _buildInstance(dep);
final registerExpression = _ghRefer.property(funcReferName).call([
asFactory
? Method((b) => b
..lambda = true
..modifier = hasAsyncDep ? MethodModifier.async : null
..body = instanceBuilder.code).closure
: instanceBuilder
], {
if (dep.instanceName != null)
'instanceName': literalString(dep.instanceName!),
if (dep.dependsOn.isNotEmpty)
'dependsOn': literalList(
dep.dependsOn.map(
(e) => typeRefer(e, _targetFile),
),
),
if (dep.environments.isNotEmpty)
'registerFor': literalSet(
dep.environments.map((e) => refer('_$e')),
),
if (dep.signalsReady != null)
'signalsReady': literalBool(dep.signalsReady!),
if (dep.preResolve == true) 'preResolve': literalBool(true),
if (dep.disposeFunction != null)
'dispose': _getDisposeFunctionAssignment(dep.disposeFunction!)
}, [
typeRefer(dep.type, _targetFile)
]);
return dep.preResolve
? registerExpression.awaited.statement
: registerExpression.statement;
}