getK<T extends Object> method

Option<Resolvable<T>> getK<T extends Object>(
  1. Entity typeEntity, {
  2. Entity groupEntity = const DefaultEntity(),
  3. bool traverse = true,
})
inherited

Retrieves the dependency.

Implementation

Option<Resolvable<T>> getK<T extends Object>(
  Entity typeEntity, {
  Entity groupEntity = const DefaultEntity(),
  bool traverse = true,
}) {
  final g = groupEntity.preferOverDefault(focusGroup);
  final option = getDependencyK<T>(
    typeEntity,
    groupEntity: g,
    traverse: traverse,
  );
  // Same pattern as `_di_base.dart::get` — collapse the
  // Option<Result<Dependency<T>>> structurally; each branch is exhaustive.
  return switch (option) {
    None() => const None(),
    Some(value: Err(:final error, :final stackTrace)) => Some(
        Sync<T>.err(Err<T>(error, stackTrace: stackTrace)),
      ),
    Some(value: Ok(value: final dep)) => switch (dep.value) {
        Sync<T>() => Some(dep.value),
        Async<T>(value: final fut) => Some(
            Async<T>(
              () => fut.then((e) {
                // Pattern-match instead of `.unwrap()`. Throwing here is
                // the *intentional* channel for Err propagation: the
                // surrounding `Async()` constructor catches the throw and
                // converts it into Err on its own `.value` channel. This
                // is the df_safer_dart convention for "propagate error
                // through the Async pipeline".
                final value = switch (e) {
                  Ok(value: final v) => v,
                  Err(:final error, :final stackTrace) =>
                    throw Err<T>(error, stackTrace: stackTrace),
                };
                registry.removeDependencyK(typeEntity, groupEntity: g).end();
                final metadata = dep.metadata.map(
                  (m) => m.copyWith(
                    preemptivetypeEntity: TypeEntity(Sync, [typeEntity]),
                  ),
                );
                registerDependencyK(
                  dependency:
                      Dependency(Sync.okValue(value), metadata: metadata),
                  checkExisting: false,
                ).end();
                return value;
              }),
            ),
          ),
      },
  };
}