Merge pull request #17137 from acwoo97/feat/knowledge-update-race-condition

fix: prevent double-save race by awaiting API calls and adding isSaving guard
This commit is contained in:
Tim Jaeryang Baek
2025-09-03 13:36:51 +04:00
committed by GitHub

View File

@@ -115,6 +115,7 @@
let debounceTimeout = null; let debounceTimeout = null;
let mediaQuery; let mediaQuery;
let dragged = false; let dragged = false;
let isSaving = false;
const createFileFromText = (name, content) => { const createFileFromText = (name, content) => {
const blob = new Blob([content], { type: 'text/plain' }); const blob = new Blob([content], { type: 'text/plain' });
@@ -434,16 +435,21 @@
}; };
const updateFileContentHandler = async () => { const updateFileContentHandler = async () => {
if (isSaving) {
console.log('Save operation already in progress, skipping...');
return;
}
isSaving = true;
try {
const fileId = selectedFile.id; const fileId = selectedFile.id;
const content = selectedFileContent; const content = selectedFileContent;
// Clear the cache for this file since we're updating it // Clear the cache for this file since we're updating it
fileContentCache.delete(fileId); fileContentCache.delete(fileId);
const res = await updateFileDataContentById(localStorage.token, fileId, content).catch(
const res = updateFileDataContentById(localStorage.token, fileId, content).catch((e) => { (e) => {
toast.error(`${e}`); toast.error(`${e}`);
}); }
);
const updatedKnowledge = await updateFileFromKnowledgeById( const updatedKnowledge = await updateFileFromKnowledgeById(
localStorage.token, localStorage.token,
id, id,
@@ -451,11 +457,13 @@
).catch((e) => { ).catch((e) => {
toast.error(`${e}`); toast.error(`${e}`);
}); });
if (res && updatedKnowledge) { if (res && updatedKnowledge) {
knowledge = updatedKnowledge; knowledge = updatedKnowledge;
toast.success($i18n.t('File content updated successfully.')); toast.success($i18n.t('File content updated successfully.'));
} }
} finally {
isSaving = false;
}
}; };
const changeDebounceHandler = () => { const changeDebounceHandler = () => {
@@ -779,12 +787,13 @@
<div> <div>
<button <button
class="self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg" class="self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
disabled={isSaving}
on:click={() => { on:click={() => {
updateFileContentHandler(); updateFileContentHandler();
}} }}
> >
{$i18n.t('Save')} {isSaving ? $i18n.t('Running...') : $i18n.t('Save')}
</button> </button>
</div> </div>
</div> </div>
@@ -836,12 +845,13 @@
<div> <div>
<button <button
class="self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg" class="self-center w-fit text-sm py-1 px-2.5 dark:text-gray-300 dark:hover:text-white hover:bg-black/5 dark:hover:bg-white/5 rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
disabled={isSaving}
on:click={() => { on:click={() => {
updateFileContentHandler(); updateFileContentHandler();
}} }}
> >
{$i18n.t('Save')} {isSaving ? $i18n.t('Running...') : $i18n.t('Save')}
</button> </button>
</div> </div>
</div> </div>