Skip to content

Commit 030194f

Browse files
NGdev2claude
andcommitted
Serialize file saves so an older write can't overwrite a newer one
Addresses review feedback: with autosave, a second save could start while the first was still writing. If the older write finished last it overwrote the newer content on disk with nothing left marked unsaved. handleFileSave now queues saves and runs them one at a time; each queued save reads the current content when it starts. A failed save doesn't block later ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent e7da2ba commit 030194f

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

‎frontend/app/view/preview/preview-model.tsx‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export class PreviewModel implements ViewModel {
140140
previewTextRef: React.RefObject<HTMLDivElement>;
141141
editMode: Atom<boolean>;
142142
autoSaveMode: Atom<AutoSaveMode>;
143+
saveQueue: Promise<void> = Promise.resolve();
143144
canPreview: PrimitiveAtom<boolean>;
144145
specializedView: Atom<Promise<{ specializedView?: string; errorStr?: string }>>;
145146
loadableSpecializedView: Atom<Loadable<{ specializedView?: string; errorStr?: string }>>;
@@ -658,7 +659,16 @@ export class PreviewModel implements ViewModel {
658659
await this.env.services.object.UpdateObjectMeta(blockOref, { ...blockMeta, edit });
659660
}
660661

661-
async handleFileSave() {
662+
handleFileSave(): Promise<void> {
663+
// run saves one at a time; a queued save reads newFileContent when it starts, so an
664+
// older write can never finish after (and overwrite) a newer one
665+
this.saveQueue = this.saveQueue
666+
.then(() => this.writeFileContent())
667+
.catch((e) => console.log("error saving file", e));
668+
return this.saveQueue;
669+
}
670+
671+
async writeFileContent() {
662672
const filePath = await globalStore.get(this.statFilePath);
663673
if (filePath == null) {
664674
return;

0 commit comments

Comments
 (0)