Yuki Takei 1 год назад
Родитель
Сommit
e807744d7d

+ 0 - 49
apps/app/src/server/service/yjs/yjs.ts

@@ -32,7 +32,6 @@ type RequestWithUser = IncomingMessage & { user: IUserHasId };
 
 export interface IYjsService {
   getYDocStatus(pageId: string): Promise<YDocStatus>;
-  // handleYDocSync(pageId: string, initialValue: string): Promise<void>;
   handleYDocUpdate(pageId: string, newValue: string): Promise<void>;
   getCurrentYdoc(pageId: string): Ydoc | undefined;
 }
@@ -135,16 +134,6 @@ class YjsService implements IYjsService {
     //     }
     //   });
 
-    //   socket.on(GlobalSocketEventName.YDocSync, async({ pageId, initialValue }) => {
-    //     try {
-    //       await this.handleYDocSync(pageId, initialValue);
-    //     }
-    //     catch (error) {
-    //       logger.warn(error.message);
-    //       socket.emit(GlobalSocketEventName.YDocSyncError, 'An error occurred during YDoc synchronization.');
-    //     }
-    //   });
-    // });
   }
 
   private injectPersistence(ysocketio: YSocketIO, mdb: MongodbPersistence): void {
@@ -218,44 +207,6 @@ class YjsService implements IYjsService {
     return YDocStatus.OUTDATED;
   }
 
-  // public async handleYDocSync(pageId: string, initialValue: string): Promise<void> {
-  //   const currentYdoc = this.getCurrentYdoc(pageId);
-  //   if (currentYdoc == null) {
-  //     return;
-  //   }
-
-  //   const persistedYdoc = await this.getPersistedYdoc(pageId);
-  //   const persistedStateVector = Y.encodeStateVector(persistedYdoc);
-
-  //   await this.mdb.flushDocument(pageId);
-
-  //   // If no write operation has been performed, insert initial value
-  //   const clientsSize = persistedYdoc.store.clients.size;
-  //   if (clientsSize === 0) {
-  //     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);
-  //     this.mdb.setMeta(pageId, 'updatedAt', Date.now());
-  //   }
-
-  //   Y.applyUpdate(currentYdoc, Y.encodeStateAsUpdate(persistedYdoc));
-
-  //   currentYdoc.on('update', async(update) => {
-  //     this.mdb.storeUpdate(pageId, update);
-  //     this.mdb.setMeta(pageId, 'updatedAt', Date.now());
-  //   });
-
-  //   currentYdoc.on('destroy', async() => {
-  //     this.mdb.flushDocument(pageId);
-  //   });
-
-  //   persistedYdoc.destroy();
-  // }
-
   public async handleYDocUpdate(pageId: string, newValue: string): Promise<void> {
     // TODO: https://redmine.weseek.co.jp/issues/132775
     // It's necessary to confirm that the user is not editing the target page in the Editor

+ 12 - 3
apps/app/src/stores/yjs.ts

@@ -15,7 +15,13 @@ type CurrentPageYjsDataUtils = {
 }
 
 export const useCurrentPageYjsData = (): SWRResponse<CurrentPageYjsData, Error> & CurrentPageYjsDataUtils => {
-  const swrResponse = useSWRStatic<CurrentPageYjsData, Error>('currentPageYjsData', undefined);
+  const { data: currentPageId } = useCurrentPageId();
+
+  const key = currentPageId != null
+    ? `/page/${currentPageId}/yjs-data`
+    : null;
+
+  const swrResponse = useSWRStatic<CurrentPageYjsData, Error>(key, undefined);
 
   const updateHasYdocsNewerThanLatestRevision = useCallback((hasYdocsNewerThanLatestRevision: boolean) => {
     swrResponse.mutate({ ...swrResponse.data, hasYdocsNewerThanLatestRevision });
@@ -29,12 +35,15 @@ export const useCurrentPageYjsData = (): SWRResponse<CurrentPageYjsData, Error>
 };
 
 export const useSWRMUTxCurrentPageYjsData = (): SWRMutationResponse<CurrentPageYjsData, Error> => {
-  const key = 'currentPageYjsData';
   const { data: currentPageId } = useCurrentPageId();
 
+  const key = currentPageId != null
+    ? `/page/${currentPageId}/yjs-data`
+    : null;
+
   return useSWRMutation(
     key,
-    () => apiv3Get<{ yjsData: CurrentPageYjsData }>(`/page/${currentPageId}/yjs-data`).then(result => result.data.yjsData),
+    ([endpoint]) => apiv3Get<{ yjsData: CurrentPageYjsData }>(endpoint).then(result => result.data.yjsData),
     { populateCache: true, revalidate: false },
   );
 };

+ 0 - 1
packages/core/src/interfaces/index.ts

@@ -13,4 +13,3 @@ export * from './subscription';
 export * from './tag';
 export * from './user';
 export * from './vite';
-export * from './websocket';

+ 0 - 6
packages/core/src/interfaces/websocket.ts

@@ -1,6 +0,0 @@
-export const GlobalSocketEventName = {
-  // YDoc
-  YDocSync: 'ydoc:sync',
-  YDocSyncError: 'ydoc:sync:error',
-} as const;
-export type GlobalSocketEventName = typeof GlobalSocketEventName[keyof typeof GlobalSocketEventName];

+ 2 - 6
packages/editor/src/client/stores/use-collaborative-editor-mode.ts

@@ -1,7 +1,7 @@
 import { useEffect, useState } from 'react';
 
 import { keymap } from '@codemirror/view';
-import { GlobalSocketEventName, type IUserHasId } from '@growi/core/dist/interfaces';
+import type { IUserHasId } from '@growi/core/dist/interfaces';
 import { useGlobalSocket } from '@growi/core/dist/swr';
 import { yCollab, yUndoManagerKeymap } from 'y-codemirror.next';
 import { SocketIOProvider } from 'y-socket.io';
@@ -44,9 +44,6 @@ export const useCollaborativeEditorMode = (
     // so only awareness is destroyed here
     provider?.awareness.destroy();
 
-    // TODO: catch ydoc:sync:error GlobalSocketEventName.YDocSyncError
-    socket?.off(GlobalSocketEventName.YDocSync);
-
     setCPageId(pageId);
 
     // reset editors
@@ -95,14 +92,13 @@ export const useCollaborativeEditorMode = (
 
     socketIOProvider.on('sync', (isSync: boolean) => {
       if (isSync) {
-        socket.emit(GlobalSocketEventName.YDocSync, { pageId, initialValue });
         const userList: IUserHasId[] = Array.from(socketIOProvider.awareness.states.values(), value => value.user.user && value.user.user);
         onEditorsUpdated(userList);
       }
     });
 
     // update args type see: SocketIOProvider.Awareness.awarenessUpdate
-    socketIOProvider.awareness.on('update', (update: any) => {
+    socketIOProvider.awareness.on('update', (update: { added: unknown[]; removed: unknown[]; }) => {
       const { added, removed } = update;
       if (added.length > 0 || removed.length > 0) {
         const userList: IUserHasId[] = Array.from(socketIOProvider.awareness.states.values(), value => value.user.user && value.user.user);