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. 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

//  ///
//  /// - [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.

/// 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`.
bool delete<S>({String? tag, String? key, bool force = false}) {
  final newKey = key ?? _getKey(S, tag);

  if (!_singl.containsKey(newKey)) {
    debugPrint('Instance "$newKey" already removed.');
    return false;
  }

  final dep = _singl[newKey];

  if (dep == null) return false;

  final _InstanceBuilderFactory builder;
  if (dep.isDirty) {
    builder = dep.lateRemove ?? dep;
  } else {
    builder = dep;
  }

  if (builder.permanent && !force) {
    debugPrint(
      // ignore: lines_longer_than_80_chars
      '"$newKey" has been marked as permanent, IntelligentManagement is not authorized to delete it.',
    );
    return false;
  }
  final i = builder.dependency;

  if (i is JuneServiceMixin && !force) {
    return false;
  }

  if (i is JuneLifeCycleMixin) {
    i.onDelete();
    debugPrint('"$newKey" onDelete() called');
  }

  if (builder.fenix) {
    builder.dependency = null;
    builder.isInit = false;
    return true;
  } else {
    if (dep.lateRemove != null) {
      dep.lateRemove = null;
      debugPrint('"$newKey" deleted from memory');
      return false;
    } else {
      _singl.remove(newKey);
      if (_singl.containsKey(newKey)) {
        debugPrint('Error removing object "$newKey"');
      } else {
        debugPrint('"$newKey" deleted from memory');
      }
      return true;
    }
  }
}