fix(web): avoid stale file writes on close (#8630)
This commit is contained in:
1 parent
7a8df3338f
commit
d2042d288e
2 files changed
+141
-1
No files matched your search
@@ -73,6 +73,98 @@ describe("FileSaveCoordinator", () => {
|
||||
expect(onPendingChange.mock.calls.at(-1)).toEqual([false]);
|
||||
});
|
||||
|
||||
it("saves an edit made inside the debounce window when the editor closes", async () => {
|
||||
vi.useFakeTimers();
|
||||
const persist = vi
|
||||
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange: vi.fn(),
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.change("unsaved");
|
||||
coordinator.dispose();
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).toHaveBeenCalledOnce();
|
||||
expect(persist).toHaveBeenCalledWith("unsaved");
|
||||
});
|
||||
|
||||
it("flushes an edit made while a write was in flight when the editor closes", async () => {
|
||||
vi.useFakeTimers();
|
||||
const inFlight = deferred();
|
||||
const persist = vi
|
||||
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
|
||||
.mockReturnValueOnce(inFlight.promise)
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange: vi.fn(),
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.change("first");
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
coordinator.change("latest");
|
||||
coordinator.dispose();
|
||||
inFlight.resolve(AsyncResult.success(undefined));
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).toHaveBeenCalledTimes(2);
|
||||
expect(persist).toHaveBeenLastCalledWith("latest");
|
||||
});
|
||||
|
||||
it("does not rewrite a write that lands while the editor closes", async () => {
|
||||
vi.useFakeTimers();
|
||||
const inFlight = deferred();
|
||||
const persist = vi
|
||||
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
|
||||
.mockReturnValueOnce(inFlight.promise)
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange: vi.fn(),
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.change("only");
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
coordinator.dispose();
|
||||
inFlight.resolve(AsyncResult.success(undefined));
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("retries a failed write when the editor closes", async () => {
|
||||
vi.useFakeTimers();
|
||||
const persist = vi
|
||||
.fn()
|
||||
.mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("write failed"))))
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange: vi.fn(),
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.change("latest");
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
expect(persist).toHaveBeenCalledOnce();
|
||||
|
||||
coordinator.dispose();
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).toHaveBeenCalledTimes(2);
|
||||
expect(persist).toHaveBeenLastCalledWith("latest");
|
||||
});
|
||||
|
||||
it("leaves the file pending when the latest write fails", async () => {
|
||||
vi.useFakeTimers();
|
||||
const onPendingChange = vi.fn();
|
||||
@@ -91,4 +183,49 @@ describe("FileSaveCoordinator", () => {
|
||||
expect(onPendingChange).toHaveBeenCalledWith(true);
|
||||
expect(onPendingChange).not.toHaveBeenCalledWith(false);
|
||||
});
|
||||
|
||||
it("ignores editor changes emitted after disposal", async () => {
|
||||
vi.useFakeTimers();
|
||||
const persist = vi
|
||||
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const onPendingChange = vi.fn();
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange,
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.dispose();
|
||||
coordinator.change("stale contents");
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).not.toHaveBeenCalled();
|
||||
expect(onPendingChange).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("does not persist confirmed contents again on disposal", async () => {
|
||||
vi.useFakeTimers();
|
||||
const persist = vi
|
||||
.fn<(contents: string) => Promise<AtomCommandResult<void, never>>>()
|
||||
.mockResolvedValue(AsyncResult.success(undefined));
|
||||
const coordinator = new FileSaveCoordinator({
|
||||
debounceMs: 500,
|
||||
persist,
|
||||
onPendingChange: vi.fn(),
|
||||
onConfirmed: vi.fn(),
|
||||
});
|
||||
|
||||
coordinator.change("temporary edit");
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
coordinator.change("original contents");
|
||||
await vi.advanceTimersByTimeAsync(500);
|
||||
expect(persist).toHaveBeenCalledTimes(2);
|
||||
|
||||
coordinator.dispose();
|
||||
await vi.runAllTimersAsync();
|
||||
|
||||
expect(persist).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
private latestContents = "";
|
||||
private latestRevision = 0;
|
||||
private confirmedRevision = 0;
|
||||
private lastChangeAt = 0;
|
||||
private saving = false;
|
||||
private disposed = false;
|
||||
@@ -18,6 +19,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
|
||||
constructor(private readonly options: FileSaveCoordinatorOptions<A, E>) {}
|
||||
|
||||
change(contents: string): void {
|
||||
if (this.disposed) return;
|
||||
this.latestContents = contents;
|
||||
this.latestRevision += 1;
|
||||
this.lastChangeAt = Date.now();
|
||||
@@ -46,7 +48,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
|
||||
}
|
||||
|
||||
private async persistLatest(): Promise<void> {
|
||||
if (this.saving || this.latestRevision === 0) return;
|
||||
if (this.saving || this.latestRevision === this.confirmedRevision) return;
|
||||
|
||||
this.saving = true;
|
||||
const contents = this.latestContents;
|
||||
@@ -54,6 +56,7 @@ export class FileSaveCoordinator<A = unknown, E = unknown> {
|
||||
const result = await this.options.persist(contents);
|
||||
const succeeded = result._tag === "Success";
|
||||
if (succeeded) {
|
||||
this.confirmedRevision = revision;
|
||||
this.options.onConfirmed(contents);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user