yjs-connection-manager.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import type { Server } from 'socket.io';
  2. import { MongodbPersistence } from 'y-mongodb-provider';
  3. import { YSocketIO } from 'y-socket.io/dist/server';
  4. import * as Y from 'yjs';
  5. import { getMongoUri } from '../util/mongoose-utils';
  6. export const MONGODB_PERSISTENCE_COLLECTION_NAME = 'yjs-writings';
  7. export const MONGODB_PERSISTENCE_FLUSH_SIZE = 100;
  8. class YjsConnectionManager {
  9. private ysocketio: YSocketIO;
  10. private mdb: MongodbPersistence;
  11. constructor(io: Server) {
  12. this.ysocketio = new YSocketIO(io);
  13. this.ysocketio.initialize();
  14. this.mdb = new MongodbPersistence(getMongoUri(), {
  15. collectionName: MONGODB_PERSISTENCE_COLLECTION_NAME,
  16. flushSize: MONGODB_PERSISTENCE_FLUSH_SIZE,
  17. });
  18. this.getCurrentYdoc = this.getCurrentYdoc.bind(this);
  19. }
  20. public async handleYDocSync(pageId: string, initialValue: string): Promise<void> {
  21. const persistedYdoc = await this.mdb.getYDoc(pageId);
  22. const persistedStateVector = Y.encodeStateVector(persistedYdoc);
  23. await this.mdb.flushDocument(pageId);
  24. const currentYdoc = this.getCurrentYdoc(pageId);
  25. const persistedCodeMirrorText = persistedYdoc.getText('codemirror').toString();
  26. const currentCodeMirrorText = currentYdoc.getText('codemirror').toString();
  27. if (persistedCodeMirrorText === '' && currentCodeMirrorText === '') {
  28. currentYdoc.getText('codemirror').insert(0, initialValue);
  29. }
  30. const diff = Y.encodeStateAsUpdate(currentYdoc, persistedStateVector);
  31. if (diff.reduce((prev, curr) => prev + curr, 0) > 0) {
  32. this.mdb.storeUpdate(pageId, diff);
  33. }
  34. Y.applyUpdate(currentYdoc, Y.encodeStateAsUpdate(persistedYdoc));
  35. currentYdoc.on('update', async(update) => {
  36. await this.mdb.storeUpdate(pageId, update);
  37. });
  38. currentYdoc.on('destroy', async() => {
  39. await this.mdb.flushDocument(pageId);
  40. });
  41. persistedYdoc.destroy();
  42. }
  43. private getCurrentYdoc(pageId: string): Y.Doc {
  44. const currentYdoc = this.ysocketio.documents.get(`yjs/${pageId}`);
  45. if (currentYdoc == null) {
  46. throw new Error(`currentYdoc for pageId ${pageId} is undefined.`);
  47. }
  48. return currentYdoc;
  49. }
  50. }
  51. export default YjsConnectionManager;