Cancel disconnected offer analysis and stop remaining provider calls

This commit is contained in:
2026-09-06 10:22:29 +03:00
parent 673d6c1bc8
commit d534ea20b9
10 changed files with 110 additions and 22 deletions
+9 -4
View File
@@ -1,8 +1,9 @@
import { validateOfferResponse } from './offerResponse.js';
/** Bind the entire multi-step request to its starting account and mounted view. */
export async function requestOfferAssistant({url, body, readToken, isCurrent = () => true, listDocuments, fetcher = fetch, timeoutMs = 120000}) {
export async function requestOfferAssistant({url, body, readToken, isCurrent = () => true, listDocuments, fetcher = fetch, timeoutMs = 120000, signal}) {
const token = readToken();
const check = () => {
signal?.throwIfAborted();
if (!token || readToken() !== token || !isCurrent())
throw new Error('Oturum veya ihale değişti. İşlemi güncel ekrandan yeniden başlatın.');
};
@@ -19,7 +20,10 @@ export async function requestOfferAssistant({url, body, readToken, isCurrent = (
}
check();
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const cancel = () => controller.abort();
signal?.addEventListener('abort',cancel,{once:true});
let timedOut=false;
const timer = setTimeout(() => {timedOut=true;controller.abort();}, timeoutMs);
try {
const response = await fetcher(url, {signal:controller.signal, method:'POST', headers:{'Content-Type':'application/json', Authorization:`Bearer ${token}`}, body:JSON.stringify(payload)});
const data = await response.json().catch(() => { throw new Error('Sunucudan okunabilir JSON yanıtı alınamadı. Yeniden deneyin.'); });
@@ -28,7 +32,8 @@ export async function requestOfferAssistant({url, body, readToken, isCurrent = (
if (!response.ok || data?.success === false) throw new Error(typeof data?.error === 'string' ? data.error : `HTTP ${response.status}`);
return validateOfferResponse(url, data);
} catch (error) {
if (controller.signal.aborted) throw new Error('AI yanıtı zaman aşımına uğradı. Önceki çalışmanız korundu; yeniden deneyin.');
if (timedOut) throw new Error('AI yanıtı zaman aşımına uğradı. Önceki çalışmanız korundu; yeniden deneyin.');
signal?.throwIfAborted();
throw error;
} finally { clearTimeout(timer); }
} finally { clearTimeout(timer);signal?.removeEventListener('abort',cancel); }
}