Skip to content

Commit 0591977

Browse files
committed
Perf: Cancel HTTP calls when UI switch context
1 parent 2ae08f3 commit 0591977

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/conversation-ui/src/App.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,12 @@ function App() {
5959
const fetchConversations = async (idToSelect = null) => {
6060
if (!account) return;
6161

62+
const controller = new AbortController();
6263
const idToken = await getIdToken(account, instance);
6364

6465
await client
6566
.get("/conversation", {
67+
signal: controller.signal,
6668
timeout: 10_000,
6769
headers: {
6870
Authorization: `Bearer ${idToken}`,
@@ -93,6 +95,10 @@ function App() {
9395
.catch((err) => {
9496
console.error(err);
9597
});
98+
99+
return () => {
100+
if (controller) controller.abort();
101+
}
96102
};
97103

98104
useEffect(() => {

src/conversation-ui/src/Conversation.jsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/conversation-ui/src/Search.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,16 @@ function Search() {
2323

2424
setLoading(true);
2525

26+
const controller = new AbortController();
2627
const idToken = await getIdToken(account, instance);
2728

2829
await client
2930
.get("/message", {
31+
signal: controller.signal,
32+
timeout: 10_000,
3033
params: {
3134
q: input,
3235
},
33-
timeout: 10_000,
3436
headers: {
3537
Authorization: `Bearer ${idToken}`,
3638
},
@@ -45,6 +47,10 @@ function Search() {
4547
.finally(() => {
4648
setLoading(false);
4749
});
50+
51+
return () => {
52+
if (controller) controller.abort();
53+
}
4854
};
4955

5056
useMemo(() => {

0 commit comments

Comments
 (0)