Skip to content

Commit 76ea2e7

Browse files
committed
fix(studio-server): a drag back to its start claims nothing, and next steps name their files
1 parent b9efd6c commit 76ea2e7

4 files changed

Lines changed: 26 additions & 5 deletions

File tree

‎packages/studio-server/src/history/projectHistory.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,20 @@ describe("claim: a writer that records after writing", () => {
334334
expect(history.list()[0]).toMatchObject({ id: first!.id, files: [{ path: "index.html" }] });
335335
});
336336

337+
it("a coalescing claim whose writes net to nothing returns null and records nothing", async () => {
338+
const { history, write } = await project({ "index.html": "A" });
339+
write("index.html", "B");
340+
expect(
341+
await history.claim(you, "Dragged Title", ["index.html"], { coalesceKey: "drag" }),
342+
).not.toBeNull();
343+
write("index.html", "A");
344+
expect(
345+
await history.claim(you, "Dragged Title", ["index.html"], { coalesceKey: "drag" }),
346+
).toBeNull();
347+
await history.flush();
348+
expect(history.list()).toEqual([]);
349+
});
350+
337351
it("a claim with another key, or its idle time, ends the coalescing claim", async () => {
338352
const { history, write } = await project({ "a.html": "A", "b.html": "B" });
339353
write("a.html", "A2");

‎packages/studio-server/src/history/projectHistory.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export interface ProjectHistory {
6868
/**
6969
* For a writer that records after writing (Studio): takes in every write so far and moves the not yet committed
7070
* outside changes to `paths` into one entry of `who`'s. Claims with the same coalesceKey merge until a claim with
71-
* another key, `idleMs` (default quietMs) without one, or any operation. Null when nothing was claimed.
71+
* another key, `idleMs` (default quietMs) without one, or any operation. Null when nothing was claimed, or
72+
* when a coalescing claim's writes net to nothing (a drag back to where it started).
7273
*/
7374
claim(
7475
who: HistoryWho,
@@ -313,12 +314,16 @@ class Engine {
313314
return taken;
314315
}
315316

316-
holdClaim(group: Group, key: string, idleMs = this.options.quietMs ?? 2000): { id: string } {
317+
holdClaim(
318+
group: Group,
319+
key: string,
320+
idleMs = this.options.quietMs ?? 2000,
321+
): { id: string } | null {
317322
clearTimeout(this.claimed?.timer);
318323
const timer = setTimeout(() => this.background(() => this.commitClaim()), idleMs);
319324
timer.unref?.();
320325
this.claimed = { group, key, timer };
321-
return { id: group.id };
326+
return group.changes.size ? { id: group.id } : null;
322327
}
323328

324329
async commitClaim(): Promise<void> {

‎packages/studio-server/src/routes/history.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe("history routes", () => {
6868
writeFileSync(join(projectDir, "index.html"), "B");
6969
await call(`/window/${windowId}/close`, {});
7070
expect(await (await call("")).json()).toMatchObject({
71-
back: { id: windowId, label: "Moved Title" },
71+
back: { id: windowId, label: "Moved Title", paths: ["index.html"] },
7272
forward: null,
7373
});
7474

‎packages/studio-server/src/routes/history.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ function idleOf(body: Record<string, unknown>): number | undefined {
3030
/** What Cmd+Z or Cmd+Shift+Z would revert next, so Studio can name it on its buttons. */
3131
function nextStep(entries: readonly HistoryEntry[], direction: "back" | "forward") {
3232
const target = stepTarget(entries, direction);
33-
return target ? { id: target.id, label: target.label, endedAt: target.endedAt } : null;
33+
if (!target) return null;
34+
const paths = target.files.map((file) => file.path);
35+
return { id: target.id, label: target.label, endedAt: target.endedAt, paths };
3436
}
3537

3638
/** Runs `task` on the project's history; no history is a 404, an engine refusal ("no longer kept") a 409. */

0 commit comments

Comments
 (0)