delete<S> method
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).
If the registration was replaced while the previous route was still
disposing (the superseded factory is kept in lateRemove), only the
superseded instance is disposed and the fresh registration stays
alive, in which case this returns false.
tagOptional "tag" used to register the InstancekeyFor internal usage, is the processed key used to register the Instance. don't use it unless you know what you are doing.forceWill delete an Instance even if marked aspermanent.
Implementation
bool delete<S>({String? tag, String? key, bool force = false}) {
final newKey = key ?? _getKey(S, tag);
if (!_singletons.containsKey(newKey)) {
Get.log('Instance "$newKey" already removed.', isError: true);
return false;
}
final dep = _singletons[newKey];
if (dep == null) return false;
if (dep.lateRemove != null) {
// The registration was replaced while a previous route was still
// disposing. Each pending disposal peels off the oldest superseded
// factory; the live registration is only removed once no superseded
// factory remains.
var parent = dep;
while (parent.lateRemove!.lateRemove != null) {
parent = parent.lateRemove!;
}
final stale = parent.lateRemove!;
final i = stale.dependency;
// Note: the `GetxServiceMixin` guard is intentionally NOT applied
// here. It exists to keep LIVE services alive across casual deletes;
// a superseded factory in `lateRemove` has already been replaced by
// a newer registration, so keeping it would only leak the stale
// instance and make the live one undeletable (the guard would fire
// on every subsequent delete before the chain is cleared).
if (i is GetLifeCycleMixin) {
i.onDelete();
Get.log('"$newKey" onDelete() called');
}
parent.lateRemove = null;
Get.log('"$newKey" deleted from memory');
return false;
}
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 i = dep.dependency;
if (i is GetxServiceMixin && !force) {
return false;
}
if (i is GetLifeCycleMixin) {
i.onDelete();
Get.log('"$newKey" onDelete() called');
}
if (dep.fenix) {
dep.dependency = null;
dep.isInit = false;
dep.isDirty = false;
return true;
} else {
_singletons.remove(newKey);
if (_singletons.containsKey(newKey)) {
Get.log('Error removing object "$newKey"', isError: true);
} else {
Get.log('"$newKey" deleted from memory');
}
return true;
}
}