@@ -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"
0 commit comments