unregisterSingleton method

Future<void> unregisterSingleton(
  1. Subsystem instance, {
  2. bool preserveTypeContext = false,
})

Unregisters and disposes a singleton instance. Safe and idempotent: does nothing if the instance is not registered.

Implementation

Future<void> unregisterSingleton(
  Subsystem instance, {
  bool preserveTypeContext = false,
}) async {
  final removed = _singletonsByDesc.remove(instance.classDesc);
  if (removed == null) {
    return;
  }

  await instance._disposeSubsystem();

  if (_singletonsByDesc.isEmpty && !preserveTypeContext) {
    await typeDesc._beginDisposeTypeContext();
    SubsystemInstanceRegistry._registriesByTypeDesc.remove(typeDesc);
  }
}