buildSingletonRegisterFun method

Code buildSingletonRegisterFun(
  1. DependencyConfig dep
)

Implementation

Code buildSingletonRegisterFun(DependencyConfig dep) {
  String funcReferName;
  final hasAsyncDep = dependencies.hasAsyncDependency(dep);
  if (dep.isAsync || hasAsyncDep) {
    funcReferName = 'singletonAsync';
  } else if (dep.dependsOn.isNotEmpty) {
    funcReferName = 'singletonWithDependencies';
  } else {
    funcReferName = 'singleton';
  }

  final instanceBuilder =
      dep.isFromModule ? _buildInstanceForModule(dep) : _buildInstance(dep);
  final instanceBuilderCode = _buildInstanceBuilderCode(instanceBuilder, dep);
  final registerExpression = _ghLocalRefer.property(funcReferName).call([
    Method((b) => b
      ..lambda = instanceBuilderCode is! Block
      ..modifier = hasAsyncDep ? MethodModifier.async : null
      ..body = instanceBuilderCode).closure
  ], {
    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;
}