@@ -39,20 +39,16 @@ const RoomsContainer = () => {
39
39
} )
40
40
) ;
41
41
42
- React . useEffect ( ( ) => {
43
- console . log ( 'You Joined Room---:' , roomCode ) ;
44
- } , [ roomCode ] ) ;
45
-
46
42
function initSocketConnection ( roomCode : string ) {
47
43
if ( socket ) socket . disconnect ( ) ; //edge case check if socket connection existed
48
44
49
45
socket = io ( API_BASE_URL , {
50
- //establishing client and server
46
+ //establishing client and server connection
51
47
transports : [ 'websocket' ]
52
48
} ) ;
53
49
50
+ //connecting user to server
54
51
socket . on ( 'connect' , ( ) => {
55
- //connecting user to server
56
52
socket . emit ( 'joining' , userName , roomCode ) ;
57
53
console . log ( `${ userName } Joined room ${ roomCode } ` ) ;
58
54
} ) ;
@@ -64,7 +60,7 @@ const RoomsContainer = () => {
64
60
callback ( newState ) ; //pull new state from host and send it to back end
65
61
} ) ;
66
62
67
- socket . on ( 'back emitting state from host' , ( state , callback ) => {
63
+ socket . on ( 'server emitting state from host' , ( state , callback ) => {
68
64
//getting state from host once joined a room
69
65
//dispatching new state to change user current state
70
66
store . dispatch ( allCooperativeState ( state . appState ) ) ;
@@ -80,25 +76,28 @@ const RoomsContainer = () => {
80
76
81
77
// receiving the message from the back end
82
78
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 ) ) ;
92
88
} else if (
93
- JSON . stringify ( currentStore . codePreviewSlice ) !==
94
- JSON . stringify ( event . codePreviewCooperative )
89
+ ! areStatesEqual (
90
+ currentStore . codePreviewSlice ,
91
+ parsedEvent . codePreviewCooperative
92
+ )
95
93
) {
96
- store . dispatch ( codePreviewCooperative ( event . codePreviewCooperative ) ) ;
94
+ store . dispatch (
95
+ codePreviewCooperative ( parsedEvent . codePreviewCooperative )
96
+ ) ;
97
97
} else if (
98
- JSON . stringify ( currentStore . styleSlice ) !==
99
- JSON . stringify ( event . styleSlice )
98
+ ! areStatesEqual ( currentStore . styleSlice , parsedEvent . styleSlice )
100
99
) {
101
- store . dispatch ( cooperativeStyle ( event . styleSlice ) ) ;
100
+ store . dispatch ( cooperativeStyle ( parsedEvent . styleSlice ) ) ;
102
101
}
103
102
}
104
103
} ) ;
@@ -122,7 +121,7 @@ const RoomsContainer = () => {
122
121
}
123
122
} , 100 ) ;
124
123
125
- //listening to changes from users in room , invoke handle store change
124
+ //listening to changes from store from user , invoke handle store change.
126
125
store . subscribe ( ( ) => {
127
126
if ( socket ) {
128
127
handleStoreChange ( ) ;
@@ -139,20 +138,22 @@ const RoomsContainer = () => {
139
138
140
139
function leaveRoom ( ) {
141
140
if ( socket ) {
142
- socket . disconnect ( ) ;
143
- } //disconnecting socket functionality
141
+ socket . disconnect ( ) ; //disconnecting socket from server
142
+ }
143
+ //reset all state values
144
144
dispatch ( setRoomCode ( '' ) ) ;
145
145
dispatch ( setUserName ( '' ) ) ;
146
146
dispatch ( setUserList ( [ ] ) ) ;
147
147
dispatch ( setUserJoined ( false ) ) ; //setting joined to false so join button appear
148
148
}
149
149
150
- //checking if both text field have any input (not including spaces)
150
+ //checking empty input field (not including spaces)
151
151
function checkInputField ( ...inputs ) {
152
152
let userName : string = inputs [ 0 ] . trim ( ) ;
153
153
let roomCode : string = inputs [ 1 ] . trim ( ) ;
154
154
return userName . length === 0 || roomCode . length === 0 ;
155
155
}
156
+
156
157
return (
157
158
< div >
158
159
< Stack //stack styling for container
@@ -224,7 +225,6 @@ const RoomsContainer = () => {
224
225
) : (
225
226
//after joinning room
226
227
< >
227
- < > </ >
228
228
< TextField
229
229
hiddenLabel = { true }
230
230
id = "filled-hidden-label-small"
0 commit comments