@@ -41,13 +41,16 @@ function Conversation() {
4141 useEffect ( ( ) => {
4242 if ( ! account ) return ;
4343
44+ const controller = new AbortController ( ) ;
45+
4446 const fetchPrompts = async ( ) => {
4547 if ( conversationId ) return ;
4648
4749 const idToken = await getIdToken ( account , instance ) ;
4850
4951 await client
5052 . get ( "/prompt" , {
53+ signal : controller . signal ,
5154 timeout : 10_000 ,
5255 headers : {
5356 Authorization : `Bearer ${ idToken } ` ,
@@ -67,6 +70,10 @@ function Conversation() {
6770 } ;
6871
6972 fetchPrompts ( ) ;
73+
74+ return ( ) => {
75+ if ( controller ) controller . abort ( ) ;
76+ }
7077 } , [ account , conversationId ] ) ;
7178
7279 useMemo ( ( ) => {
@@ -95,6 +102,8 @@ function Conversation() {
95102 useEffect ( ( ) => {
96103 if ( ! account ) return ;
97104
105+ const controller = new AbortController ( ) ;
106+
98107 const fetchConversation = async ( ) => {
99108 if ( ! conversationId ) {
100109 setConversation ( { messages : [ ] } ) ;
@@ -105,6 +114,7 @@ function Conversation() {
105114
106115 await client
107116 . get ( `/conversation/${ conversationId } ` , {
117+ signal : controller . signal ,
108118 timeout : 10_000 ,
109119 headers : {
110120 Authorization : `Bearer ${ idToken } ` ,
@@ -120,12 +130,19 @@ function Conversation() {
120130 } ;
121131
122132 fetchConversation ( ) ;
133+
134+ return ( ) => {
135+ if ( controller ) controller . abort ( ) ;
136+ }
123137 } , [ account , conversationId ] ) ;
124138
125139 const sendMessage = ( ) => {
126140 // Create a locache state cache, as state props wont't be updated until the next render
127141 let localMessages = [ ...conversation . messages ] ;
128142
143+ const controller = new AbortController ( ) ;
144+ let source ;
145+
129146 // Append the message to the list
130147 const addMessages = ( newMessages ) => {
131148 // Update local state cache
@@ -155,6 +172,8 @@ function Conversation() {
155172 // First, create the message
156173 await client
157174 . post ( "/message" , null , {
175+ signal : controller . signal ,
176+ timeout : 10_000 ,
158177 params : {
159178 content : input ,
160179 conversation_id : conversation ? conversation . id : null ,
@@ -163,7 +182,6 @@ function Conversation() {
163182 secret : secret ,
164183 language : userLang ,
165184 } ,
166- timeout : 10_000 ,
167185 headers : {
168186 Authorization : `Bearer ${ idToken } ` ,
169187 } ,
@@ -190,7 +208,7 @@ function Conversation() {
190208 const lastMessage = res . data . messages [ res . data . messages . length - 1 ] ;
191209 let content = "" ;
192210 let actions = [ ] ;
193- const source = new EventSource (
211+ source = new EventSource (
194212 `${ client . defaults . baseURL } /message/${ lastMessage . token } `
195213 ) ;
196214 source . onmessage = ( e ) => {
@@ -258,6 +276,11 @@ function Conversation() {
258276
259277 // Reset the input
260278 setInput ( "" ) ;
279+
280+ return ( ) => {
281+ if ( controller ) controller . abort ( ) ;
282+ if ( source ) source . close ( ) ;
283+ }
261284 } ;
262285
263286 // Scroll to the bottom of the page when the conversation changes
0 commit comments