Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/studio/src/hooks/useEditorSave.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ async function mountEditorSave(writeProjectFile: WriteProjectFile) {
return {
handle: captured.handle,
showToast,
rerender: async () => {
await act(async () => root.render(<Probe />));
if (!captured.handle) throw new Error("Editor save handle was not mounted");
return captured.handle;
},
unmount: () => act(async () => root.unmount()),
};
}
Expand All @@ -60,6 +65,28 @@ describe("useEditorSave pending work", () => {
vi.unstubAllGlobals();
});

it("keeps the pending getter stable across redraws while reading current edits", async () => {
const mounted = await mountEditorSave(vi.fn(async () => undefined));
const getPendingCandidate = mounted.handle.getPendingCandidate;
try {
expect(getPendingCandidate()).toBeNull();
for (let redraw = 0; redraw < 30; redraw++) {
const current = await mounted.rerender();
expect(current.getPendingCandidate).toBe(getPendingCandidate);
}
act(() => mounted.handle.handleContentChange("latest pending source"));
expect(getPendingCandidate()).toEqual({
projectId: "project-a",
path: "index.html",
content: "latest pending source",
});
act(() => mounted.handle.discardPendingSave());
expect(getPendingCandidate()).toBeNull();
} finally {
await mounted.unmount();
}
});

it("exposes and flushes the latest rAF-buffered source candidate", async () => {
const writeProjectFile = vi.fn(async () => undefined);
const mounted = await mountEditorSave(writeProjectFile);
Expand Down
4 changes: 3 additions & 1 deletion packages/studio/src/hooks/useEditorSave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,12 @@ export function useEditorSave({
pendingCandidateRef.current = null;
}, []);

const getPendingCandidate = useCallback(() => pendingCandidateRef.current, []);

return {
saveRafRef,
handleContentChange,
getPendingCandidate: () => pendingCandidateRef.current,
getPendingCandidate,
flushPendingSave,
discardPendingSave,
};
Expand Down