|
245 | 245 | error = undefined; |
246 | 246 |
|
247 | 247 | const endpoint = endpoints.find((e) => e.id === selectedEndpoint); |
248 | | - if (!endpoint) return; |
| 248 | + if (!endpoint) { |
| 249 | + loading = false; |
| 250 | + return; |
| 251 | + } |
249 | 252 |
|
| 253 | + let timeoutId: number | undefined = undefined; |
| 254 | + let didTimeout = false; |
250 | 255 | try { |
251 | 256 | const url1 = buildUrl(server1Base, endpoint, params); |
252 | 257 | const url2 = buildUrl(server2Base, endpoint, params); |
253 | 258 |
|
254 | | - const response = await fetch('/api/proxy', { |
| 259 | + const timeoutPromise = new Promise((_, reject) => { |
| 260 | + timeoutId = window.setTimeout(() => { |
| 261 | + didTimeout = true; |
| 262 | + reject(new Error('Request timed out after 5 seconds')); |
| 263 | + }, 5000); |
| 264 | + }); |
| 265 | +
|
| 266 | + const fetchPromise = fetch('/api/proxy', { |
255 | 267 | method: 'POST', |
256 | 268 | headers: { 'Content-Type': 'application/json' }, |
257 | 269 | body: JSON.stringify({ url1, url2 }) |
258 | 270 | }); |
259 | 271 |
|
260 | | - const data = await response.json(); |
| 272 | + const response = await Promise.race([fetchPromise, timeoutPromise]); |
| 273 | + if (didTimeout) return; |
| 274 | +
|
| 275 | + if (timeoutId !== undefined) clearTimeout(timeoutId); |
| 276 | +
|
| 277 | + const data = await (response as Response).json(); |
261 | 278 |
|
262 | 279 | if (data.error) { |
263 | 280 | error = data.error; |
|
269 | 286 |
|
270 | 287 | await logWatchedKeys(data.response1, data.response2); |
271 | 288 | } catch (e) { |
272 | | - error = e instanceof Error ? e.message : 'Unknown error'; |
| 289 | + if (didTimeout) { |
| 290 | + error = 'Request timed out. Please try again.'; |
| 291 | + } else { |
| 292 | + error = e instanceof Error ? e.message : 'Unknown error'; |
| 293 | + } |
273 | 294 | } finally { |
| 295 | + if (timeoutId !== undefined) clearTimeout(timeoutId); |
274 | 296 | loading = false; |
275 | 297 | } |
276 | 298 | } |
|
0 commit comments