Skip to content

Commit

Permalink
Merge pull request #117 from whereby/kevinhanna/handle-room-join-errors
Browse files Browse the repository at this point in the history
core: handle room_joined errors
  • Loading branch information
kevinwhereby authored Mar 21, 2024
2 parents 748ec2c + d4937dd commit b5283c1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/tasty-geese-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@whereby.com/core": minor
---

Handle errors while joining a room
19 changes: 18 additions & 1 deletion packages/core/src/redux/slices/__tests__/roomConnection.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe("roomConnectionSlice", () => {
const result = roomConnectionSlice.reducer(
undefined,
signalEvents.roomJoined({
error: "room_locked",
selfId: "selfId",
isLocked: false,
}),
Expand All @@ -39,6 +38,24 @@ describe("roomConnectionSlice", () => {
});
});

it("should set status to disconnected and populate the error if the there is an error", () => {
const result = roomConnectionSlice.reducer(
undefined,
signalEvents.roomJoined({
error: "room_full",
selfId: "selfId",
isLocked: false,
}),
);

expect(result).toEqual({
status: "disconnected",
session: null,
error: "room_full",
});
});
});
describe("signalEvents.clientKicked", () => {
it("should set status to kicked if the client is kicked", () => {
const result = roomConnectionSlice.reducer(
undefined,
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/redux/slices/roomConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export type ConnectionStatus =
export interface RoomConnectionState {
session: { createdAt: string; id: string } | null;
status: ConnectionStatus;
error: unknown;
error: string | null;
}

const initialState: RoomConnectionState = {
Expand All @@ -64,7 +64,6 @@ export const roomConnectionSlice = createSlice({
},
extraReducers: (builder) => {
builder.addCase(signalEvents.roomJoined, (state, action) => {
//TODO: Handle error
const { error, isLocked } = action.payload;

if (error === "room_locked" && isLocked) {
Expand All @@ -74,6 +73,14 @@ export const roomConnectionSlice = createSlice({
};
}

if (error) {
return {
...state,
status: "disconnected",
error,
};
}

return {
...state,
status: "connected",
Expand Down Expand Up @@ -192,6 +199,7 @@ export const selectRoomConnectionRaw = (state: RootState) => state.roomConnectio
export const selectRoomConnectionSession = (state: RootState) => state.roomConnection.session;
export const selectRoomConnectionSessionId = (state: RootState) => state.roomConnection.session?.id;
export const selectRoomConnectionStatus = (state: RootState) => state.roomConnection.status;
export const selectRoomConnectionError = (state: RootState) => state.roomConnection.error;

/**
* Reactors
Expand Down

0 comments on commit b5283c1

Please sign in to comment.