delete<S> method

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

Delete registered Class Instance S (or tag) and, closes any open controllers DisposableInterface, cleans up the memory

Deletes the Instance<S>, cleaning the memory and closes any open controllers (DisposableInterface).

  • tag Optional "tag" used to register the Instance
  • key For internal usage, is the processed key used to register the Instance. don't use it unless you know what you are doing.
  • force Will delete an Instance even if marked as permanent.

Implementation

bool delete<S>({String? tag, String? key, bool force = false}) {
  final newKey = key ?? _getKey(S, tag);

  if (!_singl.containsKey(newKey)) {
    Get.log('Instance "$newKey" already removed.', isError: true);
    return false;
  }

  final dep = _singl[newKey];

  if (dep == null) return false;

  void cleanFactory(_InstanceBuilderFactory<Object?> factory) {
    if (factory.lateRemove != null) {
      cleanFactory(factory.lateRemove!);
    }
    final i = factory.dependency;
    if (i is GetLifeCycleMixin) {
      i.onDelete();
      Get.log('"$newKey" onDelete() called');
    }
  }

  if (dep.permanent && !force) {
    Get.log(
      // ignore: lines_longer_than_80_chars
      '"$newKey" has been marked as permanent, SmartManagement is not authorized to delete it.',
      isError: true,
    );
    return false;
  }
  final primaryDependency = dep.dependency;

  if (primaryDependency is GetxServiceMixin && !force) {
    return false;
  }

  cleanFactory(dep);

  if (dep.fenix) {
    dep.dependency = null;
    dep.isInit = false;
    dep.lateRemove = null;
    return true;
  } else {
    _singl.remove(newKey);
    if (_singl.containsKey(newKey)) {
      Get.log('Error removing object "$newKey"', isError: true);
    } else {
      Get.log('"$newKey" deleted from memory');
    }
    return true;
  }
}