unregisterChild method

Result<Option<DI>> unregisterChild({
  1. Entity groupEntity = const DefaultEntity(),
})
inherited

Children are registered as Lazy<DI>, so unregister must remove the lazy key — unregister<DI> would not match under the strict-keying contract.

Implementation

Result<Option<DI>> unregisterChild({
  Entity groupEntity = const DefaultEntity(),
}) {
  final g = groupEntity.preferOverDefault(focusGroup);
  final container = switch (childrenContainer) {
    Some(value: final c) => c,
    None() => null,
  };
  if (container == null) return Err('No child container registered.');
  // Children are registered as Sync lazies (see [registerChild]). Any
  // other shape (Async unregister / Async lazy singleton) is a contract
  // violation surfaced as Err on the returned Result so the error
  // propagates through the standard Result pipeline rather than throwing.
  // Debug-only `assert` highlights the misuse early during development.
  // unregister IS side-effectful — call it once and reuse.
  final unregistered = container.unregister<Lazy<DI>>(groupEntity: g);
  assert(
    unregistered is Sync,
    'unregisterChild: child unregister resolved Async. Children must be '
    'registered as Sync lazies — see SupportsChildrenMixin.registerChild.',
  );
  return switch (unregistered) {
    Sync(value: Ok(value: Some(value: final lazy))) =>
      _eagerLazyDI(lazy).map((di) => Some(di)),
    Sync(value: Ok(value: None())) => const Ok(None()),
    Sync(value: Err(:final error, :final stackTrace)) =>
      Err<Option<DI>>(error, stackTrace: stackTrace),
    Async() => Err<Option<DI>>(
        'unregisterChild: child unregister resolved Async, but children '
        'must be Sync. This is a programming-error contract violation.',
      ),
  };
}