isRegistered<T extends Object> method

bool isRegistered<T extends Object>({
  1. Entity groupEntity = const DefaultEntity(),
  2. bool traverse = true,
  3. @Deprecated('Cycle detection is internal; this parameter is now ignored.') Set<DI>? visited,
})

Returns whether a dependency keyed under exact type T is registered in groupEntity. Strict: a Lazy<T> registration does NOT count here — callers wanting that must check isRegistered<Lazy<T>>(). Mirrors the keying contract of the registry's insert/remove.

Iterative DFS over the parent chain so deep hierarchies cannot blow the call stack. Cycle-safe via the internal seen set.

Implementation

bool isRegistered<T extends Object>({
  Entity groupEntity = const DefaultEntity(),
  bool traverse = true,
  @Deprecated('Cycle detection is internal; this parameter is now ignored.')
  Set<DI>? visited,
}) {
  assert(T != Object, 'T must be specified and cannot be Object.');
  final seen = <DI>{};
  final stack = <(DI, Entity)>[
    (this as DI, groupEntity.preferOverDefault(focusGroup)),
  ];
  while (stack.isNotEmpty) {
    final (di, g) = stack.removeLast();
    if (!seen.add(di)) continue;
    if (di.registry.containsDependency<T>(groupEntity: g)) return true;
    if (!traverse) break;
    // Push in reverse so first parent is popped first (sibling order).
    final parentList = di.parents.toList(growable: false);
    for (var i = parentList.length - 1; i >= 0; i--) {
      final parent = parentList[i];
      stack.add((parent, g.preferOverDefault(parent.focusGroup)));
    }
  }
  return false;
}