yjs-connection-manager.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import type { Server } from 'socket.io';
  2. import { MongodbPersistence } from 'y-mongodb-provider';
  3. import { YSocketIO, type Document as Ydoc } from 'y-socket.io/dist/server';
  4. import * as Y from 'yjs';
  5. import { getMongoUri } from '../util/mongoose-utils';
  6. const MONGODB_PERSISTENCE_COLLECTION_NAME = 'yjs-writings';
  7. const MONGODB_PERSISTENCE_FLUSH_SIZE = 100;
  8. export const extractPageIdFromYdocId = (ydocId: string): string | undefined => {
  9. const result = ydocId.match(/yjs\/(.*)/);
  10. return result?.[1];
  11. };
  12. class YjsConnectionManager {
  13. private static instance: YjsConnectionManager;
  14. private ysocketio: YSocketIO;
  15. private mdb: MongodbPersistence;
  16. get ysocketioInstance(): YSocketIO {
  17. return this.ysocketio;
  18. }
  19. get mdbInstance(): MongodbPersistence {
  20. return this.mdb;
  21. }
  22. private constructor(io: Server) {
  23. this.ysocketio = new YSocketIO(io);
  24. this.ysocketio.initialize();
  25. this.mdb = new MongodbPersistence(getMongoUri(), {
  26. collectionName: MONGODB_PERSISTENCE_COLLECTION_NAME,
  27. flushSize: MONGODB_PERSISTENCE_FLUSH_SIZE,
  28. });
  29. }
  30. public static getInstance(io?: Server) {
  31. if (this.instance != null) {
  32. return this.instance;
  33. }
  34. if (io == null) {
  35. throw new Error("'io' is required if initialize YjsConnectionManager");
  36. }
  37. this.instance = new YjsConnectionManager(io);
  38. return this.instance;
  39. }
  40. public async handleYDocSync(pageId: string, initialValue: string): Promise<void> {
  41. const currentYdoc = this.getCurrentYdoc(pageId);
  42. if (currentYdoc == null) {
  43. return;
  44. }
  45. const persistedYdoc = await this.getPersistedYdoc(pageId);
  46. await this.mdb.flushDocument(pageId);
  47. // If no write operation has been performed, insert initial value
  48. const clientsSize = persistedYdoc.store.clients.size;
  49. if (clientsSize === 0) {
  50. currentYdoc.getText('codemirror').insert(0, initialValue);
  51. }
  52. await this.syncWithPersistedYdoc(pageId, currentYdoc, persistedYdoc);
  53. currentYdoc.on('update', async(update) => {
  54. await this.mdb.storeUpdate(pageId, update);
  55. });
  56. currentYdoc.on('destroy', async() => {
  57. await this.mdb.flushDocument(pageId);
  58. });
  59. persistedYdoc.destroy();
  60. }
  61. public async handleYDocUpdate(pageId: string, newValue: string): Promise<void> {
  62. // TODO: https://redmine.weseek.co.jp/issues/132775
  63. // It's necessary to confirm that the user is not editing the target page in the Editor
  64. const currentYdoc = this.getCurrentYdoc(pageId);
  65. if (currentYdoc == null) {
  66. return;
  67. }
  68. const currentMarkdownLength = currentYdoc.getText('codemirror').length;
  69. currentYdoc.getText('codemirror').delete(0, currentMarkdownLength);
  70. currentYdoc.getText('codemirror').insert(0, newValue);
  71. const persistedYdoc = await this.getPersistedYdoc(pageId);
  72. await this.syncWithPersistedYdoc(pageId, currentYdoc, persistedYdoc);
  73. }
  74. private async syncWithPersistedYdoc(pageId: string, currentYdoc: Ydoc, persistedYdoc: Y.Doc): Promise<void> {
  75. const persistedStateVector = Y.encodeStateVector(persistedYdoc);
  76. const diff = Y.encodeStateAsUpdate(currentYdoc, persistedStateVector);
  77. if (diff.reduce((prev, curr) => prev + curr, 0) > 0) {
  78. await this.mdb.storeUpdate(pageId, diff);
  79. }
  80. }
  81. public getCurrentYdoc(pageId: string): Ydoc | undefined {
  82. const currentYdoc = this.ysocketio.documents.get(`yjs/${pageId}`);
  83. return currentYdoc;
  84. }
  85. public async getPersistedYdoc(pageId: string): Promise<Y.Doc> {
  86. const persistedYdoc = await this.mdb.getYDoc(pageId);
  87. return persistedYdoc;
  88. }
  89. }
  90. export const instantiateYjsConnectionManager = (io: Server): YjsConnectionManager => {
  91. return YjsConnectionManager.getInstance(io);
  92. };
  93. // export the singleton instance
  94. export const getYjsConnectionManager = (): YjsConnectionManager => {
  95. return YjsConnectionManager.getInstance();
  96. };