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

@ -6,11 +6,31 @@
import { RealtimeDoc } from './realtime-doc.js'
import { describe, expect, it } from '@jest/globals'
describe('websocket-doc', () => {
it('saves the initial content', () => {
describe('realtime doc', () => {
it('saves an initial text content correctly', () => {
const textContent = 'textContent'
const websocketDoc = new RealtimeDoc(textContent)
const realtimeDoc = new RealtimeDoc(textContent)
expect(realtimeDoc.getCurrentContent()).toBe(textContent)
})
expect(websocketDoc.getCurrentContent()).toBe(textContent)
it('will initialize an empty text if no initial content is given', () => {
const realtimeDoc = new RealtimeDoc()
expect(realtimeDoc.getCurrentContent()).toBe('')
})
it('restores a yjs state vector update correctly', () => {
const realtimeDoc = new RealtimeDoc(
'notTheVectorText',
[
1, 1, 221, 208, 165, 230, 3, 0, 4, 1, 15, 109, 97, 114, 107, 100, 111,
119, 110, 67, 111, 110, 116, 101, 110, 116, 32, 116, 101, 120, 116, 67,
111, 110, 116, 101, 110, 116, 70, 114, 111, 109, 83, 116, 97, 116, 101,
86, 101, 99, 116, 111, 114, 85, 112, 100, 97, 116, 101, 0
]
)
expect(realtimeDoc.getCurrentContent()).toBe(
'textContentFromStateVectorUpdate'
)
})
})