shutdown method

Future<void> shutdown()

Runs shutdown, then attempts every cleanup and resource disposal even when a preceding stage fails. Reports failures in CleanupException.

Implementation

Future<void> shutdown() async {
  if (!_finalized || _shutdown) return;
  _shutdown = true;
  final failures = <({Object error, StackTrace stackTrace})>[];
  try {
    runSchedule(Schedules.shutdown);
  } catch (error, stackTrace) {
    failures.add((error: error, stackTrace: stackTrace));
  }
  for (var i = _cleanups.length - 1; i >= 0; i--) {
    try {
      await _cleanups[i]();
    } catch (error, stackTrace) {
      failures.add((error: error, stackTrace: stackTrace));
    }
  }
  _cleanups.clear();
  try {
    world.resources.disposeAll();
  } catch (error, stackTrace) {
    failures.add((error: error, stackTrace: stackTrace));
  }
  if (failures.isNotEmpty) throw CleanupException(failures);
}