Skip to content

Commit 7e69c04

Browse files
committed
fix(studio): a scene swap cancelled before it starts does not run
1 parent 8406290 commit 7e69c04

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

‎packages/studio/src/player/sceneSwap.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ describe("sceneSwapFor", () => {
7272
expect(replaced).toBe(false);
7373
});
7474

75+
it("does not start a swap whose cancel signal was aborted before the call", async () => {
76+
const fetchSpy = vi
77+
.spyOn(globalThis, "fetch")
78+
.mockImplementation(async () => new Response("<html></html>"));
79+
const { iframe, swap } = swappableIframe();
80+
const cancel = new AbortController();
81+
cancel.abort(new Error("superseded by a newer edit"));
82+
await expect(sceneSwapFor(iframe)!("/preview", () => true, cancel.signal)).rejects.toThrow(
83+
"superseded",
84+
);
85+
expect(fetchSpy).not.toHaveBeenCalled();
86+
expect(swap).not.toHaveBeenCalled();
87+
});
88+
7589
it("swaps a document that arrives in time", async () => {
7690
vi.spyOn(globalThis, "fetch").mockImplementation(async () => new Response("<html></html>"));
7791
const { iframe, swap } = swappableIframe();

‎packages/studio/src/player/sceneSwap.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function sceneSwapFor(
2525
const swap = win?.__hfSwapScenes;
2626
if (typeof swap !== "function") return null;
2727
return async (url, isCurrent, cancel) => {
28+
if (cancel?.aborted) throw cancel.reason;
2829
const deadline = new AbortController();
2930
const timer = setTimeout(
3031
() => deadline.abort(new Error("the preview took too long to swap")),

0 commit comments

Comments
 (0)