From bac3d587780175acd26ba862ab1faf1e4809a489 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 17:33:39 +0000 Subject: [PATCH 1/6] Harden what happens when saving to the database fails Three gaps from the review of home-page: - A request failing inside create, update or delete left the transaction's completion promise rejecting with nobody listening, so each failure was reported a second time as an unhandled rejection. The promise is now marked handled; the caller still gets the request's error. - Closing a tab never warned once the database was in use, on the grounds that every edit is already stored. After a failed save that is not true. The storage now tracks which files a failed flush left unsaved, until they are next saved, and the tab warns before closing while any remain. - The cross-tab change handler refreshed the project list first and gave up if that failed, so a delete in another tab could go unnoticed. It now logs the failure and carries on to deal with the open project. --- docs/tech-overview.md | 4 +- src/e2e/storage-errors.test.ts | 51 +++++++++++++++++++----- src/fs/indexeddb-storage.test.ts | 17 ++++++++ src/fs/indexeddb-storage.ts | 28 +++++++++++++ src/fs/projects-db.test.ts | 21 ++++++++++ src/fs/projects-db.ts | 18 +++++++-- src/project/projects.test.ts | 37 +++++++++++++++++ src/project/projects.ts | 16 +++++++- src/workbench/BeforeUnloadDirtyCheck.tsx | 17 ++++---- 9 files changed, 186 insertions(+), 23 deletions(-) diff --git a/docs/tech-overview.md b/docs/tech-overview.md index cf4df7b7f..12a3e1e99 100644 --- a/docs/tech-overview.md +++ b/docs/tech-overview.md @@ -57,7 +57,7 @@ Edits are written through as they happen. There is no separate "save" action for - Writes are coalesced per file for 300ms, then flushed as one transaction. The project's timestamp is the time of the edit, so "last modified" does not depend on when the flush ran. - Pending writes are also flushed when the page is hidden or unloading. A transaction that waits on a read, or on its requests finishing, does not survive the page unloading. So a flush never reads before it writes, and one from a hidden or unloading page commits explicitly. This is why the storage holds the project's name and writes the whole project record each time, rather than reading the record and updating it. - A flush at any other time checks, in the same transaction, that the project still exists. -- A failed flush is not retried. The in-memory copy still has the edit, the user sees a toast that stays until dismissed ("Browser storage full" when the quota is exceeded), and the error is logged once per session. +- A failed flush is not retried. The in-memory copy still has the edit, the user sees a toast that stays until dismissed ("Browser storage full" when the quota is exceeded), and the error is logged once per session. Until the same files are next saved successfully, the user is warned before closing the tab. MicroPython itself is only needed to build a hex file or flash, not to edit. If it fails to load, projects still open and save, and loading is tried again the next time something needs it. @@ -102,7 +102,7 @@ Some choices that are not obvious from the code: - **The editor opens straight to code.** `/project` in a new tab opens the most recent project rather than asking the user to pick one. The editor has always been quick to get into, and some users bookmark it. - **No project id in the URL.** The tab's project is kept in session storage instead. This may be revisited if projects are ever stored on a server. - **Each deployment has its own database.** The database name includes the base path, so deployments that share an origin, such as production and beta, keep separate projects. -- **No warning before closing a tab.** The editor used to warn when a tab was closed with changes made since the last hex file was loaded or saved, because closing the tab lost them. With the database every edit is already stored, so there is nothing to warn about. The warning remains where there is no database. +- **No warning before closing a tab.** The editor used to warn when a tab was closed with changes made since the last hex file was loaded or saved, because closing the tab lost them. With the database every edit is already stored, so there is nothing to warn about unless a save has failed. The warning remains where there is no database. - **Imports never overwrite silently.** With the database, a hex or an idea makes a new project rather than replacing the open one. Without it there is only one project, so the editor asks before replacing one that has been edited: in micro:bit classroom a student's dropped hex would otherwise replace the teacher's starter program without a word. Adding files that would overwrite existing ones asks first in every mode. - **A project changed in another tab is reloaded, not merged.** The last write wins. diff --git a/src/e2e/storage-errors.test.ts b/src/e2e/storage-errors.test.ts index 9d0d755a5..d1fd32b2e 100644 --- a/src/e2e/storage-errors.test.ts +++ b/src/e2e/storage-errors.test.ts @@ -85,20 +85,27 @@ test.describe("storage fallback", () => { }); /** - * Makes the next IndexedDB put throw, then restores it. The first put in a - * project flush is the project record, so this fails the whole flush. + * Makes the next IndexedDB put throw, then restores it, or with `every` + * makes them all throw. The first put in a project flush is the project + * record, so this fails the whole flush. */ const injectWriteError = async ( app: App, - errorName: "QuotaExceededError" | "UnknownError" + errorName: "QuotaExceededError" | "UnknownError", + { every = false }: { every?: boolean } = {} ) => { - await app.page.evaluate((errorName) => { - const original = IDBObjectStore.prototype.put; - IDBObjectStore.prototype.put = function () { - IDBObjectStore.prototype.put = original; - throw new DOMException(`Simulated ${errorName}`, errorName); - }; - }, errorName); + await app.page.evaluate( + ({ errorName, every }) => { + const original = IDBObjectStore.prototype.put; + IDBObjectStore.prototype.put = function () { + if (!every) { + IDBObjectStore.prototype.put = original; + } + throw new DOMException(`Simulated ${errorName}`, errorName); + }; + }, + { errorName, every } + ); }; /** @@ -169,4 +176,28 @@ test.describe("storage read errors", () => { await homePage.cards.open("Untitled project"); await app.expectEditorContainText("from microbit import"); }); + + test("warns before closing while an edit has not saved", async ({ app }) => { + await injectWriteError(app, "UnknownError", { every: true }); + await app.typeInEditor("A change!"); + await expect( + app.page.getByText("Failed to save your project to browser storage") + ).toBeVisible(); + + await app.closeAndExpectBeforeUnloadPrompt(); + }); + + test("does not warn once a later save has succeeded", async ({ app }) => { + // Only the first save fails. typeInEditor's last keys are typed slowly + // enough to be saved again, successfully, after it. + await injectWriteError(app, "UnknownError"); + await app.typeInEditor("A change!"); + await expect( + app.page.getByText("Failed to save your project to browser storage") + ).toBeVisible(); + // Past the storage's flush delay, so the last save has finished. + await app.page.waitForTimeout(600); + + await app.closeWithoutBeforeUnloadPrompt(); + }); }); diff --git a/src/fs/indexeddb-storage.test.ts b/src/fs/indexeddb-storage.test.ts index 8efae93bc..ad520dc07 100644 --- a/src/fs/indexeddb-storage.test.ts +++ b/src/fs/indexeddb-storage.test.ts @@ -172,6 +172,23 @@ describe("IndexedDBFSStorage", () => { expect((await db.get(projectId))?.name).toEqual("Renamed here"); }); + it("knows what a failed flush left unsaved, until it is next saved", async () => { + expect(storage.hasUnsavedChanges).toEqual(false); + vi.spyOn(db, "save").mockRejectedValueOnce(new Error("Simulated")); + await storage.write("main.py", new Uint8Array([1])); + await storage.flush(); + expect(storage.hasUnsavedChanges).toEqual(true); + + // Another file saving does not save the one that failed. + await storage.write("other.py", new Uint8Array([2])); + await storage.flush(); + expect(storage.hasUnsavedChanges).toEqual(true); + + await storage.write("main.py", new Uint8Array([3])); + await storage.flush(); + expect(storage.hasUnsavedChanges).toEqual(false); + }); + it("does not write back a project deleted elsewhere", async () => { await db.delete(projectId); await storage.write("main.py", new Uint8Array([1])); diff --git a/src/fs/indexeddb-storage.ts b/src/fs/indexeddb-storage.ts index 2c3c2bec2..9e9c845a2 100644 --- a/src/fs/indexeddb-storage.ts +++ b/src/fs/indexeddb-storage.ts @@ -41,6 +41,10 @@ export class IndexedDBFSStorage implements FSStorage { private timer: ReturnType | undefined; private flushing: Promise = Promise.resolve(); private abandoned = false; + // What a failed flush left out of the database and no flush has written + // since: files by name, and the project record. + private unsavedFiles = new Set(); + private unsavedRecord = false; private readonly handleVisibilityChange = () => { if (document.visibilityState === "hidden") { void this.flush({ urgent: true }); @@ -93,6 +97,17 @@ export class IndexedDBFSStorage implements FSStorage { this.timer = undefined; this.pendingWrites = new Map(); this.pendingSince = undefined; + this.unsavedFiles = new Set(); + this.unsavedRecord = false; + } + + /** + * True while a failed flush has left something out of the database, so + * that closing the tab now would lose it. A failed flush is not retried, + * so this lasts until the same files are next written successfully. + */ + get hasUnsavedChanges(): boolean { + return this.unsavedFiles.size > 0 || this.unsavedRecord; } /** @@ -221,8 +236,21 @@ export class IndexedDBFSStorage implements FSStorage { this.pendingWrites = new Map(); this.pendingSince = undefined; this.nameUnsaved = false; + const names = [...Object.keys(files.writes), ...files.deletes]; const saved = this.db .save(project, files, { urgent }) + .then( + () => { + names.forEach((name) => this.unsavedFiles.delete(name)); + // Every flush writes the whole record. + this.unsavedRecord = false; + }, + (e) => { + names.forEach((name) => this.unsavedFiles.add(name)); + this.unsavedRecord = true; + throw e; + } + ) .then(() => this.onChange()) .catch((e) => this.onError(e)); const earlier = this.flushing; diff --git a/src/fs/projects-db.test.ts b/src/fs/projects-db.test.ts index 1fb17675c..5b42fbf8d 100644 --- a/src/fs/projects-db.test.ts +++ b/src/fs/projects-db.test.ts @@ -120,6 +120,27 @@ describe("ProjectsDatabase", () => { expect(contents(await db.files("a"))).toEqual({ "main.py": [2] }); }); + it("leaves no unhandled rejection when a request in a transaction fails", async () => { + await db.create(meta("a", 1), {}); + const unhandled: unknown[] = []; + const listener = (reason: unknown) => unhandled.push(reason); + process.on("unhandledRejection", listener); + // The next put fails as a request, as a put can when storage is full, + // rather than as a call: it adds a key that already exists. + const put = IDBObjectStore.prototype.put; + IDBObjectStore.prototype.put = function (this: IDBObjectStore, value) { + IDBObjectStore.prototype.put = put; + return this.add(value); + }; + + await expect(db.create(meta("a", 2), {})).rejects.toThrow(); + await new Promise((resolve) => setTimeout(resolve, 20)); + + process.off("unhandledRejection", listener); + expect(unhandled).toEqual([]); + expect(await db.get("a")).toEqual(meta("a", 1)); + }); + it("touch makes a project the most recent", async () => { await db.create(meta("a", 1), {}); await db.create(meta("b", 2), {}); diff --git a/src/fs/projects-db.ts b/src/fs/projects-db.ts index 9e6edc408..bfb968cea 100644 --- a/src/fs/projects-db.ts +++ b/src/fs/projects-db.ts @@ -85,6 +85,18 @@ export const databaseName = ( : `python-editor${base.replace(/\/$/, "")}`; }; +/** + * idb gives every transaction a promise of its completion, tx.done. A method + * that awaits a request and throws when it fails never reaches its own + * `await tx.done`, and the transaction's rejection would then be unhandled: + * one more error report for a failure the caller has already been given. + * This marks it handled. Awaiting tx.done still rejects. + */ +const handled = }>(tx: T): T => { + tx.done.catch(() => {}); + return tx; +}; + export class ProjectsDatabase { private constructor(private db: IDBPDatabase) {} @@ -192,7 +204,7 @@ export class ProjectsDatabase { meta: ProjectMeta, files: Record ): Promise { - const tx = this.db.transaction(stores, "readwrite"); + const tx = handled(this.db.transaction(stores, "readwrite")); await tx.objectStore(PROJECTS).put(meta); const fileStore = tx.objectStore(FILES); for (const [name, data] of Object.entries(files)) { @@ -209,7 +221,7 @@ export class ProjectsDatabase { id: string, changes: Partial> ): Promise { - const tx = this.db.transaction(PROJECTS, "readwrite"); + const tx = handled(this.db.transaction(PROJECTS, "readwrite")); const existing = await tx.store.get(id); if (!existing) { throw new Error(`No such project ${id}`); @@ -295,7 +307,7 @@ export class ProjectsDatabase { } async delete(id: string): Promise { - const tx = this.db.transaction(stores, "readwrite"); + const tx = handled(this.db.transaction(stores, "readwrite")); await tx.objectStore(PROJECTS).delete(id); const fileStore = tx.objectStore(FILES); for (const key of await fileStore.index("projectId").getAllKeys(id)) { diff --git a/src/project/projects.test.ts b/src/project/projects.test.ts index dcec9f49d..edd8bcfb2 100644 --- a/src/project/projects.test.ts +++ b/src/project/projects.test.ts @@ -205,6 +205,21 @@ describe("Projects", () => { expect(fs.project.name).toEqual("Other"); }); + it("has unsaved changes while a failed save has not been made good", async () => { + const { db, fs, projects } = await setup(); + await projects.openCurrent(); + await fs.initialize(); + expect(projects.hasUnsavedChanges).toEqual(false); + vi.spyOn(logging, "error").mockImplementation(() => undefined); + vi.spyOn(db!, "save").mockRejectedValueOnce(new Error("Simulated")); + + await fs.write(MAIN_FILE, "# one", VersionAction.MAINTAIN); + await vi.waitFor(() => expect(projects.hasUnsavedChanges).toEqual(true)); + + await fs.write(MAIN_FILE, "# two", VersionAction.MAINTAIN); + await vi.waitFor(() => expect(projects.hasUnsavedChanges).toEqual(false)); + }); + it("a failed save raises saveerror each time but is logged once", async () => { const { db, fs, projects } = await setup(); await projects.openCurrent(); @@ -338,6 +353,28 @@ describe("Projects", () => { return tabs; }; + it("still lets go of a deleted project if refreshing the list fails", async () => { + const { a, b, id } = await twoTabs(); + const logged = vi + .spyOn(logging, "error") + .mockImplementation(() => undefined); + vi.spyOn(b.projects, "refresh").mockRejectedValueOnce( + new Error("Simulated") + ); + const deleted = new Promise((resolve) => + b.projects.addEventListener("openprojectdeleted", resolve) + ); + + await a.projects.delete([id]); + await deleted; + + expect(b.projects.currentId).toBeUndefined(); + expect(logged).toHaveBeenCalledWith( + "Failed to refresh the project list", + expect.any(Error) + ); + }); + it("still lets go of it if it is deleted while reloading", async () => { let deleted: Promise | undefined; const errors: unknown[] = []; diff --git a/src/project/projects.ts b/src/project/projects.ts index 2e819cf15..6eb2a4f5a 100644 --- a/src/project/projects.ts +++ b/src/project/projects.ts @@ -167,6 +167,14 @@ export class Projects extends TypedEventTarget { return this.projectLink.pending; } + /** + * True while an edit to the open project has failed to save and has not + * been saved since, so that closing the tab would lose it. + */ + get hasUnsavedChanges(): boolean { + return this.openStorage?.hasUnsavedChanges ?? false; + } + /** The project the editor has open in this tab, if any. */ get currentId(): string | undefined { return this.openId; @@ -409,7 +417,13 @@ export class Projects extends TypedEventTarget { private handleSyncMessage = (event: MessageEvent) => { void (async () => { const { type, projectIds } = event.data; - await this.refresh(); + try { + await this.refresh(); + } catch (e) { + // The list is stale, but what the notice means for the open project + // still has to be acted on: it may have been deleted. + this.logging.error("Failed to refresh the project list", e); + } const id = this.openId; if (!id || !projectIds.includes(id)) { return; diff --git a/src/workbench/BeforeUnloadDirtyCheck.tsx b/src/workbench/BeforeUnloadDirtyCheck.tsx index f96e94a48..e73d47b3e 100644 --- a/src/workbench/BeforeUnloadDirtyCheck.tsx +++ b/src/workbench/BeforeUnloadDirtyCheck.tsx @@ -6,21 +6,24 @@ import { useEffect } from "react"; import { useFileSystem } from "../fs/fs-hooks"; import { useProjectsDatabaseActive } from "../fs/storage-status"; +import { useProjectsIfAvailable } from "../project/projects-hooks"; /** * Warns the user before closing a tab if they've made changes that would be - * lost. Nothing is lost when the project is in the projects database, so the - * warning only applies to the session-storage fallback and iframe mode. + * lost. + * + * With the projects database, edits are saved as they are made, so the only + * ones at risk are those that failed to save. With the session-storage + * fallback and in iframe mode, anything since the last hex save is. */ const BeforeUnloadDirtyCheck = () => { const fs = useFileSystem(); + const projects = useProjectsIfAvailable(); const persisted = useProjectsDatabaseActive(); useEffect(() => { - if (persisted) { - return; - } const listener = (e: BeforeUnloadEvent) => { - if (fs.dirty) { + const atRisk = persisted ? projects?.hasUnsavedChanges : fs.dirty; + if (atRisk) { e.preventDefault(); // Modern browsers don't show this text to users due to abuse. e.returnValue = @@ -31,7 +34,7 @@ const BeforeUnloadDirtyCheck = () => { return () => { window.removeEventListener("beforeunload", listener); }; - }, [fs, persisted]); + }, [fs, persisted, projects]); return null; }; From ad79590b6e925ffac31f6367c2173a680c9dce7c Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 17:40:23 +0000 Subject: [PATCH 2/6] Move the close warning tests to the write errors block --- src/e2e/storage-errors.test.ts | 48 +++++++++++++++++----------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/e2e/storage-errors.test.ts b/src/e2e/storage-errors.test.ts index d1fd32b2e..13f575c1c 100644 --- a/src/e2e/storage-errors.test.ts +++ b/src/e2e/storage-errors.test.ts @@ -138,6 +138,30 @@ test.describe("storage write errors", () => { app.page.getByText("An unexpected error occurred") ).toBeHidden(); }); + + test("warns before closing while an edit has not saved", async ({ app }) => { + await injectWriteError(app, "UnknownError", { every: true }); + await app.typeInEditor("A change!"); + await expect( + app.page.getByText("Failed to save your project to browser storage") + ).toBeVisible(); + + await app.closeAndExpectBeforeUnloadPrompt(); + }); + + test("does not warn once a later save has succeeded", async ({ app }) => { + // Only the first save fails. typeInEditor's last keys are typed slowly + // enough to be saved again, successfully, after it. + await injectWriteError(app, "UnknownError"); + await app.typeInEditor("A change!"); + await expect( + app.page.getByText("Failed to save your project to browser storage") + ).toBeVisible(); + // Past the storage's flush delay, so the last save has finished. + await app.page.waitForTimeout(600); + + await app.closeWithoutBeforeUnloadPrompt(); + }); }); /** @@ -176,28 +200,4 @@ test.describe("storage read errors", () => { await homePage.cards.open("Untitled project"); await app.expectEditorContainText("from microbit import"); }); - - test("warns before closing while an edit has not saved", async ({ app }) => { - await injectWriteError(app, "UnknownError", { every: true }); - await app.typeInEditor("A change!"); - await expect( - app.page.getByText("Failed to save your project to browser storage") - ).toBeVisible(); - - await app.closeAndExpectBeforeUnloadPrompt(); - }); - - test("does not warn once a later save has succeeded", async ({ app }) => { - // Only the first save fails. typeInEditor's last keys are typed slowly - // enough to be saved again, successfully, after it. - await injectWriteError(app, "UnknownError"); - await app.typeInEditor("A change!"); - await expect( - app.page.getByText("Failed to save your project to browser storage") - ).toBeVisible(); - // Past the storage's flush delay, so the last save has finished. - await app.page.waitForTimeout(600); - - await app.closeWithoutBeforeUnloadPrompt(); - }); }); From ccb65ffe064e10c412680c3baf9d6e7546a1cbb1 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 17:45:44 +0000 Subject: [PATCH 3/6] Keep a failed save's changes for the next flush A failed flush dropped its changes, keeping only the names of the files it had failed to save. So switching or creating a project lost them: the flush on switching sent only what was newly pending, and the new project's storage started with nothing unsaved, which ended the close warning too. With storage full, going to the projects page to delete something is the natural remedy, and one click on another project there lost the edit. A change now stays queued until a flush is known to have saved it. A failed flush still sets no timer of its own, so nothing retries for ever against a full quota, but the next flush, whatever causes it, carries everything still unsaved. That includes the one made when the project is switched. Unsaved changes are now simply that the last flush failed, in place of tracking files by name, and the warning clears on the next successful save of anything. --- docs/tech-overview.md | 2 +- src/fs/indexeddb-storage.test.ts | 41 +++++++++++++++++---- src/fs/indexeddb-storage.ts | 62 ++++++++++++++++++-------------- src/project/projects.test.ts | 18 ++++++++++ 4 files changed, 89 insertions(+), 34 deletions(-) diff --git a/docs/tech-overview.md b/docs/tech-overview.md index 12a3e1e99..a5079024c 100644 --- a/docs/tech-overview.md +++ b/docs/tech-overview.md @@ -57,7 +57,7 @@ Edits are written through as they happen. There is no separate "save" action for - Writes are coalesced per file for 300ms, then flushed as one transaction. The project's timestamp is the time of the edit, so "last modified" does not depend on when the flush ran. - Pending writes are also flushed when the page is hidden or unloading. A transaction that waits on a read, or on its requests finishing, does not survive the page unloading. So a flush never reads before it writes, and one from a hidden or unloading page commits explicitly. This is why the storage holds the project's name and writes the whole project record each time, rather than reading the record and updating it. - A flush at any other time checks, in the same transaction, that the project still exists. -- A failed flush is not retried. The in-memory copy still has the edit, the user sees a toast that stays until dismissed ("Browser storage full" when the quota is exceeded), and the error is logged once per session. Until the same files are next saved successfully, the user is warned before closing the tab. +- A failed flush is not retried on a timer, which against a full quota would never end. Its edits stay queued and go with the next flush, whatever causes it: a later edit, the page being hidden, or the project being switched. Meanwhile the user sees a toast that stays until dismissed ("Browser storage full" when the quota is exceeded), is warned before closing the tab, and the error is logged once per session. MicroPython itself is only needed to build a hex file or flash, not to edit. If it fails to load, projects still open and save, and loading is tried again the next time something needs it. diff --git a/src/fs/indexeddb-storage.test.ts b/src/fs/indexeddb-storage.test.ts index ad520dc07..7f3a429b4 100644 --- a/src/fs/indexeddb-storage.test.ts +++ b/src/fs/indexeddb-storage.test.ts @@ -172,20 +172,46 @@ describe("IndexedDBFSStorage", () => { expect((await db.get(projectId))?.name).toEqual("Renamed here"); }); - it("knows what a failed flush left unsaved, until it is next saved", async () => { + it("has unsaved changes after a failed flush, until the next succeeds", async () => { expect(storage.hasUnsavedChanges).toEqual(false); vi.spyOn(db, "save").mockRejectedValueOnce(new Error("Simulated")); await storage.write("main.py", new Uint8Array([1])); await storage.flush(); expect(storage.hasUnsavedChanges).toEqual(true); - // Another file saving does not save the one that failed. + // Saving anything saves what failed too. await storage.write("other.py", new Uint8Array([2])); await storage.flush(); - expect(storage.hasUnsavedChanges).toEqual(true); + expect(storage.hasUnsavedChanges).toEqual(false); + expect(contents(await db.files(projectId))).toEqual({ + "main.py": [1], + "other.py": [2], + }); + }); - await storage.write("main.py", new Uint8Array([3])); + it("does not retry a failed flush until something else flushes", async () => { + const save = vi + .spyOn(db, "save") + .mockRejectedValueOnce(new Error("Simulated")); + await storage.write("main.py", new Uint8Array([1])); await storage.flush(); + await new Promise((resolve) => setTimeout(resolve, 60)); + expect(save).toHaveBeenCalledTimes(1); + + // As when the project is switched. + await storage.dispose(); + expect(save).toHaveBeenCalledTimes(2); + expect(contents(await db.files(projectId))).toEqual({ "main.py": [1] }); + }); + + it("sends the newer content if a file changes again after a failed flush", async () => { + vi.spyOn(db, "save").mockRejectedValueOnce(new Error("Simulated")); + await storage.write("main.py", new Uint8Array([1])); + await storage.flush(); + await storage.write("main.py", new Uint8Array([2])); + await storage.remove("old.py"); + await storage.flush(); + expect(contents(await db.files(projectId))).toEqual({ "main.py": [2] }); expect(storage.hasUnsavedChanges).toEqual(false); }); @@ -209,7 +235,7 @@ describe("IndexedDBFSStorage", () => { expect(errors).toEqual([]); }); - it("reports a failed flush and drops those changes rather than throwing", async () => { + it("reports a failed flush rather than throwing, and keeps its changes for the next", async () => { vi.spyOn(db, "save").mockRejectedValueOnce( new DOMException("full", "QuotaExceededError") ); @@ -220,6 +246,9 @@ describe("IndexedDBFSStorage", () => { await storage.write("other.py", new Uint8Array([2])); await storage.flush(); - expect(contents(await db.files(projectId))).toEqual({ "other.py": [2] }); + expect(contents(await db.files(projectId))).toEqual({ + "main.py": [1], + "other.py": [2], + }); }); }); diff --git a/src/fs/indexeddb-storage.ts b/src/fs/indexeddb-storage.ts index 9e9c845a2..df52e8800 100644 --- a/src/fs/indexeddb-storage.ts +++ b/src/fs/indexeddb-storage.ts @@ -21,9 +21,10 @@ const defaultFlushDelayMs = 300; * decide, from when the project is opened. Another tab changing it is told * to this one by Projects, which opens the project afresh. * - * Failures never propagate: the in-memory primary still holds the content, - * so a failed flush is reported through onError and the changes are dropped - * rather than retried forever against, say, a full quota. + * Failures never propagate: a failed flush is reported through onError. Its + * changes stay queued and go with the next flush, whatever causes that: a + * later edit, the page being hidden, or the project being switched. There + * is no retry of its own, which against a full quota would never end. * * The dirty flag is not stored: it exists to warn before work is lost, and * a project in the database outlives the tab. @@ -33,18 +34,17 @@ export class IndexedDBFSStorage implements FSStorage { private name: string | undefined; // The name was set here and has not been flushed yet. private nameUnsaved = false; + // Changes not yet known to be in the database. An entry stays here while + // a flush carrying it is in flight, and after one that fails. private pendingWrites = new Map(); - // When the oldest pending change was made, or undefined with nothing to - // flush: the project's "last modified" is the edit, not the flush, which - // may come later than another project's. + // When the oldest change not yet sent in a flush was made, or undefined + // with nothing new to flush: the project's "last modified" is the edit, + // not the flush, which may come later than another project's. private pendingSince: number | undefined; private timer: ReturnType | undefined; private flushing: Promise = Promise.resolve(); private abandoned = false; - // What a failed flush left out of the database and no flush has written - // since: files by name, and the project record. - private unsavedFiles = new Set(); - private unsavedRecord = false; + private lastFlushFailed = false; private readonly handleVisibilityChange = () => { if (document.visibilityState === "hidden") { void this.flush({ urgent: true }); @@ -97,17 +97,16 @@ export class IndexedDBFSStorage implements FSStorage { this.timer = undefined; this.pendingWrites = new Map(); this.pendingSince = undefined; - this.unsavedFiles = new Set(); - this.unsavedRecord = false; + this.lastFlushFailed = false; } /** - * True while a failed flush has left something out of the database, so - * that closing the tab now would lose it. A failed flush is not retried, - * so this lasts until the same files are next written successfully. + * True while the last flush failed, so that closing the tab now would + * lose what it carried. Each flush sends everything still unsaved, so the + * next one to succeed clears this. */ get hasUnsavedChanges(): boolean { - return this.unsavedFiles.size > 0 || this.unsavedRecord; + return this.lastFlushFailed; } /** @@ -217,37 +216,46 @@ export class IndexedDBFSStorage implements FSStorage { flush({ urgent = false }: { urgent?: boolean } = {}): Promise { clearTimeout(this.timer); this.timer = undefined; - if (this.pendingSince === undefined) { + if (this.abandoned || this.pendingSince === undefined) { return this.flushing; } + const since = this.pendingSince; const project: ProjectMeta = { id: this.projectId, name: this.name, - timestamp: this.pendingSince, + timestamp: since, }; + const sent = new Map(this.pendingWrites); const files: FileChanges = { writes: {}, deletes: [] }; - for (const [name, data] of this.pendingWrites) { + for (const [name, data] of sent) { if (data === null) { files.deletes.push(name); } else { files.writes[name] = data; } } - this.pendingWrites = new Map(); this.pendingSince = undefined; - this.nameUnsaved = false; - const names = [...Object.keys(files.writes), ...files.deletes]; const saved = this.db .save(project, files, { urgent }) .then( () => { - names.forEach((name) => this.unsavedFiles.delete(name)); - // Every flush writes the whole record. - this.unsavedRecord = false; + for (const [name, data] of sent) { + // Unless it has been changed again since. + if (this.pendingWrites.get(name) === data) { + this.pendingWrites.delete(name); + } + } + if (this.name === project.name) { + this.nameUnsaved = false; + } + this.lastFlushFailed = false; }, (e) => { - names.forEach((name) => this.unsavedFiles.add(name)); - this.unsavedRecord = true; + if (!this.abandoned) { + // So that the next flush, whatever causes it, sends this again. + this.pendingSince = Math.min(since, this.pendingSince ?? since); + this.lastFlushFailed = true; + } throw e; } ) diff --git a/src/project/projects.test.ts b/src/project/projects.test.ts index edd8bcfb2..5865e856f 100644 --- a/src/project/projects.test.ts +++ b/src/project/projects.test.ts @@ -220,6 +220,24 @@ describe("Projects", () => { await vi.waitFor(() => expect(projects.hasUnsavedChanges).toEqual(false)); }); + it("saves an edit that failed to save when the project is switched", async () => { + const { db, fs, projects } = await setup(); + await projects.openCurrent(); + await fs.initialize(); + const first = projects.currentId!; + vi.spyOn(logging, "error").mockImplementation(() => undefined); + vi.spyOn(db!, "save").mockRejectedValueOnce(new Error("Simulated")); + await fs.write(MAIN_FILE, "# not saved yet", VersionAction.MAINTAIN); + await vi.waitFor(() => expect(projects.hasUnsavedChanges).toEqual(true)); + + await projects.create("Second"); + + expect(decode((await db!.file(first, MAIN_FILE))!)).toEqual( + "# not saved yet" + ); + expect(projects.hasUnsavedChanges).toEqual(false); + }); + it("a failed save raises saveerror each time but is logged once", async () => { const { db, fs, projects } = await setup(); await projects.openCurrent(); From 7570371c944617d34aee7117cecb9faf121fe38a Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 17:57:47 +0000 Subject: [PATCH 4/6] Close three gaps the retry queue opened Keeping a failed save's changes for the next flush had side effects: - With the project open in two tabs, a failed save in one followed by any save in the other left the first out of step. It reloaded from the database, losing its edit from the screen, and then the outgoing storage wrote that edit to the database. The change handler now saves what this tab has outstanding before it reloads, taking the name from the database first as the name is the other tab's to change. If that still cannot be saved, the tab keeps its copy and does not reload. - A flush that failed because the project had been deleted was queued like any other, and the next urgent flush, which does not check the project exists, wrote part of it back. save() now rejects with NoSuchProjectError and the storage drops everything on that. - After a spell of failures the project was stamped with the first failed edit's time. It is now stamped with the latest edit's. --- src/fs/indexeddb-storage.test.ts | 23 ++++++++++++++ src/fs/indexeddb-storage.ts | 37 ++++++++++++++-------- src/fs/projects-db.test.ts | 9 ++++-- src/fs/projects-db.ts | 16 +++++++--- src/project/projects.test.ts | 53 ++++++++++++++++++++++++++++++++ src/project/projects.ts | 21 +++++++++++-- 6 files changed, 138 insertions(+), 21 deletions(-) diff --git a/src/fs/indexeddb-storage.test.ts b/src/fs/indexeddb-storage.test.ts index 7f3a429b4..e808a6553 100644 --- a/src/fs/indexeddb-storage.test.ts +++ b/src/fs/indexeddb-storage.test.ts @@ -222,6 +222,29 @@ describe("IndexedDBFSStorage", () => { expect(errors).toHaveLength(1); expect(await db.get(projectId)).toBeUndefined(); expect(contents(await db.files(projectId))).toEqual({}); + + // Nor later: an urgent flush does not check the project exists, so what + // that flush carried must not be kept for it. + window.dispatchEvent(new Event("pagehide")); + await storage.flush(); + expect(await db.get(projectId)).toBeUndefined(); + expect(contents(await db.files(projectId))).toEqual({}); + expect(storage.hasUnsavedChanges).toEqual(false); + }); + + it("stamps the project with its latest edit, even after failed flushes", async () => { + const now = vi.spyOn(Date, "now"); + vi.spyOn(db, "save").mockRejectedValueOnce(new Error("Simulated")); + now.mockReturnValue(100); + await storage.write("main.py", new Uint8Array([1])); + await storage.flush(); + + now.mockReturnValue(5000); + await storage.write("other.py", new Uint8Array([2])); + await storage.flush(); + now.mockRestore(); + + expect((await db.get(projectId))?.timestamp).toEqual(5000); }); it("writes nothing once abandoned", async () => { diff --git a/src/fs/indexeddb-storage.ts b/src/fs/indexeddb-storage.ts index df52e8800..26020dc53 100644 --- a/src/fs/indexeddb-storage.ts +++ b/src/fs/indexeddb-storage.ts @@ -3,7 +3,12 @@ * * SPDX-License-Identifier: MIT */ -import { FileChanges, ProjectMeta, ProjectsDatabase } from "./projects-db"; +import { + FileChanges, + NoSuchProjectError, + ProjectMeta, + ProjectsDatabase, +} from "./projects-db"; import { FSStorage } from "./storage"; const defaultFlushDelayMs = 300; @@ -37,10 +42,12 @@ export class IndexedDBFSStorage implements FSStorage { // Changes not yet known to be in the database. An entry stays here while // a flush carrying it is in flight, and after one that fails. private pendingWrites = new Map(); - // When the oldest change not yet sent in a flush was made, or undefined - // with nothing new to flush: the project's "last modified" is the edit, - // not the flush, which may come later than another project's. - private pendingSince: number | undefined; + // There is something that no flush has yet been sent for, or that one + // failed to save. + private needsFlush = false; + // When the latest change was made. The project's "last modified" is the + // edit, not the flush, which may come later than another project's. + private lastChange = 0; private timer: ReturnType | undefined; private flushing: Promise = Promise.resolve(); private abandoned = false; @@ -96,7 +103,7 @@ export class IndexedDBFSStorage implements FSStorage { clearTimeout(this.timer); this.timer = undefined; this.pendingWrites = new Map(); - this.pendingSince = undefined; + this.needsFlush = false; this.lastFlushFailed = false; } @@ -193,7 +200,8 @@ export class IndexedDBFSStorage implements FSStorage { this.pendingWrites = new Map(); return; } - this.pendingSince ??= Date.now(); + this.needsFlush = true; + this.lastChange = Date.now(); if (this.timer === undefined) { this.timer = setTimeout(() => void this.flush(), this.flushDelayMs); } @@ -216,14 +224,13 @@ export class IndexedDBFSStorage implements FSStorage { flush({ urgent = false }: { urgent?: boolean } = {}): Promise { clearTimeout(this.timer); this.timer = undefined; - if (this.abandoned || this.pendingSince === undefined) { + if (this.abandoned || !this.needsFlush) { return this.flushing; } - const since = this.pendingSince; const project: ProjectMeta = { id: this.projectId, name: this.name, - timestamp: since, + timestamp: this.lastChange, }; const sent = new Map(this.pendingWrites); const files: FileChanges = { writes: {}, deletes: [] }; @@ -234,7 +241,7 @@ export class IndexedDBFSStorage implements FSStorage { files.writes[name] = data; } } - this.pendingSince = undefined; + this.needsFlush = false; const saved = this.db .save(project, files, { urgent }) .then( @@ -251,9 +258,13 @@ export class IndexedDBFSStorage implements FSStorage { this.lastFlushFailed = false; }, (e) => { - if (!this.abandoned) { + if (e instanceof NoSuchProjectError) { + // Nothing queued must be sent again: an urgent flush does not + // check the project exists and would write part of it back. + this.abandon(); + } else if (!this.abandoned) { // So that the next flush, whatever causes it, sends this again. - this.pendingSince = Math.min(since, this.pendingSince ?? since); + this.needsFlush = true; this.lastFlushFailed = true; } throw e; diff --git a/src/fs/projects-db.test.ts b/src/fs/projects-db.test.ts index 5b42fbf8d..e8ba8c6d8 100644 --- a/src/fs/projects-db.test.ts +++ b/src/fs/projects-db.test.ts @@ -5,7 +5,12 @@ */ import "fake-indexeddb/auto"; import { openDB } from "idb"; -import { databaseName, ProjectMeta, ProjectsDatabase } from "./projects-db"; +import { + databaseName, + NoSuchProjectError, + ProjectMeta, + ProjectsDatabase, +} from "./projects-db"; let counter = 0; const uniqueName = () => `test-${Date.now()}-${counter++}`; @@ -94,7 +99,7 @@ describe("ProjectsDatabase", () => { it("save refuses a project that no longer exists and writes nothing", async () => { await expect( db.save(meta("gone", 1), { writes: { "main.py": bytes(1) }, deletes: [] }) - ).rejects.toThrow(/No such project gone/); + ).rejects.toBeInstanceOf(NoSuchProjectError); expect(await db.get("gone")).toBeUndefined(); expect(contents(await db.files("gone"))).toEqual({}); }); diff --git a/src/fs/projects-db.ts b/src/fs/projects-db.ts index bfb968cea..c81eff3c7 100644 --- a/src/fs/projects-db.ts +++ b/src/fs/projects-db.ts @@ -48,6 +48,14 @@ export interface FileChanges { deletes: string[]; } +/** The project has been deleted, most likely in another tab. */ +export class NoSuchProjectError extends Error { + constructor(id: string) { + super(`No such project ${id}`); + this.name = "NoSuchProjectError"; + } +} + const PROJECTS = "projects"; const FILES = "files"; const stores = [PROJECTS, FILES] as const; @@ -224,7 +232,7 @@ export class ProjectsDatabase { const tx = handled(this.db.transaction(PROJECTS, "readwrite")); const existing = await tx.store.get(id); if (!existing) { - throw new Error(`No such project ${id}`); + throw new NoSuchProjectError(id); } await tx.store.put({ ...existing, ...changes, id }); await tx.done; @@ -242,8 +250,8 @@ export class ProjectsDatabase { * rather than when the requests have finished. This gives up the check * that the project still exists, which needs the result of a read, so a * project deleted in the meantime is written back. - * @throws if the project no longer exists (unless urgent), leaving - * nothing written. + * @throws NoSuchProjectError if the project no longer exists (unless + * urgent), leaving nothing written. */ save( project: ProjectMeta, @@ -261,7 +269,7 @@ export class ProjectsDatabase { tx.onabort = () => reject( missing - ? new Error(`No such project ${project.id}`) + ? new NoSuchProjectError(project.id) : tx.error ?? new DOMException("Transaction aborted", "AbortError") ); try { diff --git a/src/project/projects.test.ts b/src/project/projects.test.ts index 5865e856f..56383bed2 100644 --- a/src/project/projects.test.ts +++ b/src/project/projects.test.ts @@ -371,6 +371,59 @@ describe("Projects", () => { return tabs; }; + it("saves this tab's failed edit before reloading for the other's change", async () => { + const { a, b, db, id } = await twoTabs(); + vi.spyOn(logging, "error").mockImplementation(() => undefined); + vi.spyOn(db, "save").mockRejectedValueOnce(new Error("Simulated")); + await b.fs.write(MAIN_FILE, "# b, not saved yet", VersionAction.MAINTAIN); + await vi.waitFor(() => + expect(b.projects.hasUnsavedChanges).toEqual(true) + ); + + await a.fs.write("helper.py", "# from a", VersionAction.INCREMENT); + await vi.waitFor(() => + expect(b.fs.project.files.map((f) => f.name)).toContain("helper.py") + ); + + // b still shows its edit, and everything agrees with the database. + expect(decode((await b.fs.read(MAIN_FILE)).data)).toEqual( + "# b, not saved yet" + ); + expect(decode((await db.file(id, MAIN_FILE))!)).toEqual( + "# b, not saved yet" + ); + expect(b.projects.hasUnsavedChanges).toEqual(false); + await vi.waitFor(async () => + expect(decode((await a.fs.read(MAIN_FILE)).data)).toEqual( + "# b, not saved yet" + ) + ); + }); + + it("keeps this tab's copy if its edit still cannot be saved", async () => { + const { a, b, db } = await twoTabs(); + vi.spyOn(logging, "error").mockImplementation(() => undefined); + const save = db.save.bind(db); + // Saves of b's edit fail; a's go through. + vi.spyOn(db, "save").mockImplementation((project, files, options) => + MAIN_FILE in files.writes + ? Promise.reject(new Error("Simulated")) + : save(project, files, options) + ); + await b.fs.write(MAIN_FILE, "# b, cannot save", VersionAction.MAINTAIN); + await vi.waitFor(() => + expect(b.projects.hasUnsavedChanges).toEqual(true) + ); + + await a.fs.write("helper.py", "# from a", VersionAction.INCREMENT); + await new Promise((resolve) => setTimeout(resolve, 800)); + + expect(decode((await b.fs.read(MAIN_FILE)).data)).toEqual( + "# b, cannot save" + ); + expect(b.projects.hasUnsavedChanges).toEqual(true); + }); + it("still lets go of a deleted project if refreshing the list fails", async () => { const { a, b, id } = await twoTabs(); const logged = vi diff --git a/src/project/projects.ts b/src/project/projects.ts index 6eb2a4f5a..664f71583 100644 --- a/src/project/projects.ts +++ b/src/project/projects.ts @@ -20,6 +20,7 @@ import { generateId } from "../fs/fs-util"; import { IndexedDBFSStorage } from "../fs/indexeddb-storage"; import { PendingProjectLink } from "../fs/project-link"; import { + NoSuchProjectError, ProjectContent, ProjectListEntry, ProjectsDatabase, @@ -348,7 +349,7 @@ export class Projects extends TypedEventTarget { const db = await this.requireDb(); const content = loaded ?? (await db.load(id)); if (!content) { - throw new Error(`No such project ${id}`); + throw new NoSuchProjectError(id); } const primary = new InMemoryFSStorage(content.meta.name); for (const [name, data] of Object.entries(content.files)) { @@ -434,7 +435,23 @@ export class Projects extends TypedEventTarget { let content: ProjectContent | undefined; if (type === "changed") { try { - content = await this.db?.load(id); + const storage = this.openStorage; + const record = await this.db?.get(id); + if (record && storage && this.openId === id) { + // What this tab has still to save goes first, so that the reload + // has it too. Otherwise the reload would put the database's copy + // on screen in its place, and the save that follows would leave + // the two out of step. The name is the other tab's to change, so + // take it before saving. + storage.adoptName(record.name); + await storage.flush(); + if (storage.hasUnsavedChanges) { + // It cannot be saved, and a reload would lose it from here + // too. Carry on with this tab's copy. + return; + } + } + content = record && (await this.db?.load(id)); } catch (e) { // Carry on with this tab's copy. this.logging.error("Failed to reload project", e); From 58688d76b0d6db22994de7d2a5e89c3612858e82 Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 18:17:55 +0000 Subject: [PATCH 5/6] Save before reloading the open project, however it is asked for Opening the project that is already open took the reload path, which put the database's copy on screen before the outgoing storage flushed. After a failed save that lost the edit from the editor while the database gained it, and the next keystroke wrote the old content back. That is the path the queue was meant for: storage full, go to the projects page, free some space, click back into the project. The cross-tab handler already saved first and skipped the reload if that failed. That now lives in switchTo's reload branch, so it covers both. The handler asks for a reload and treats a project that is not there as deleted. Also corrects two comments that still said a failed flush's changes are dropped, and the overview on a tab that cannot save: it stays out of step until the project is opened in it again or the tab is reloaded. --- docs/tech-overview.md | 2 +- src/fs/indexeddb-storage.ts | 2 +- src/project/projects.test.ts | 45 +++++++++++++++ src/project/projects.ts | 104 +++++++++++++++++------------------ 4 files changed, 98 insertions(+), 55 deletions(-) diff --git a/docs/tech-overview.md b/docs/tech-overview.md index a5079024c..b5de4626b 100644 --- a/docs/tech-overview.md +++ b/docs/tech-overview.md @@ -66,7 +66,7 @@ MicroPython itself is only needed to build a hex file or flash, not to edit. If Tabs tell each other about changes over a `BroadcastChannel`, naming the projects affected: - Every tab refreshes its list of projects. -- A tab with a changed project open reloads it. +- A tab with a changed project open reloads it, after first saving anything it has outstanding. If that cannot be saved, the tab keeps its own copy rather than lose the edit, and stays out of step until the project is next opened in it or the tab is reloaded. - A tab with a deleted project open stops writing it, and if the editor is showing goes to the home page with a message. ### Without IndexedDB, and embedded diff --git a/src/fs/indexeddb-storage.ts b/src/fs/indexeddb-storage.ts index 26020dc53..b1443e823 100644 --- a/src/fs/indexeddb-storage.ts +++ b/src/fs/indexeddb-storage.ts @@ -62,7 +62,7 @@ export class IndexedDBFSStorage implements FSStorage { /** * @param project The project's record as it is in the database now. - * @param onError Reports a failed flush; the changes in it are dropped. + * @param onError Reports a failed flush; its changes go with the next. * @param onChange Called after a successful flush, for cross-tab sync. */ constructor( diff --git a/src/project/projects.test.ts b/src/project/projects.test.ts index 56383bed2..f0d0a6487 100644 --- a/src/project/projects.test.ts +++ b/src/project/projects.test.ts @@ -238,6 +238,51 @@ describe("Projects", () => { expect(projects.hasUnsavedChanges).toEqual(false); }); + describe("opening the open project again after a failed save", () => { + // As after going to the projects page to free some space. + const withFailedEdit = async (failures: "once" | "always") => { + const { db, fs, projects } = await setup(); + await projects.openCurrent(); + await fs.initialize(); + vi.spyOn(logging, "error").mockImplementation(() => undefined); + const save = vi.spyOn(db!, "save"); + if (failures === "once") { + save.mockRejectedValueOnce(new Error("Simulated")); + } else { + save.mockRejectedValue(new Error("Simulated")); + } + await fs.write(MAIN_FILE, "# not saved yet", VersionAction.MAINTAIN); + await vi.waitFor(() => expect(projects.hasUnsavedChanges).toEqual(true)); + return { db: db!, fs, projects, id: projects.currentId! }; + }; + + it("saves the edit and keeps it on screen", async () => { + const { db, fs, projects, id } = await withFailedEdit("once"); + + await projects.open(id); + + expect(decode((await fs.read(MAIN_FILE)).data)).toEqual( + "# not saved yet" + ); + expect(decode((await db.file(id, MAIN_FILE))!)).toEqual( + "# not saved yet" + ); + expect(projects.hasUnsavedChanges).toEqual(false); + }); + + it("keeps the edit on screen if it still cannot be saved", async () => { + const { fs, projects, id } = await withFailedEdit("always"); + + await projects.open(id); + + expect(decode((await fs.read(MAIN_FILE)).data)).toEqual( + "# not saved yet" + ); + expect(projects.currentId).toEqual(id); + expect(projects.hasUnsavedChanges).toEqual(true); + }); + }); + it("a failed save raises saveerror each time but is logged once", async () => { const { db, fs, projects } = await setup(); await projects.openCurrent(); diff --git a/src/project/projects.ts b/src/project/projects.ts index 664f71583..e66f8edb2 100644 --- a/src/project/projects.ts +++ b/src/project/projects.ts @@ -40,7 +40,10 @@ export class ProjectsChangedEvent extends Event { } } -/** A write to the open project's storage failed; its changes were dropped. */ +/** + * A write to the open project's storage failed. Its changes go with the next + * write that is made. + */ export class ProjectSaveErrorEvent extends Event { constructor(public readonly error: unknown) { super("saveerror"); @@ -340,28 +343,49 @@ export class Projects extends TypedEventTarget { * storage the host is waiting for; afterwards it switches. Loads the * project before changing anything, so a failed read leaves all as it was. * - * @param loaded The project's content when the caller has just written - * or read it, to skip reading it again. A caller that has checked the - * project exists passes what it read, so that a delete cannot land - * between its read and one here. + * Asked for the project that is already open, it reloads it: another tab + * has changed it, or the user has opened it again. What this tab has still + * to save is saved first, and if that fails the tab keeps its copy. + * + * @param created The project's content when the caller has just created + * it, to skip reading it back. + * @throws NoSuchProjectError if the project has been deleted. */ - private async switchTo(id: string, loaded?: ProjectContent): Promise { + private async switchTo(id: string, created?: ProjectContent): Promise { const db = await this.requireDb(); - const content = loaded ?? (await db.load(id)); + const previous = this.openStorage; + const reloading = this.openId === id && previous !== undefined; + if (reloading) { + const record = await db.get(id); + if (!record) { + throw new NoSuchProjectError(id); + } + // Saved before reloading so that the reload has it too. Otherwise the + // database's copy would go on screen in its place, and the save that + // followed would leave the two out of step. The name is the other + // tab's to change, so take it before saving. + previous.adoptName(record.name); + await previous.flush(); + if (previous.hasUnsavedChanges) { + // It cannot be saved, and a reload would lose it from here too. + return; + } + } + const content = created ?? (await db.load(id)); if (!content) { throw new NoSuchProjectError(id); } + if (reloading && this.openStorage !== previous) { + // The user opened another project while this waited. Don't go back. + return; + } const primary = new InMemoryFSStorage(content.meta.name); for (const [name, data] of Object.entries(content.files)) { await primary.write(name, data); } - const previous = this.openStorage; - const reloading = this.openId === id; if (reloading) { - // Reloading the open project, for another tab's change. Edits here - // that the outgoing storage has still to flush must not take the - // project's name back to what it was before that change. - previous?.adoptName(content.meta.name); + // For anything edited here since the save above. + previous.adoptName(content.meta.name); } const indexed = new IndexedDBFSStorage( db, @@ -429,51 +453,25 @@ export class Projects extends TypedEventTarget { if (!id || !projectIds.includes(id)) { return; } - // A "changed" message says nothing about now: the project may have - // been deleted since it was sent, with the "deleted" message still to - // come. So look, and treat a missing project as deleted. - let content: ProjectContent | undefined; if (type === "changed") { + // Reload the open project so this tab shows the other's edits. A + // "changed" message says nothing about now: the project may have + // been deleted since it was sent, with the "deleted" message still + // to come, in which case it is not there to reload. try { - const storage = this.openStorage; - const record = await this.db?.get(id); - if (record && storage && this.openId === id) { - // What this tab has still to save goes first, so that the reload - // has it too. Otherwise the reload would put the database's copy - // on screen in its place, and the save that follows would leave - // the two out of step. The name is the other tab's to change, so - // take it before saving. - storage.adoptName(record.name); - await storage.flush(); - if (storage.hasUnsavedChanges) { - // It cannot be saved, and a reload would lose it from here - // too. Carry on with this tab's copy. - return; - } - } - content = record && (await this.db?.load(id)); - } catch (e) { - // Carry on with this tab's copy. - this.logging.error("Failed to reload project", e); + await this.switchTo(id); return; - } - } - if (this.openId !== id) { - // While this waited the user opened another project, or the - // "deleted" message was handled. - return; - } - if (content) { - // Reload the open project so this tab shows the other's edits. If - // it is deleted from here on, the "deleted" message finds it open - // and lets go of it. If the reload fails, carry on with this tab's - // copy. - try { - await this.switchTo(id, content); } catch (e) { - this.logging.error("Failed to reload project", e); + if (!(e instanceof NoSuchProjectError)) { + // Carry on with this tab's copy. + this.logging.error("Failed to reload project", e); + return; + } } - } else { + } + if (this.openId === id) { + // Unless the user opened another project, or a "deleted" message + // was handled, while this waited. this.letGoOfOpenProject(); this.dispatchTypedEvent( "openprojectdeleted", From b09462e988c0de45f9e718795b995f355b59eaeb Mon Sep 17 00:00:00 2001 From: Matt Hillsdon Date: Fri, 20 Nov 2026 18:21:47 +0000 Subject: [PATCH 6/6] Say all the ways a reload can be overtaken --- src/project/projects.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/project/projects.ts b/src/project/projects.ts index e66f8edb2..c49ab481d 100644 --- a/src/project/projects.ts +++ b/src/project/projects.ts @@ -376,7 +376,9 @@ export class Projects extends TypedEventTarget { throw new NoSuchProjectError(id); } if (reloading && this.openStorage !== previous) { - // The user opened another project while this waited. Don't go back. + // What was open changed while this waited: the user opened another + // project, a "deleted" message let go of this one, or an overlapping + // reload got there first. This reload has been overtaken. return; } const primary = new InMemoryFSStorage(content.meta.name);