From 119c3acb18eb1c3deda4748f1564e1c7914ce55a Mon Sep 17 00:00:00 2001 From: Prayas Lashkari Date: Tue, 17 Mar 2026 01:57:55 -0400 Subject: [PATCH] feat: implement async restart recording functionality to ensure proper session handling --- src/hooks/useScreenRecorder.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts index dbe91528..a69da598 100644 --- a/src/hooks/useScreenRecorder.ts +++ b/src/hooks/useScreenRecorder.ts @@ -377,11 +377,23 @@ export function useScreenRecorder(): UseScreenRecorderReturn { recording ? stopRecording.current() : startRecording(); }; - const restartRecording = () => { + const restartRecording = async () => { if (!recording) return; discardRecording.current = true; + + // Wait for the old recorder's onstop to fully complete before starting + // a new session, so the discard logic doesn't race with new chunks. + const waitForStop = new Promise((resolve) => { + if (mediaRecorder.current) { + mediaRecorder.current.addEventListener("stop", () => resolve(), { once: true }); + } else { + resolve(); + } + }); + stopRecording.current(); - startRecording(); + await waitForStop; + await startRecording(); }; return {