feat(Index.tsx): implement abort controller for text correction to prevent race conditions

This commit is contained in:
2025-08-29 18:15:55 +02:00
parent 333d44e621
commit f27f897358

View File

@@ -124,6 +124,7 @@ $current`);
const [correctedContent, setCorrectedContent] = useState(''); const [correctedContent, setCorrectedContent] = useState('');
const [previousNoteCorrectedContent, setPreviousNoteCorrectedContent] = useState(''); const [previousNoteCorrectedContent, setPreviousNoteCorrectedContent] = useState('');
const [correctionTimeout, setCorrectionTimeout] = useState<NodeJS.Timeout>(); const [correctionTimeout, setCorrectionTimeout] = useState<NodeJS.Timeout>();
const [correctionAbortController, setCorrectionAbortController] = useState<AbortController | null>(null);
const { resolvedTheme, setTheme } = useTheme(); const { resolvedTheme, setTheme } = useTheme();
@@ -338,20 +339,38 @@ $current`);
// Debounced text correction for previous note // Debounced text correction for previous note
const debouncedCorrectPreviousNote = (content: string) => { const debouncedCorrectPreviousNote = (content: string) => {
// Cancel any pending correction request
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
}
if (correctionTimeout) { if (correctionTimeout) {
clearTimeout(correctionTimeout); clearTimeout(correctionTimeout);
} }
// Create new AbortController for this request
const abortController = new AbortController();
setCorrectionAbortController(abortController);
const timeout = setTimeout(async () => { const timeout = setTimeout(async () => {
if (content.trim()) { if (content.trim()) {
try { try {
addDebugInfo('Correcting previous note text...'); addDebugInfo('Correcting previous note text...');
const corrected = await correctText(content); const corrected = await correctText(content, abortController);
setPreviousNoteCorrectedContent(corrected); setPreviousNoteCorrectedContent(corrected);
} catch (error) { } catch (error) {
if (error instanceof Error && error.name === 'AbortError') {
addDebugInfo('Previous note correction cancelled');
return; // Don't set empty content for aborted requests
} else {
console.error('Previous note text correction failed:', error); console.error('Previous note text correction failed:', error);
setPreviousNoteCorrectedContent(''); setPreviousNoteCorrectedContent('');
} }
}
} else { } else {
setPreviousNoteCorrectedContent(''); setPreviousNoteCorrectedContent('');
} }
@@ -361,7 +380,7 @@ $current`);
}; };
// Correct text using Ollama // Correct text using Ollama
const correctText = async (content: string): Promise<string> => { const correctText = async (content: string, abortController?: AbortController): Promise<string> => {
try { try {
const response = await fetchWithTiming(`${ollamaEndpoint}/api/generate`, { const response = await fetchWithTiming(`${ollamaEndpoint}/api/generate`, {
method: 'POST', method: 'POST',
@@ -387,6 +406,7 @@ ${content}`,
keep_alive: ollamaKeepAlive, keep_alive: ollamaKeepAlive,
temperature: 0.1, temperature: 0.1,
}), }),
signal: abortController?.signal,
}, 'Correct Text'); }, 'Correct Text');
if (!response.ok) { if (!response.ok) {
@@ -1169,6 +1189,16 @@ ${content}`,
const reversedDelta = -delta; const reversedDelta = -delta;
const newIndex = Math.max(0, Math.min(currentNoteIndex + reversedDelta, noteCache.length - 1)); const newIndex = Math.max(0, Math.min(currentNoteIndex + reversedDelta, noteCache.length - 1));
if (newIndex !== currentNoteIndex && noteCache[newIndex]) { if (newIndex !== currentNoteIndex && noteCache[newIndex]) {
// Cancel any pending correction request before changing notes
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
setCorrectionAbortController(null);
}
setCurrentNoteIndex(newIndex); setCurrentNoteIndex(newIndex);
setPreviousNote(noteCache[newIndex]); setPreviousNote(noteCache[newIndex]);
setIsPreviousNoteModified(false); setIsPreviousNoteModified(false);
@@ -1355,6 +1385,14 @@ ${content}`,
if (correctionTimeout) { if (correctionTimeout) {
clearTimeout(correctionTimeout); clearTimeout(correctionTimeout);
} }
// Cancel any pending correction request on cleanup
if (correctionAbortController) {
try {
correctionAbortController.abort();
} catch (error) {
// Ignore abort errors - the controller might already be aborted
}
}
}; };
}, [currentNote, previousNote, scratchPad, isPreviousNoteModified, isSearchOpen, isGotoOpen, isCleanupOpen]); }, [currentNote, previousNote, scratchPad, isPreviousNoteModified, isSearchOpen, isGotoOpen, isCleanupOpen]);