Skip to content

Commit ef20b12

Browse files
committed
adding comments
1 parent adc5c5d commit ef20b12

File tree

3 files changed

+31
-31
lines changed

3 files changed

+31
-31
lines changed

app/src/components/left/RoomsContainer.tsx

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,16 @@ const RoomsContainer = () => {
3939
})
4040
);
4141

42-
React.useEffect(() => {
43-
console.log('You Joined Room---:', roomCode);
44-
}, [roomCode]);
45-
4642
function initSocketConnection(roomCode: string) {
4743
if (socket) socket.disconnect(); //edge case check if socket connection existed
4844

4945
socket = io(API_BASE_URL, {
50-
//establishing client and server
46+
//establishing client and server connection
5147
transports: ['websocket']
5248
});
5349

50+
//connecting user to server
5451
socket.on('connect', () => {
55-
//connecting user to server
5652
socket.emit('joining', userName, roomCode);
5753
console.log(`${userName} Joined room ${roomCode}`);
5854
});
@@ -64,7 +60,7 @@ const RoomsContainer = () => {
6460
callback(newState); //pull new state from host and send it to back end
6561
});
6662

67-
socket.on('back emitting state from host', (state, callback) => {
63+
socket.on('server emitting state from host', (state, callback) => {
6864
//getting state from host once joined a room
6965
//dispatching new state to change user current state
7066
store.dispatch(allCooperativeState(state.appState));
@@ -80,25 +76,28 @@ const RoomsContainer = () => {
8076

8177
// receiving the message from the back end
8278
socket.on('new state from back', (event) => {
83-
let currentStore: any = JSON.stringify(store.getState());
84-
if (currentStore !== event) {
85-
currentStore = JSON.parse(currentStore);
86-
event = JSON.parse(event);
87-
if (
88-
JSON.stringify(currentStore.appState) !==
89-
JSON.stringify(event.appState)
90-
) {
91-
store.dispatch(allCooperativeState(event.appState));
79+
const currentStore = JSON.parse(JSON.stringify(store.getState()));
80+
const parsedEvent = JSON.parse(event);
81+
82+
const areStatesEqual = (stateA, stateB) =>
83+
JSON.stringify(stateA) === JSON.stringify(stateB);
84+
85+
if (!areStatesEqual(currentStore, parsedEvent)) {
86+
if (!areStatesEqual(currentStore.appState, parsedEvent.appState)) {
87+
store.dispatch(allCooperativeState(parsedEvent.appState));
9288
} else if (
93-
JSON.stringify(currentStore.codePreviewSlice) !==
94-
JSON.stringify(event.codePreviewCooperative)
89+
!areStatesEqual(
90+
currentStore.codePreviewSlice,
91+
parsedEvent.codePreviewCooperative
92+
)
9593
) {
96-
store.dispatch(codePreviewCooperative(event.codePreviewCooperative));
94+
store.dispatch(
95+
codePreviewCooperative(parsedEvent.codePreviewCooperative)
96+
);
9797
} else if (
98-
JSON.stringify(currentStore.styleSlice) !==
99-
JSON.stringify(event.styleSlice)
98+
!areStatesEqual(currentStore.styleSlice, parsedEvent.styleSlice)
10099
) {
101-
store.dispatch(cooperativeStyle(event.styleSlice));
100+
store.dispatch(cooperativeStyle(parsedEvent.styleSlice));
102101
}
103102
}
104103
});
@@ -122,7 +121,7 @@ const RoomsContainer = () => {
122121
}
123122
}, 100);
124123

125-
//listening to changes from users in room, invoke handle store change
124+
//listening to changes from store from user, invoke handle store change.
126125
store.subscribe(() => {
127126
if (socket) {
128127
handleStoreChange();
@@ -139,20 +138,22 @@ const RoomsContainer = () => {
139138

140139
function leaveRoom() {
141140
if (socket) {
142-
socket.disconnect();
143-
} //disconnecting socket functionality
141+
socket.disconnect(); //disconnecting socket from server
142+
}
143+
//reset all state values
144144
dispatch(setRoomCode(''));
145145
dispatch(setUserName(''));
146146
dispatch(setUserList([]));
147147
dispatch(setUserJoined(false)); //setting joined to false so join button appear
148148
}
149149

150-
//checking if both text field have any input (not including spaces)
150+
//checking empty input field (not including spaces)
151151
function checkInputField(...inputs) {
152152
let userName: string = inputs[0].trim();
153153
let roomCode: string = inputs[1].trim();
154154
return userName.length === 0 || roomCode.length === 0;
155155
}
156+
156157
return (
157158
<div>
158159
<Stack //stack styling for container
@@ -224,7 +225,6 @@ const RoomsContainer = () => {
224225
) : (
225226
//after joinning room
226227
<>
227-
<></>
228228
<TextField
229229
hiddenLabel={true}
230230
id="filled-hidden-label-small"

server/controllers/sessionController.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const sessionController: SessionController = {
2626
if (!session) {
2727
res.locals.loggedIn = false;
2828
return next();
29-
// return res.redirect('/');
3029
}
3130
res.locals.loggedIn = true;
3231
return next();

server/server.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,17 @@ io.on('connection', (client) => {
119119
let newClientResponse = await io //send the requested host state to the new client
120120
.timeout(5000)
121121
.to(newClientID)
122-
.emitWithAck('back emitting state from host', hostState[0]); //sending state to the new client
122+
.emitWithAck('server emitting state from host', hostState[0]); //sending state to the new client
123123

124+
//client response is confirmed
124125
if (newClientResponse[0].status === 'confirmed') {
125126
client.join(roomCode); //client joining a room
126127
io.to(roomCode).emit('updateUserList', roomLists[roomCode]); //send the message to all clients in room but the sender
127128
}
128129
} catch (error) {
129-
//if joining event is having an error
130+
//if joining event is having an error and time out
130131
console.log(
131-
'the client did not acknowledge the event in the given delay:',
132+
'Request Timeout: Client failed to request state from host.',
132133
error
133134
);
134135
}

0 commit comments

Comments
 (0)