releaseDocument method
Writes what documentId is holding, closes it, and lets go of it.
The other half of the lazy open. A document stays in memory once it has been asked for, so a server that never calls this holds every room it has ever served. Call it when the last client of a room disconnects.
The id stays in the catalog — the document is still served, it is just not in memory. That is what separates this from removeDocument. The next getDocument reads it back from the storage.
Nothing here is lost: the persistence flushes before the storage closes.
One rule: the document must not be in use. This disposes it, and a
caller holding the CRDTDocument a previous getDocument handed back
would be writing into a disposed one. Every session in this package
re-reads through getDocument, so calling this between two requests is
safe; idleAfter takes the same risk on a timer, which is why it should
be far longer than a request takes.
Implementation
Future<void> releaseDocument(String documentId) {
_idleTimers.remove(documentId)?.cancel();
final opening = _open.remove(documentId);
if (opening == null) {
// Nothing open, but a release may still be running: join it rather than
// reporting a document as let go while its last write is in flight.
return _releasing[documentId] ?? Future<void>.value();
}
final releasing = _release(documentId, opening);
_releasing[documentId] = releasing;
return releasing;
}