close method

  1. @override
Future<void> close()
override

Flushes and closes every open document.

The catalog is left as it is: it describes what this server serves, and that is still true after a shutdown.

Asking this registry for a document afterwards throws a StateError.

Implementation

@override
Future<void> close() async {
  _closed = true;
  for (final timer in _idleTimers.values) {
    timer.cancel();
  }
  _idleTimers.clear();

  // A loop, not one pass: an open started before `_closed` was set is still
  // in flight, and it installs its entry when it lands. A single pass over
  // the map would leave that document writing for the life of the process.
  while (_open.isNotEmpty || _releasing.isNotEmpty) {
    final releasing = List<Future<void>>.of(_releasing.values);
    final opening = List<Future<_OpenDocument>>.of(_open.values);
    _open.clear();

    for (final release in releasing) {
      try {
        await release;
      } catch (error, stack) {
        _onError?.call(error, stack);
      }
    }
    for (final open in opening) {
      try {
        await (await open).dispose();
      } catch (error, stack) {
        // One document that cannot be written must not keep the others open.
        _onError?.call(error, stack);
      }
    }
  }

  await _snapshots.close();
}