diff --git a/docs/tech-overview.md b/docs/tech-overview.md index cf4df7b7f..b5de4626b 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 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. @@ -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 @@ -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..13f575c1c 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 } + ); }; /** @@ -131,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(); + }); }); /** diff --git a/src/fs/indexeddb-storage.test.ts b/src/fs/indexeddb-storage.test.ts index 8efae93bc..e808a6553 100644 --- a/src/fs/indexeddb-storage.test.ts +++ b/src/fs/indexeddb-storage.test.ts @@ -172,6 +172,49 @@ describe("IndexedDBFSStorage", () => { expect((await db.get(projectId))?.name).toEqual("Renamed here"); }); + 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); + + // Saving anything saves what failed too. + await storage.write("other.py", new Uint8Array([2])); + await storage.flush(); + expect(storage.hasUnsavedChanges).toEqual(false); + expect(contents(await db.files(projectId))).toEqual({ + "main.py": [1], + "other.py": [2], + }); + }); + + 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); + }); + it("does not write back a project deleted elsewhere", async () => { await db.delete(projectId); await storage.write("main.py", new Uint8Array([1])); @@ -179,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 () => { @@ -192,7 +258,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") ); @@ -203,6 +269,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 2c3c2bec2..b1443e823 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; @@ -21,9 +26,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,14 +39,19 @@ 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. - 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; + private lastFlushFailed = false; private readonly handleVisibilityChange = () => { if (document.visibilityState === "hidden") { void this.flush({ urgent: true }); @@ -51,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( @@ -92,7 +103,17 @@ export class IndexedDBFSStorage implements FSStorage { clearTimeout(this.timer); this.timer = undefined; this.pendingWrites = new Map(); - this.pendingSince = undefined; + this.needsFlush = false; + this.lastFlushFailed = false; + } + + /** + * 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.lastFlushFailed; } /** @@ -179,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); } @@ -202,27 +224,52 @@ 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.needsFlush) { return this.flushing; } const project: ProjectMeta = { id: this.projectId, name: this.name, - timestamp: this.pendingSince, + timestamp: this.lastChange, }; + 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; + this.needsFlush = false; const saved = this.db .save(project, files, { urgent }) + .then( + () => { + 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) => { + 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.needsFlush = true; + this.lastFlushFailed = 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..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({}); }); @@ -120,6 +125,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..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; @@ -85,6 +93,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 +212,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,10 +229,10 @@ 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}`); + throw new NoSuchProjectError(id); } await tx.store.put({ ...existing, ...changes, id }); await tx.done; @@ -230,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, @@ -249,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 { @@ -295,7 +315,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..f0d0a6487 100644 --- a/src/project/projects.test.ts +++ b/src/project/projects.test.ts @@ -205,6 +205,84 @@ 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("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); + }); + + 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(); @@ -338,6 +416,81 @@ 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 + .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..c49ab481d 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, @@ -39,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"); @@ -167,6 +171,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; @@ -331,28 +343,51 @@ 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 Error(`No such project ${id}`); + throw new NoSuchProjectError(id); + } + if (reloading && this.openStorage !== previous) { + // 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); 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, @@ -409,40 +444,36 @@ 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; } - // 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 { - content = 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", 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; };