Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(recording): let a pause finish before a resume or stop acts

davbio3707bc5a80fa
diff --git a/src/entrypoints/background/__tests__/pause-narration.test.ts b/src/entrypoints/background/__tests__/pause-narration.test.ts
index 435d64c..0b95e45 100644
--- a/src/entrypoints/background/__tests__/pause-narration.test.ts
+++ b/src/entrypoints/background/__tests__/pause-narration.test.ts
@@ -29,7 +29,8 @@ vi.mock('../voice', () => ({
whenNarrationSettled: (...args: unknown[]) => whenNarrationSettled(...args),
}));
-import { pauseCapture, restartNarrationOnceTranscriptionSettles, resumeCapture } from '../pause';
+import { pauseCapture, restartNarrationOnceTranscriptionSettles, resumeCapture, resumeFromPause } from '../pause';
+import { broadcastDismissBlur } from '../tab-manager';
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
@@ -178,3 +179,96 @@ describe('the retry that waits for the transcription', () => {
expect(reportNarrationLost).toHaveBeenCalled();
});
});
+
+describe('a resume that lands while the pause is still flushing', () => {
+ it('waits for the pause to stop the mic before it restarts it', async () => {
+ voicePhase = 'recording';
+ const order: string[] = [];
+ let finishStop!: () => void;
+ stopVoiceNarration.mockImplementation(
+ () =>
+ new Promise<void>((resolve) => {
+ finishStop = () => {
+ order.push('mic stopped');
+ voicePhase = 'transcribing';
+ resolve();
+ };
+ }),
+ );
+ const tryStart = vi.fn(() => {
+ order.push('restart attempted');
+ return true;
+ });
+
+ const pausing = pauseCapture('manual');
+ await flush();
+ const resuming = resumeCapture(tryStart);
+ await flush();
+ expect(actor.getSnapshot().value).toBe('PAUSED');
+
+ finishStop();
+ await pausing;
+ await resuming;
+ await flush();
+
+ expect(actor.getSnapshot().value).toBe('RECORDING');
+ expect(order).toEqual(['mic stopped', 'restart attempted']);
+ });
+
+ it('keeps waiting on the first pause when a second pause is pressed during its flush', async () => {
+ voicePhase = 'recording';
+ let finishStop!: () => void;
+ stopVoiceNarration.mockImplementation(
+ () =>
+ new Promise<void>((resolve) => {
+ finishStop = () => {
+ voicePhase = 'transcribing';
+ resolve();
+ };
+ }),
+ );
+ const tryStart = vi.fn(() => true);
+
+ const pausing = pauseCapture('manual');
+ await flush();
+ expect(await pauseCapture('manual')).toBe(false);
+ const resuming = resumeCapture(tryStart);
+ await flush();
+
+ expect(actor.getSnapshot().value).toBe('PAUSED');
+ expect(tryStart).not.toHaveBeenCalled();
+
+ finishStop();
+ await pausing;
+ await resuming;
+ expect(actor.getSnapshot().value).toBe('RECORDING');
+ });
+
+ it('closes the blur overlay only after the pause that opened it has settled', async () => {
+ voicePhase = 'recording';
+ const order: string[] = [];
+ let finishStop!: () => void;
+ stopVoiceNarration.mockImplementation(
+ () =>
+ new Promise<void>((resolve) => {
+ finishStop = () => {
+ order.push('pause settled');
+ resolve();
+ };
+ }),
+ );
+ vi.mocked(broadcastDismissBlur).mockImplementation(async () => {
+ order.push('overlay dismissed');
+ });
+
+ const pausing = pauseCapture('blur');
+ await flush();
+ const resuming = resumeFromPause(vi.fn(() => true));
+ await flush();
+ finishStop();
+ await pausing;
+ await resuming;
+
+ expect(order).toEqual(['pause settled', 'overlay dismissed']);
+ });
+});
diff --git a/src/entrypoints/background/index.ts b/src/entrypoints/background/index.ts
index f9444de..6b69864 100644
--- a/src/entrypoints/background/index.ts
+++ b/src/entrypoints/background/index.ts
@@ -28,7 +28,7 @@ import { recordUpdate } from '@/lib/update-notice';
import { getActor, getStateUpdate, initActor, initActorFallback, waitUntilReady } from './actor';
import { generateDescriptionOnDemand, generateGuideMetaOnStop, settlePendingDescriptions } from './guide-meta';
import { registerNavigationListeners } from './navigation';
-import { pauseCapture, resumeFromPause } from './pause';
+import { pauseCapture, resumeFromPause, whenPauseSettled } from './pause';
import { handleCaptureStep, handleFinalizeInputStep, handleUpdateInputStep } from './step-pipeline';
import {
broadcastClearBlur,
@@ -150,6 +150,7 @@ export default defineBackground(() => {
onMessage('stopRecording', async () => {
await waitUntilReady();
+ await whenPauseSettled();
const actor = getActor();
const { currentGuideId: guideId, insertTargetGuideId, insertAtIndex } = actor.getSnapshot().context;
await broadcastStopCapture();
diff --git a/src/entrypoints/background/pause.ts b/src/entrypoints/background/pause.ts
index 0ceed7f..571784e 100644
--- a/src/entrypoints/background/pause.ts
+++ b/src/entrypoints/background/pause.ts
@@ -21,7 +21,25 @@ const NARRATION_RESTART_ATTEMPTS = 6;
export type NarrationStarter = () => Promise<boolean> | boolean;
-export async function pauseCapture(reason: PauseReason): Promise<boolean> {
+let pauseInFlight: Promise<boolean> | null = null;
+
+export async function whenPauseSettled(): Promise<void> {
+ await pauseInFlight?.catch(() => false);
+}
+
+export function pauseCapture(reason: PauseReason): Promise<boolean> {
+ if (getActor().getSnapshot().value !== CaptureState.RECORDING) return Promise.resolve(false);
+ const pausing = pauseAndStopNarration(reason);
+ pauseInFlight = pausing;
+ void pausing
+ .catch(() => false)
+ .finally(() => {
+ if (pauseInFlight === pausing) pauseInFlight = null;
+ });
+ return pausing;
+}
+
+async function pauseAndStopNarration(reason: PauseReason): Promise<boolean> {
const actor = getActor();
if (actor.getSnapshot().value !== CaptureState.RECORDING) return false;
@@ -35,6 +53,7 @@ export async function pauseCapture(reason: PauseReason): Promise<boolean> {
}
export async function resumeCapture(tryStartNarration?: NarrationStarter): Promise<boolean> {
+ await whenPauseSettled();
const actor = getActor();
if (actor.getSnapshot().value !== CaptureState.PAUSED) return false;
@@ -70,6 +89,7 @@ export async function restartNarrationOnceTranscriptionSettles(tryStartNarration
}
export async function resumeFromPause(tryStartNarration?: NarrationStarter): Promise<boolean> {
+ await whenPauseSettled();
await broadcastDismissBlur();
return resumeCapture(tryStartNarration);
}