Skip to content

Commit ccc14f3

Browse files
Report failures to start a save or announce one
save() opened its transaction outside its promise, so a connection the browser had closed made flush() throw after it had taken the pending changes: an uncaught error and no save-error toast. A throw from onChange rejected the flush chain for good, and every later read with it. Both now reach onError like any other failed save. The comment on flush also claimed more than it does for a flush already in flight when the page unloads.
1 parent 7288d00 commit ccc14f3

4 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎src/fs/indexeddb-storage.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ describe("IndexedDBFSStorage", () => {
141141
expect(errors).toEqual([]);
142142
});
143143

144+
it("reports a failure to announce a change and carries on", async () => {
145+
const failing = new IndexedDBFSStorage(
146+
db,
147+
{ id: projectId, name: undefined, timestamp: 1 },
148+
(e) => errors.push(e),
149+
() => {
150+
throw new Error("channel closed");
151+
},
152+
20
153+
);
154+
await failing.write("main.py", new Uint8Array([1]));
155+
await failing.flush();
156+
expect(errors).toHaveLength(1);
157+
158+
await failing.write("main.py", new Uint8Array([2]));
159+
expect(Array.from(await failing.read("main.py"))).toEqual([2]);
160+
await failing.dispose();
161+
});
162+
144163
it("does not write back a project deleted elsewhere", async () => {
145164
await db.delete(projectId);
146165
await storage.write("main.py", new Uint8Array([1]));

‎src/fs/indexeddb-storage.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,11 @@ export class IndexedDBFSStorage implements FSStorage {
172172
/**
173173
* Writes everything pending in one transaction. Safe to call at any time.
174174
*
175-
* The transaction's requests are issued before this returns, which is
176-
* what lets a flush from a page that is unloading succeed, and it runs
175+
* The transaction's requests are issued before this returns, which an
176+
* urgent flush needs to stand a chance as the page unloads, and it runs
177177
* after those of earlier flushes, so the latest content is what is kept.
178+
* An earlier flush that is still in flight when the page unloads is not
179+
* helped by a later urgent one: only the urgent one commits explicitly.
178180
*
179181
* @param urgent The page may be about to go away; see
180182
* ProjectsDatabase.save.
@@ -202,10 +204,10 @@ export class IndexedDBFSStorage implements FSStorage {
202204
}
203205
this.pendingWrites = new Map();
204206
this.pendingSince = undefined;
205-
const saved = this.db.save(project, files, { urgent }).then(
206-
() => this.onChange(),
207-
(e) => this.onError(e)
208-
);
207+
const saved = this.db
208+
.save(project, files, { urgent })
209+
.then(() => this.onChange())
210+
.catch((e) => this.onError(e));
209211
const earlier = this.flushing;
210212
this.flushing = earlier.then(() => saved);
211213
return this.flushing;

‎src/fs/projects-db.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,16 @@ describe("ProjectsDatabase", () => {
9999
expect(contents(await db.files("gone"))).toEqual({});
100100
});
101101

102+
it("save rejects rather than throws when the connection has closed", async () => {
103+
await db.create(meta("a", 1), {});
104+
db.close();
105+
let result: Promise<void> | undefined;
106+
expect(() => {
107+
result = db.save(meta("a", 2), { writes: {}, deletes: [] });
108+
}).not.toThrow();
109+
await expect(result).rejects.toThrow();
110+
});
111+
102112
it("an urgent save issues its writes without waiting to check", async () => {
103113
await db.create(meta("a", 1), { "main.py": bytes(1) });
104114
await db.save(

‎src/fs/projects-db.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,12 @@ export class ProjectsDatabase {
216216
{ writes, deletes }: FileChanges,
217217
{ urgent = false }: { urgent?: boolean } = {}
218218
): Promise<void> {
219-
// The plain transaction, not idb's wrapper: its promise of completion
220-
// would be an unhandled rejection on the abort below.
221-
const tx = unwrap(this.db).transaction(stores, "readwrite");
222219
return new Promise((resolve, reject) => {
220+
// The plain transaction, not idb's wrapper: its promise of completion
221+
// would be an unhandled rejection on the abort below. Opened in here
222+
// so that failing to, as when the browser has closed the connection,
223+
// is a rejection like any other failure.
224+
const tx = unwrap(this.db).transaction(stores, "readwrite");
223225
let missing = false;
224226
tx.oncomplete = () => resolve();
225227
tx.onabort = () =>

0 commit comments

Comments
 (0)