prune method
void
prune(
- void onRemove(
- T
Method to remove instances that is no longer used
Called every time MvvmInstance.dispose called for instance
Implementation
void prune(void Function(T) onRemove) {
final removeFunctions = <Function>[];
_references.forEach((scope, refs) {
if (scope == BaseScopes.global || scope == BaseScopes.unique) {
return;
}
refs.forEach((key, value) {
final indicesToRemove = <int>[];
for (final (index, element) in value.indexed) {
if (element != 0) {
continue;
}
final object = getObjectInScope(
type: key.toString(),
scopeId: scope,
index: index,
);
indicesToRemove.add(index);
if (object == null) {
return;
}
removeFunctions.add(
() {
if (value.isEmpty) {
removeObjectReferenceInScope(
scopeId: scope,
type: key,
);
} else {
indicesToRemove.forEach(value.removeAt);
// checking again
if (value.isEmpty) {
removeObjectReferenceInScope(
scopeId: scope,
type: key,
);
}
}
removeObjectInScope(
type: key.toString(),
scopeId: scope,
index: index,
onRemove: onRemove,
);
},
);
}
});
});
for (final element in removeFunctions) {
element.call();
}
}