Skip to content

Commit

Permalink
Implement board.left event to sync user list when someone leaves the …
Browse files Browse the repository at this point in the history
…board
  • Loading branch information
raimohanska committed Jan 24, 2024
1 parent b930c66 commit 74125fe
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions backend/src/websocket-sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export function endSession(socket: WsWrapper) {
const boardState = maybeGetBoard(session.boardSession.boardId)
if (boardState) {
boardState.sessions = boardState.sessions.filter((s) => s.sessionId !== sessionId)
broadcastBoardEvent({ action: "board.left", boardId: boardState.board.id, sessionId })
} else {
console.warn(`Board state not found when ending session: ${session.boardSession.boardId}`)
}
Expand Down
2 changes: 2 additions & 0 deletions common/src/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ export type BoardStateSyncEvent =
| GotBoardLocks
| CursorPositions
| JoinedBoard
| LeftBoard
| UserLoggedIn
| AckJoinBoard
| DeniedJoinBoard
Expand Down Expand Up @@ -326,6 +327,7 @@ export type RecentBoardsFromServer = { action: "user.boards"; email: string; boa
export type Ack = { action: "ack"; ackId: string; serials: Record<Id, Serial> }
export type ActionApplyFailed = { action: "board.action.apply.failed" }
export type JoinedBoard = { action: "board.joined"; boardId: Id } & UserSessionInfo
export type LeftBoard = { action: "board.left"; boardId: Id; sessionId: Id }
export type UserInfoUpdate = { action: "userinfo.set" } & UserSessionInfo
export type InitBoardNew = { action: "board.init"; board: Board; accessLevel: AccessLevel }
export type InitBoardDiff = {
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/store/board-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ export function BoardStore(
}
} else if (event.action === "board.joined") {
return { ...state, users: state.users.concat(event) }
} else if (event.action === "board.left") {
return { ...state, users: state.users.filter((u) => u.sessionId !== event.sessionId) }
} else if (event.action === "board.locks") {
return { ...state, locks: event.locks }
} else if (event.action === "ack") {
Expand Down

0 comments on commit 74125fe

Please sign in to comment.