unregisterChildT method

Result<Option<DI>> unregisterChildT(
  1. Type type, {
  2. Entity groupEntity = const DefaultEntity(),
})
inherited

Type-keyed variant of unregisterChild.

Implementation

Result<Option<DI>> unregisterChildT(
  Type type, {
  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.');
  final unregistered =
      container.unregisterK(TypeEntity(Lazy, [type]), groupEntity: g);
  assert(
    unregistered is Sync,
    'unregisterChildT: child unregister resolved Async. Children must be '
    'registered as Sync lazies — see SupportsChildrenMixin.registerChild.',
  );
  return switch (unregistered) {
    // Pattern-match instead of `raw as Lazy<DI>` so adversarial misuse
    // (a non-`Lazy<DI>` written into the children container) surfaces
    // as Err rather than a thrown TypeError.
    Sync(value: Ok(value: Some(value: final raw))) => switch (raw) {
        final Lazy<DI> lazy => _eagerLazyDI(lazy).map((di) => Some(di)),
        _ => Err<Option<DI>>(
            'unregisterChildT: registered value under type $type is not a '
            'Lazy<DI> (got ${raw.runtimeType}). The children container '
            'appears to hold non-DI entries — use `container.unregister...` '
            'directly for those.',
          ),
      },
    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>>(
        'unregisterChildT: child unregister resolved Async, but children '
        'must be Sync. This is a programming-error contract violation.',
      ),
  };
}