refactor: save ydoc state in the database, so it can be restored easier

By storing the ydoc state in the database we can reconnect lost clients easier
and enable offline editing because we continue using the crdt data that has been
used by the client before the connection loss.

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-03-24 10:26:49 +01:00
parent 4707540237
commit a826677225
26 changed files with 301 additions and 204 deletions

View file

@ -43,6 +43,7 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
.createRevision(
realtimeNote.getNote(),
realtimeNote.getRealtimeDoc().getCurrentContent(),
realtimeNote.getRealtimeDoc().encodeStateAsUpdate(),
)
.catch((reason) => this.logger.error(reason));
}
@ -68,9 +69,12 @@ export class RealtimeNoteService implements BeforeApplicationShutdown {
* @return The created realtime note
*/
private async createNewRealtimeNote(note: Note): Promise<RealtimeNote> {
const initialContent = (await this.revisionsService.getLatestRevision(note))
.content;
const realtimeNote = this.realtimeNoteStore.create(note, initialContent);
const lastRevision = await this.revisionsService.getLatestRevision(note);
const realtimeNote = this.realtimeNoteStore.create(
note,
lastRevision.content,
lastRevision.yjsStateVector ?? undefined,
);
realtimeNote.on('beforeDestroy', () => {
this.saveRealtimeNote(realtimeNote);
});