From 420586839ac6e6192caa258b271da948b5f80992 Mon Sep 17 00:00:00 2001 From: molvqingtai Date: Wed, 25 Sep 2024 02:54:17 +0800 Subject: [PATCH] perf: multiple Tab for the same user lead to duplicate joining issues --- src/domain/Room.ts | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/domain/Room.ts b/src/domain/Room.ts index 470341c..6d712d5 100644 --- a/src/domain/Room.ts +++ b/src/domain/Room.ts @@ -281,20 +281,28 @@ const RoomDomain = Remesh.domain({ const commandEvent$ = (() => { switch (message.type) { case SendType.UserSync: { - const self = get(UserListQuery()).find((user) => user.peerId === peerRoom.peerId)! - const isJoining = self.joinTime < message.joinTime - return of( - UpdateUserListCommand({ type: 'create', user: message }), - isJoining - ? messageListDomain.command.CreateItemCommand({ - ...message, - id: nanoid(), - body: `"${message.username}" joined the chat`, - type: MessageType.Prompt, - date: Date.now() - }) - : null - ) + const userList = get(UserListQuery()) + const selfUser = userList.find((user) => user.peerId === peerRoom.peerId)! + // If the browser has multiple tabs open, it can cause the same user to join multiple times with the same peerId but different userId + const isSelfJoinEvent = !!userList.find((user) => user.userId === message.userId) + // When a new user joins, it triggers join events for all users, i.e., newUser join event and oldUser join event + // Use joinTime to determine if it's a new user + const isNewJoinEvent = selfUser.joinTime < message.joinTime + + return isSelfJoinEvent + ? EMPTY + : of( + UpdateUserListCommand({ type: 'create', user: message }), + isNewJoinEvent + ? messageListDomain.command.CreateItemCommand({ + ...message, + id: nanoid(), + body: `"${message.username}" joined the chat`, + type: MessageType.Prompt, + date: Date.now() + }) + : null + ) } case SendType.Text: return of(