Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix: release recording stop requests after failures
Richie McIlroy96634b4b903c
diff --git a/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx b/apps/desktop/src/routes/(window-chrome)/new-main/index.tsxindex 54b90dc08..1cfb085c8 100644--- a/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx+++ b/apps/desktop/src/routes/(window-chrome)/new-main/index.tsx@@ -78,6 +78,7 @@ import {isRecordingStartCancelled,recordingMetaNeedsRecovery,recordingOpenErrorMessage,+ runRecordingStopRequest,} from "~/utils/recording";import {type CaptureDisplay,@@ -2907,14 +2908,18 @@ function Page() {};stopRequest = request;setStopRequested(true);- try {- await commands.stopRecording();- } catch (error) {- await dialog.message(- error instanceof Error ? error.message : String(error),- { title: "Stop Recording", kind: "error" },- );- }+ await runRecordingStopRequest({+ stop: () => commands.stopRecording(),+ isCurrent: () => stopRequest === request,+ onError: (error) => {+ stopErrorRequest = request;+ setStopError(error instanceof Error ? error.message : String(error));+ },+ onSettled: () => {+ stopRequest = undefined;+ setStopRequested(false);+ },+ });},}));diff --git a/apps/desktop/src/routes/in-progress-recording.tsx b/apps/desktop/src/routes/in-progress-recording.tsxindex a8ae9bcfd..47de69bdb 100644--- a/apps/desktop/src/routes/in-progress-recording.tsx+++ b/apps/desktop/src/routes/in-progress-recording.tsx@@ -36,7 +36,10 @@ import {createOptionsQuery,revealRecordingWindow,} from "~/utils/queries";-import { handleRecordingResult } from "~/utils/recording";+import {+ handleRecordingResult,+ runRecordingStopRequest,+} from "~/utils/recording";import type {CameraInfo,CurrentRecording,@@ -534,20 +537,19 @@ function InProgressRecordingInner() {};stopRequest = request;setStopRequested(true);- try {- await commands.stopRecording();- } catch (error) {- if (stopRequest === request) {+ await runRecordingStopRequest({+ stop: () => commands.stopRecording(),+ isCurrent: () => stopRequest === request,+ onError: (error) => {stopErrorRequest = request;setStopError(error instanceof Error ? error.message : String(error));setIssuePanelVisible(true);- }- } finally {- if (stopRequest === request) {+ },+ onSettled: () => {stopRequest = undefined;setStopRequested(false);- }- }+ },+ });},}));diff --git a/apps/desktop/src/utils/recording.test.ts b/apps/desktop/src/utils/recording.test.tsindex 178a7fcdc..b4e372444 100644--- a/apps/desktop/src/utils/recording.test.ts+++ b/apps/desktop/src/utils/recording.test.ts@@ -1,6 +1,10 @@import * as dialog from "@tauri-apps/plugin-dialog";import { beforeEach, describe, expect, it, vi } from "vitest";-import { handleRecordingResult, isRecordingStartCancelled } from "./recording";+import {+ handleRecordingResult,+ isRecordingStartCancelled,+ runRecordingStopRequest,+} from "./recording";vi.mock("@tauri-apps/api/event", () => ({ emit: vi.fn() }));vi.mock("@tauri-apps/plugin-dialog", () => ({ message: vi.fn() }));@@ -31,3 +35,60 @@ describe("recording start cancellation", () => {});});});++describe("recording stop requests", () => {+ it("releases a failed request so Stop can be retried while retaining the error", async () => {+ let pending = true;+ let error: unknown;+ const failure = new Error("Windows are still restoring");+ const stop = vi+ .fn()+ .mockRejectedValueOnce(failure)+ .mockResolvedValueOnce(undefined);+ const options = {+ stop,+ isCurrent: () => pending,+ onError: (value: unknown) => {+ error = value;+ },+ onSettled: () => {+ pending = false;+ },+ };+ await runRecordingStopRequest(options);+ expect(pending).toBe(false);+ expect(error).toBe(failure);+ pending = true;+ await runRecordingStopRequest(options);+ expect(stop).toHaveBeenCalledTimes(2);+ expect(pending).toBe(false);+ expect(error).toBe(failure);+ });++ it.each([false, true])(+ "ignores a stale completion after a new recording starts (failure: %s)",+ async (fails) => {+ let finish: () => void = () => {};+ const stopped = new Promise<void>((resolve) => {+ finish = resolve;+ });+ let current = true;+ const onError = vi.fn();+ const onSettled = vi.fn();+ const pending = runRecordingStopRequest({+ stop: async () => {+ await stopped;+ if (fails) throw new Error("Old stop error");+ },+ isCurrent: () => current,+ onError,+ onSettled,+ });+ current = false;+ finish();+ await pending;+ expect(onError).not.toHaveBeenCalled();+ expect(onSettled).not.toHaveBeenCalled();+ },+ );+});diff --git a/apps/desktop/src/utils/recording.ts b/apps/desktop/src/utils/recording.tsindex 137849023..4fde8b327 100644--- a/apps/desktop/src/utils/recording.ts+++ b/apps/desktop/src/utils/recording.ts@@ -39,6 +39,21 @@ export function recordingOpenErrorMessage(return error instanceof Error ? error.message : String(error);}+export async function runRecordingStopRequest(options: {+ stop: () => Promise<unknown>;+ isCurrent: () => boolean;+ onError: (error: unknown) => void;+ onSettled: () => void;+}) {+ try {+ await options.stop();+ } catch (error) {+ if (options.isCurrent()) options.onError(error);+ } finally {+ if (options.isCurrent()) options.onSettled();+ }+}+export function isRecordingStartCancelled(error: unknown): boolean {const message = error instanceof Error ? error.message : error;return message === "Recording cancelled before starting.";