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,27 +435,34 @@
}; };
const updateFileContentHandler = async () => { const updateFileContentHandler = async () => {
const fileId = selectedFile.id; if (isSaving) {
const content = selectedFileContent; console.log('Save operation already in progress, skipping...');
return;
// Clear the cache for this file since we're updating it }
fileContentCache.delete(fileId); isSaving = true;
try {
const res = updateFileDataContentById(localStorage.token, fileId, content).catch((e) => { const fileId = selectedFile.id;
toast.error(`${e}`); const content = selectedFileContent;
}); // Clear the cache for this file since we're updating it
fileContentCache.delete(fileId);
const updatedKnowledge = await updateFileFromKnowledgeById( const res = await updateFileDataContentById(localStorage.token, fileId, content).catch(
localStorage.token, (e) => {
id, toast.error(`${e}`);
fileId }
).catch((e) => { );
toast.error(`${e}`); const updatedKnowledge = await updateFileFromKnowledgeById(
}); localStorage.token,
id,
if (res && updatedKnowledge) { fileId
knowledge = updatedKnowledge; ).catch((e) => {
toast.success($i18n.t('File content updated successfully.')); toast.error(`${e}`);
});
if (res && updatedKnowledge) {
knowledge = updatedKnowledge;
toast.success($i18n.t('File content updated successfully.'));
}
} finally {
isSaving = false;
} }
}; };
@@ -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>