Files
outline/shared/collaboration/documentName.ts
T
Tom Moor cca21437f5 Flatten collaborative state of large documents (#14587)
* feat: Flatten collaborative state of large documents

Replace edit history in large collaborative state with fresh state once a
document is idle with no open sessions, and when state is over the maximum
length on save. Each replacement increments a state generation that is part of
the collaboration room name, so old state never merges into the new state.

* fix: Refuse to load state of another generation and lock editor during reset

* Remove excessive tests

* fix: Reset rooms on all instances and check state generation during authentication

* refactor: Schedule state flattening only for changed rooms, from PersistenceExtension

* fix: Refuse API text updates applied to state of an earlier generation
2026-11-29 15:49:34 -05:00

49 lines
1.4 KiB
TypeScript

/** The parts of a collaborative document name. */
export interface DocumentNameParts {
/** The ID of the document. */
documentId: string;
/** The generation of the document's collaborative state. */
stateGeneration: number;
}
/**
* Build the name of the collaboration room for a document. Each state
* generation has its own room, so state from an earlier generation is never
* merged into a later one.
*
* @param documentId the ID of the document.
* @param stateGeneration the generation of the document's collaborative state.
* @returns the name of the room.
*/
export function buildDocumentName(
documentId: string,
stateGeneration: number
): string {
return stateGeneration > 0
? `document.${documentId}.${stateGeneration}`
: `document.${documentId}`;
}
/**
* Parse the name of a collaboration room for a document.
*
* @param name the name of the room.
* @returns the parts of the name, or undefined if the name is not valid.
*/
export function parseDocumentName(name: string): DocumentNameParts | undefined {
const [type, documentId, generation, ...rest] = name.split(".");
if (type !== "document" || !documentId || rest.length) {
return undefined;
}
const stateGeneration = generation === undefined ? 0 : Number(generation);
if (
!Number.isSafeInteger(stateGeneration) ||
buildDocumentName(documentId, stateGeneration) !== name
) {
return undefined;
}
return { documentId, stateGeneration };
}