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
+3 -2
View File
@@ -27,9 +27,10 @@ const OfferAssistantSession = ({ tender }) => {
const live = useRef(true);
const session = useRef(getToken());
const busy = useRef(false);
useEffect(() => { live.current = true; return () => { live.current = false; }; }, []);
const lifetime = useRef(new AbortController());
useEffect(() => { live.current = true; lifetime.current = new AbortController(); return () => { live.current = false; lifetime.current.abort(); }; }, []);
const current = () => live.current && Boolean(session.current) && getToken() === session.current;
const post = (url, body, documents) => requestOfferAssistant({url, body, readToken:getToken, isCurrent:current, listDocuments:documents});
const post = (url, body, documents) => requestOfferAssistant({url, body, readToken:getToken, isCurrent:current, listDocuments:documents, signal:lifetime.current.signal});
const [arac, setArac] = useState('sor');
const [metin, setMetin] = useState(''); // isteğe bağlı: yapıştırılan doküman metni
const [sourceName, setSourceName] = useState('');
+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); }
}