File tree Expand file tree Collapse file tree 1 file changed +10
-1
lines changed
kilo-app/src/components/kiloclaw Expand file tree Collapse file tree 1 file changed +10
-1
lines changed Original file line number Diff line number Diff line change @@ -155,13 +155,23 @@ function StreamChatUI({
155155 const chatClient = StreamChat . getInstance ( apiKey ) ;
156156
157157 let cancelled = false ;
158+ setConnectError ( null ) ;
158159
159160 const connect = async ( ) => {
160161 try {
162+ // Await disconnect to prevent tokenManager.reset() from racing with the new connection
163+ if ( chatClient . userID ) {
164+ await chatClient . disconnectUser ( ) ;
165+ }
166+ if ( cancelled ) {
167+ return ;
168+ }
161169 await chatClient . connectUser ( { id : userId } , tokenProvider ) ;
162170 const ch = chatClient . channel ( 'messaging' , channelId ) ;
163171 await ch . watch ( { presence : true } ) ;
172+ // cancelled may change across awaits above
164173 if ( ! cancelled ) {
174+ // eslint-disable-line typescript-eslint/no-unnecessary-condition
165175 setClient ( chatClient ) ;
166176 setChannel ( ch ) ;
167177 }
@@ -176,7 +186,6 @@ function StreamChatUI({
176186
177187 return ( ) => {
178188 cancelled = true ;
179- void chatClient . disconnectUser ( ) ;
180189 setClient ( null ) ;
181190 setChannel ( null ) ;
182191 } ;
You can’t perform that action at this time.
0 commit comments