Skip to content

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

feat: implement async restart recording functionality to ensure proper session handling

Prayas Lashkari119c3acb18eb
diff --git a/src/hooks/useScreenRecorder.ts b/src/hooks/useScreenRecorder.ts
index dbe915284..a69da598f 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<void>((resolve) => {
+ if (mediaRecorder.current) {
+ mediaRecorder.current.addEventListener("stop", () => resolve(), { once: true });
+ } else {
+ resolve();
+ }
+ });
+
stopRecording.current();
- startRecording();
+ await waitForStop;
+ await startRecording();
};
return {