Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(rec): let a newer prefs snapshot win over the failed-write re-read

EtienneLescot7164b9e44604
diff --git a/src/components/ai-edition/v4/RecStage.test.tsx b/src/components/ai-edition/v4/RecStage.test.tsx
index 8844459e0..c9b65ef77 100644
--- a/src/components/ai-edition/v4/RecStage.test.tsx
+++ b/src/components/ai-edition/v4/RecStage.test.tsx
@@ -214,6 +214,55 @@ describe("RecStage controls", () => {
await waitFor(() => expect(button).toHaveAttribute("aria-pressed", "true"));
});
+ // Another window can land a change while this panel's write is failing. Its pushed
+ // snapshot is newer than the re-read that the failure starts, so the push must win.
+ it("keeps a snapshot pushed after a rejected write over the re-read", async () => {
+ const stored: RecordingPrefs = {
+ micEnabled: false,
+ micDeviceId: null,
+ micDeviceName: null,
+ camEnabled: false,
+ camDeviceId: null,
+ camDeviceName: null,
+ systemAudioEnabled: false,
+ cursorCaptureMode: "editable-overlay",
+ hideDesktopIcons: false,
+ autoZoomEnabled: true,
+ };
+ stubRecordingPrefs();
+ let answerReread: ((prefs: RecordingPrefs) => void) | undefined;
+ const getRecordingPrefs = vi
+ .fn<() => Promise<RecordingPrefs>>()
+ .mockResolvedValueOnce(stored)
+ .mockImplementationOnce(
+ () =>
+ new Promise((resolve) => {
+ answerReread = resolve;
+ }),
+ );
+ const setRecordingPrefs = vi.fn(async () => {
+ throw new Error("write failed");
+ });
+ Object.assign(window.electronAPI as object, { getRecordingPrefs, setRecordingPrefs });
+ renderRecStage();
+ await waitFor(() => expect(getRecordingPrefs).toHaveBeenCalledTimes(1));
+
+ const button = screen.getByTestId("rec-auto-zoom-button");
+ await act(async () => {
+ button.click();
+ });
+ await waitFor(() => expect(getRecordingPrefs).toHaveBeenCalledTimes(2));
+ act(() => {
+ recordingPrefsListeners.forEach((listener) =>
+ listener({ ...stored, autoZoomEnabled: false }),
+ );
+ });
+ await act(async () => {
+ answerReread?.(stored);
+ });
+ expect(button).toHaveAttribute("aria-pressed", "false");
+ });
+
it("waits for microphone discovery before starting the meter and normalizes default", async () => {
stubRecordingPrefs({
micEnabled: true,
diff --git a/src/components/ai-edition/v4/RecStage.tsx b/src/components/ai-edition/v4/RecStage.tsx
index ba0ea0dbc..72f51454c 100644
--- a/src/components/ai-edition/v4/RecStage.tsx
+++ b/src/components/ai-edition/v4/RecStage.tsx
@@ -78,11 +78,15 @@ export function RecStage({
}) {
const t = useScopedT("editor");
const [prefs, setPrefsState] = useState<RecordingPrefsState>(DEFAULT_PREFS);
+ // Bumped by every local change and every pushed snapshot, so the re-read after a
+ // failed write can tell that something newer has landed since.
+ const prefsRevision = useRef(0);
useEffect(() => {
let cancelled = false;
let receivedNewerSnapshot = false;
const unsubscribe = window.electronAPI?.onRecordingPrefsChanged?.((next) => {
receivedNewerSnapshot = true;
+ prefsRevision.current += 1;
if (!cancelled) setPrefsState(normalizedRecordingPrefs(next));
});
void window.electronAPI
@@ -103,6 +107,7 @@ export function RecStage({
};
}, []);
const updatePrefs = (patch: Partial<RecordingPrefsState>) => {
+ const revision = ++prefsRevision.current;
// The write is fired beside the optimistic patch rather than inside the updater:
// an updater is expected to be pure, and React re-invokes it under StrictMode,
// which sent every preference change twice.
@@ -113,11 +118,13 @@ export function RecStage({
// import reads back when it decides whether to place zooms, so a rejected
// write that left the patch on screen would have the user looking at Off
// while the next take still gets decorated. Re-read rather than invert the
- // patch: a snapshot pushed while the write was in flight is newer than
- // anything a rollback could restore.
+ // patch, and drop the answer if a pushed snapshot or a later change landed
+ // meanwhile: either is newer than what this re-read can say.
try {
const current = await window.electronAPI?.getRecordingPrefs?.();
- if (current) setPrefsState(normalizedRecordingPrefs(current));
+ if (current && revision === prefsRevision.current) {
+ setPrefsState(normalizedRecordingPrefs(current));
+ }
} catch (readErr) {
console.warn("[rec-stage] failed to re-read the recording prefs:", readErr);
}