perf: multiple Tab for the same user lead to duplicate joining issues

This commit is contained in:
molvqingtai 2024-09-25 02:54:17 +08:00
parent e9e73bd128
commit 420586839a

View file

@ -281,20 +281,28 @@ const RoomDomain = Remesh.domain({
const commandEvent$ = (() => { const commandEvent$ = (() => {
switch (message.type) { switch (message.type) {
case SendType.UserSync: { case SendType.UserSync: {
const self = get(UserListQuery()).find((user) => user.peerId === peerRoom.peerId)! const userList = get(UserListQuery())
const isJoining = self.joinTime < message.joinTime const selfUser = userList.find((user) => user.peerId === peerRoom.peerId)!
return of( // If the browser has multiple tabs open, it can cause the same user to join multiple times with the same peerId but different userId
UpdateUserListCommand({ type: 'create', user: message }), const isSelfJoinEvent = !!userList.find((user) => user.userId === message.userId)
isJoining // When a new user joins, it triggers join events for all users, i.e., newUser join event and oldUser join event
? messageListDomain.command.CreateItemCommand({ // Use joinTime to determine if it's a new user
...message, const isNewJoinEvent = selfUser.joinTime < message.joinTime
id: nanoid(),
body: `"${message.username}" joined the chat`, return isSelfJoinEvent
type: MessageType.Prompt, ? EMPTY
date: Date.now() : of(
}) UpdateUserListCommand({ type: 'create', user: message }),
: null isNewJoinEvent
) ? messageListDomain.command.CreateItemCommand({
...message,
id: nanoid(),
body: `"${message.username}" joined the chat`,
type: MessageType.Prompt,
date: Date.now()
})
: null
)
} }
case SendType.Text: case SendType.Text:
return of( return of(