|
|
@@ -25,6 +25,10 @@ class YjsConnectionManager {
|
|
|
return this.ysocketio;
|
|
|
}
|
|
|
|
|
|
+ get mdbInstance(): MongodbPersistence {
|
|
|
+ return this.mdb;
|
|
|
+ }
|
|
|
+
|
|
|
private constructor(io: Server) {
|
|
|
this.ysocketio = new YSocketIO(io);
|
|
|
this.ysocketio.initialize();
|
|
|
@@ -55,7 +59,6 @@ class YjsConnectionManager {
|
|
|
}
|
|
|
|
|
|
const persistedYdoc = await this.getPersistedYdoc(pageId);
|
|
|
- const persistedStateVector = Y.encodeStateVector(persistedYdoc);
|
|
|
|
|
|
await this.mdb.flushDocument(pageId);
|
|
|
|
|
|
@@ -65,13 +68,7 @@ class YjsConnectionManager {
|
|
|
currentYdoc.getText('codemirror').insert(0, initialValue);
|
|
|
}
|
|
|
|
|
|
- const diff = Y.encodeStateAsUpdate(currentYdoc, persistedStateVector);
|
|
|
-
|
|
|
- if (diff.reduce((prev, curr) => prev + curr, 0) > 0) {
|
|
|
- this.mdb.storeUpdate(pageId, diff);
|
|
|
- }
|
|
|
-
|
|
|
- Y.applyUpdate(currentYdoc, Y.encodeStateAsUpdate(persistedYdoc));
|
|
|
+ await this.syncWithPersistedYdoc(pageId, currentYdoc, persistedYdoc);
|
|
|
|
|
|
currentYdoc.on('update', async(update) => {
|
|
|
await this.mdb.storeUpdate(pageId, update);
|
|
|
@@ -95,7 +92,18 @@ class YjsConnectionManager {
|
|
|
const currentMarkdownLength = currentYdoc.getText('codemirror').length;
|
|
|
currentYdoc.getText('codemirror').delete(0, currentMarkdownLength);
|
|
|
currentYdoc.getText('codemirror').insert(0, newValue);
|
|
|
- Y.encodeStateAsUpdate(currentYdoc);
|
|
|
+
|
|
|
+ const persistedYdoc = await this.getPersistedYdoc(pageId);
|
|
|
+ await this.syncWithPersistedYdoc(pageId, currentYdoc, persistedYdoc);
|
|
|
+ }
|
|
|
+
|
|
|
+ private async syncWithPersistedYdoc(pageId: string, currentYdoc: Ydoc, persistedYdoc: Y.Doc): Promise<void> {
|
|
|
+ const persistedStateVector = Y.encodeStateVector(persistedYdoc);
|
|
|
+ const diff = Y.encodeStateAsUpdate(currentYdoc, persistedStateVector);
|
|
|
+ if (diff.reduce((prev, curr) => prev + curr, 0) > 0) {
|
|
|
+ await this.mdb.storeUpdate(pageId, diff);
|
|
|
+ }
|
|
|
+ Y.applyUpdate(currentYdoc, Y.encodeStateAsUpdate(persistedYdoc));
|
|
|
}
|
|
|
|
|
|
public getCurrentYdoc(pageId: string): Ydoc | undefined {
|