-
Notifications
You must be signed in to change notification settings - Fork 2
Cherry pick/optimize excalidraw sync code #179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shuntian
wants to merge
3
commits into
main
Choose a base branch
from
cherry-pick/optimize-excalidraw-sync-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ import ExcalidrawManager from "../managers/excalidraw-manager"; | |
| import UsersManager from "../managers/users-manager"; | ||
| import IOHelper from "./io-helper"; | ||
| import checkPermission from "./is-permission-valid"; | ||
| import logger from "../../../loggers"; | ||
|
|
||
| class ExdrawIOHandler { | ||
|
|
||
|
|
@@ -24,25 +25,37 @@ class ExdrawIOHandler { | |
| onConnection(socket) { | ||
| // todo permission check | ||
| this.ioHelper.sendInitRoomToPrivate(socket.id); | ||
| socket.on('join-room', async (params) => { | ||
| // join room | ||
| const { doc_uuid: docUuid, user: userInfo } = params; | ||
| socket.join(docUuid); | ||
|
|
||
| const usersManager = UsersManager.getInstance(); | ||
| if (!usersManager.getUser(docUuid, socket.id)) { | ||
| usersManager.addUser(docUuid, socket.id, userInfo); | ||
| socket.on('join-room', async (params, callback) => { | ||
| try { | ||
| const { doc_uuid: docUuid, user: userInfo } = params || {}; | ||
| if (!docUuid || !userInfo) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Warning] user 对象形状未校验 Why this matters: Suggested fix: 校验 userInfo 为对象且含协议约定的用户名字段;失败时不调用 socket.join/UsersManager,返回失败 ACK,并增加 user: {} 回归用例。 |
||
| throw new Error('Invalid join-room parameters'); | ||
| } | ||
|
|
||
| await socket.join(docUuid); | ||
|
|
||
| const usersManager = UsersManager.getInstance(); | ||
| if (!usersManager.getUser(docUuid, socket.id)) { | ||
| usersManager.addUser(docUuid, socket.id, userInfo); | ||
| } | ||
|
|
||
| const users = usersManager.getDocUsers(docUuid); | ||
|
|
||
| // if (users.length === 1) { | ||
| // this.ioHelper.sendFirstInRoomMessage(socket.id); | ||
| // } else { | ||
| // this.ioHelper.sendNewUserMessage(socket, docUuid); | ||
| // } | ||
|
|
||
| this.ioHelper.sendRoomUserChangeMessage(socket, docUuid, users); | ||
| callback && callback({ success: true }); | ||
| } catch (error) { | ||
| logger.error('SOCKET_MESSAGE: Join room failed', error); | ||
| callback && callback({ | ||
| success: false, | ||
| error_type: 'join_room_error', | ||
| }); | ||
| } | ||
|
|
||
| const users = usersManager.getDocUsers(docUuid); | ||
|
|
||
| // if (users.length === 1) { | ||
| // this.ioHelper.sendFirstInRoomMessage(socket.id); | ||
| // } else { | ||
| // this.ioHelper.sendNewUserMessage(socket, docUuid); | ||
| // } | ||
|
|
||
| this.ioHelper.sendRoomUserChangeMessage(socket, docUuid, users); | ||
| }); | ||
|
|
||
| socket.on('elements-updated', async (params, callback) => { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| import ExcalidrawManager from '../../src/modules/exdraw/managers/excalidraw-manager'; | ||
| import ExcalidrawDocument from '../../src/modules/exdraw/models/excalidraw-document'; | ||
| import seaServerAPI from '../../src/modules/exdraw/api/sea-server-api'; | ||
|
|
||
| jest.mock('../../src/modules/exdraw/api/sea-server-api', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| getSceneDownloadLink: jest.fn(), | ||
| getSceneContent: jest.fn(), | ||
| saveSceneContent: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../src/loggers', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| error: jest.fn(), | ||
| info: jest.fn(), | ||
| warn: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('../../src/utils', () => ({ | ||
| __esModule: true, | ||
| deleteDir: jest.fn(), | ||
| getErrorMessage: jest.fn(() => ({})), | ||
| errorHandle: jest.fn(), | ||
| })); | ||
|
|
||
| const createDeferred = () => { | ||
| let resolve; | ||
| const promise = new Promise((res) => { | ||
| resolve = res; | ||
| }); | ||
| return { promise, resolve }; | ||
| }; | ||
|
|
||
| describe('ExcalidrawManager cold document loading and saving', () => { | ||
| let manager; | ||
|
|
||
| beforeEach(() => { | ||
| manager = ExcalidrawManager.getInstance(); | ||
| manager.documents = new Map(); | ||
| jest.clearAllMocks(); | ||
| seaServerAPI.getSceneDownloadLink.mockResolvedValue({ | ||
| data: { download_link: 'https://example.test/doc' }, | ||
| }); | ||
| }); | ||
|
|
||
| it('uses the document already cached when concurrent cold loads finish', async () => { | ||
| const contentLoads = []; | ||
| seaServerAPI.getSceneContent.mockImplementation(() => { | ||
| const deferred = createDeferred(); | ||
| contentLoads.push(deferred); | ||
| return deferred.promise; | ||
| }); | ||
|
|
||
| const firstLoad = manager.getSceneDoc('doc-uuid', 'doc.excalidraw', 'user-a'); | ||
| const secondLoad = manager.getSceneDoc('doc-uuid', 'doc.excalidraw', 'user-b'); | ||
|
|
||
| await new Promise(resolve => setImmediate(resolve)); | ||
| expect(contentLoads).toHaveLength(2); | ||
|
|
||
| contentLoads[0].resolve({ | ||
| data: { version: 0, elements: [] }, | ||
| }); | ||
| await new Promise(resolve => setImmediate(resolve)); | ||
|
|
||
| contentLoads[1].resolve({ | ||
| data: { version: 0, elements: [] }, | ||
| }); | ||
|
|
||
| const [firstDocument, secondDocument] = await Promise.all([firstLoad, secondLoad]); | ||
| const cachedDocument = manager.documents.get('doc-uuid'); | ||
|
|
||
| expect(cachedDocument.last_modify_user).toBe('user-a'); | ||
| expect(firstDocument.last_modify_user).toBe('user-a'); | ||
| expect(secondDocument.last_modify_user).toBe('user-a'); | ||
| }); | ||
|
|
||
| it('keeps the document dirty when an operation arrives during saving', async () => { | ||
| const document = new ExcalidrawDocument('doc-uuid', 'doc.excalidraw', { | ||
| version: 0, | ||
| elements: [], | ||
| }); | ||
| document.setMeta({ need_save: true }); | ||
| manager.documents.set('doc-uuid', document); | ||
|
|
||
| const save = createDeferred(); | ||
| seaServerAPI.saveSceneContent.mockReturnValue(save.promise); | ||
|
|
||
| const savePromise = manager.saveSceneDoc('doc-uuid'); | ||
| document.setValue([{ id: 'new-element' }], 1); | ||
| save.resolve({ data: {} }); | ||
|
|
||
| expect(await savePromise).toBe(true); | ||
| expect(document.getMeta()).toEqual(expect.objectContaining({ | ||
| is_saving: false, | ||
| need_save: true, | ||
| })); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import ExdrawIOHandler from '../src/modules/exdraw/wio/io-handler'; | ||
| import IOHelper from '../src/modules/exdraw/wio/io-helper'; | ||
| import UsersManager from '../src/modules/exdraw/managers/users-manager'; | ||
|
|
||
| jest.mock('../src/modules/exdraw/wio/io-helper'); | ||
| jest.mock('../src/modules/exdraw/managers/users-manager'); | ||
| jest.mock('../src/loggers', () => ({ | ||
| __esModule: true, | ||
| default: { | ||
| error: jest.fn(), | ||
| }, | ||
| })); | ||
|
|
||
| describe('ExdrawIOHandler join-room', () => { | ||
| let ioHelper; | ||
| let usersManager; | ||
| let socket; | ||
| let handlers; | ||
|
|
||
| beforeEach(() => { | ||
| handlers = {}; | ||
| ioHelper = { | ||
| sendInitRoomToPrivate: jest.fn(), | ||
| sendRoomUserChangeMessage: jest.fn(), | ||
| }; | ||
| usersManager = { | ||
| getUser: jest.fn(), | ||
| addUser: jest.fn(), | ||
| getDocUsers: jest.fn().mockReturnValue([]), | ||
| }; | ||
| socket = { | ||
| id: 'socket-id', | ||
| on: jest.fn((event, handler) => { | ||
| handlers[event] = handler; | ||
| }), | ||
| join: jest.fn().mockResolvedValue(undefined), | ||
| }; | ||
| IOHelper.getInstance.mockReturnValue(ioHelper); | ||
| UsersManager.getInstance.mockReturnValue(usersManager); | ||
| }); | ||
|
|
||
| it('returns a success acknowledgement after joining the room', async () => { | ||
| const callback = jest.fn(); | ||
| const handler = new ExdrawIOHandler({}); | ||
|
|
||
| handler.onConnection(socket); | ||
| await handlers['join-room']({ | ||
| doc_uuid: 'doc-uuid', | ||
| user: { _username: 'user' }, | ||
| }, callback); | ||
|
|
||
| expect(socket.join).toHaveBeenCalledWith('doc-uuid'); | ||
| expect(ioHelper.sendRoomUserChangeMessage).toHaveBeenCalledWith(socket, 'doc-uuid', []); | ||
| expect(callback).toHaveBeenCalledWith({ success: true }); | ||
| }); | ||
|
|
||
| it('returns a failure acknowledgement when joining the room throws', async () => { | ||
| const callback = jest.fn(); | ||
| socket.join.mockRejectedValue(new Error('adapter unavailable')); | ||
| const handler = new ExdrawIOHandler({}); | ||
|
|
||
| handler.onConnection(socket); | ||
| await handlers['join-room']({ | ||
| doc_uuid: 'doc-uuid', | ||
| user: { _username: 'user' }, | ||
| }, callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith({ | ||
| success: false, | ||
| error_type: 'join_room_error', | ||
| }); | ||
| }); | ||
|
|
||
| it('returns a failure acknowledgement when user management throws', async () => { | ||
| const callback = jest.fn(); | ||
| usersManager.addUser.mockImplementation(() => { | ||
| throw new Error('user manager unavailable'); | ||
| }); | ||
| const handler = new ExdrawIOHandler({}); | ||
|
|
||
| handler.onConnection(socket); | ||
| await handlers['join-room']({ | ||
| doc_uuid: 'doc-uuid', | ||
| user: { _username: 'user' }, | ||
| }, callback); | ||
|
|
||
| expect(callback).toHaveBeenCalledWith({ | ||
| success: false, | ||
| error_type: 'join_room_error', | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Warning] 参数错误 ACK 未覆盖
Why this matters:
实现已把缺少 doc_uuid 或 user 的 join-room 请求转换为 join_room_error,但新增测试只覆盖成功路径和有效参数下的内部异常。参数校验分支一旦被后续重构破坏,客户端会重新落入 ACK 超时与重复重连。
Suggested fix: 增加缺少 doc_uuid、缺少 user 和空 params 三个用例,断言 callback 收到 { success: false, error_type: "join_room_error" },且 socket.join 和 UsersManager 均未调用。