delete<S> method

Future<bool> delete<S>({
  1. String? tag,
  2. bool force = false,
})

Removes a dependency from the scope.

If the dependency is currently instantiated and implements LevitScopeDisposable, its LevitScopeDisposable.onClose method will be called.

Set force to true to delete dependencies marked as permanent.

Returns true if the dependency was found and deleted.

Implementation

Future<bool> delete<S>({String? tag, bool force = false}) async {
  _ensureUsable();
  final key = _getKey<S>(tag);
  final keyString = key.debugString;

  if (_aliases.containsKey(key)) {
    final canonicalKey = _aliases[key]!;
    final info = _registry[canonicalKey];
    _removeAlias(key);
    if (info != null) {
      _notifyDelete(keyString, info, 'deleteAlias');
    }
    return true;
  }

  final info = _registry[key];
  if (info == null) return false;

  if (info.permanent && !force) return false;

  _notifyDelete(keyString, info, 'delete');
  _removeCanonicalBinding(key);

  if (info.isInstantiated) {
    await _disposeAll(<Object?>[info.instance]);
  }
  return true;
}