Skip to content

Commit 890a18c

Browse files
committed
Fixed pause / resume sync on separate tabs + timer
1 parent dfa0470 commit 890a18c

File tree

11 files changed

+382
-121
lines changed

11 files changed

+382
-121
lines changed

src/manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "__MSG_extName__",
44
"description": "__MSG_extDesc__",
55
"default_locale": "en",
6-
"version": "4.2.2",
6+
"version": "4.2.3",
77
"background": {
88
"service_worker": "background.bundle.js"
99
},
@@ -101,7 +101,7 @@
101101
"managed_schema": "schema.json"
102102
},
103103
"externally_connectable": {
104-
"matches": ["https://app.screenity.io/*", "http://localhost:3000/*"]
104+
"matches": ["https://app.screenity.io/*"]
105105
},
106106
"optional_permissions": [
107107
"offscreen",

src/pages/Background/listeners/onTabActivatedListener.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,21 @@ export const handleTabActivation = async (activeInfo) => {
55
const {
66
recordingStartTime,
77
recording,
8+
paused,
9+
pausedAt,
10+
totalPausedMs,
811
restarting,
912
pendingRecording,
1013
recorderSession,
1114
} = await chrome.storage.local.get([
1215
"recordingStartTime",
1316
"recording",
14-
"restarting",
15-
"pendingRecording",
16-
"recorderSession",
1717
"paused",
1818
"pausedAt",
1919
"totalPausedMs",
20+
"restarting",
21+
"pendingRecording",
22+
"recorderSession",
2023
]);
2124

2225
// Get the activated tab

src/pages/Background/listeners/onTabUpdatedListener.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,24 @@ export const handleTabUpdate = async (tabId, changeInfo, tab) => {
55
if (changeInfo.status === "complete") {
66
const {
77
recording,
8+
paused,
9+
pausedAt,
10+
totalPausedMs,
811
restarting,
912
tabRecordedID,
1013
pendingRecording,
1114
recordingStartTime,
1215
recorderSession,
1316
} = await chrome.storage.local.get([
1417
"recording",
18+
"paused",
19+
"pausedAt",
20+
"totalPausedMs",
1521
"restarting",
1622
"tabRecordedID",
1723
"pendingRecording",
1824
"recordingStartTime",
1925
"recorderSession",
20-
"paused",
21-
"pausedAt",
22-
"totalPausedMs",
2326
]);
2427

2528
// Check both recording flag AND recorderSession to avoid race conditions
@@ -60,9 +63,9 @@ export const handleTabUpdate = async (tabId, changeInfo, tab) => {
6063
if (alarm) {
6164
const { alarmTime } = await chrome.storage.local.get(["alarmTime"]);
6265
const remaining = Math.max(0, Math.floor(alarmTime - elapsed));
63-
sendMessageTab(activeInfo.tabId, { type: "time", time: remaining });
66+
sendMessageTab(tabId, { type: "time", time: remaining });
6467
} else {
65-
sendMessageTab(activeInfo.tabId, { type: "time", time: elapsed });
68+
sendMessageTab(tabId, { type: "time", time: elapsed });
6669
}
6770
}
6871

src/pages/Background/messaging/handlers.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -959,17 +959,27 @@ export const setupHandlers = () => {
959959
return await loginWithWebsite();
960960
});
961961
registerMessage("sync-recording-state", async (message, sendResponse) => {
962-
const { recording, paused, recordingStartTime, pendingRecording } =
963-
await chrome.storage.local.get([
964-
"recording",
965-
"paused",
966-
"recordingStartTime",
967-
"pendingRecording",
968-
]);
962+
const {
963+
recording,
964+
paused,
965+
recordingStartTime,
966+
pausedAt,
967+
totalPausedMs,
968+
pendingRecording,
969+
} = await chrome.storage.local.get([
970+
"recording",
971+
"paused",
972+
"recordingStartTime",
973+
"pausedAt",
974+
"totalPausedMs",
975+
"pendingRecording",
976+
]);
969977
sendResponse({
970978
recording: Boolean(recording),
971979
paused: Boolean(paused),
972980
recordingStartTime: recordingStartTime || null,
981+
pausedAt: pausedAt || null,
982+
totalPausedMs: totalPausedMs || 0,
973983
pendingRecording: Boolean(pendingRecording),
974984
});
975985
return true;

src/pages/Background/recording/startRecording.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ import { sendMessageRecord } from "./sendMessageRecord";
22

33
export const startRecording = async () => {
44
chrome.storage.local.set({
5-
recordingStartTime: Date.now(),
65
restarting: false,
7-
recording: true,
86
});
97

108
// Check if customRegion is set
@@ -57,7 +55,6 @@ export const startAfterCountdown = async () => {
5755

5856
// If there is an active recording tab or offscreen document, begin recording
5957
if (recordingTab !== null || offscreen) {
60-
await chrome.storage.local.set({ recording: true });
6158
startRecording();
6259
}
6360
} catch (error) {

src/pages/CloudRecorder/CloudRecorder.jsx

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const CloudRecorder = () => {
9292
const recoveryAttempted = useRef(false);
9393
const screenTrackLostRef = useRef(false);
9494
const screenTrackMonitor = useRef(null);
95+
const pausedStateRef = useRef(false);
9596

9697
// This checks if the recording was previously initialized
9798
const isInit = useRef(false);
@@ -557,6 +558,14 @@ const CloudRecorder = () => {
557558
}
558559
};
559560

561+
const setRecordingTimingState = async (nextState) => {
562+
try {
563+
await chrome.storage.local.set(nextState);
564+
} catch (err) {
565+
console.warn("Failed to persist recording timing state:", err);
566+
}
567+
};
568+
560569
const flushPendingChunks = async () => {
561570
if (screenUploader.current) {
562571
try {
@@ -627,6 +636,14 @@ const CloudRecorder = () => {
627636

628637
// Stop the silent audio keep-alive
629638
stopTabKeepAlive();
639+
await setRecordingTimingState({
640+
recording: false,
641+
paused: false,
642+
recordingStartTime: null,
643+
pausedAt: null,
644+
totalPausedMs: 0,
645+
});
646+
pausedStateRef.current = false;
630647

631648
const { projectId, multiMode, multiSceneCount, recordingToScene } =
632649
await chrome.storage.local.get([
@@ -1184,7 +1201,15 @@ const CloudRecorder = () => {
11841201
screenTimer.current.notificationInterval = timerInterval;
11851202
screenTimer.current.warned = warned;
11861203

1187-
chrome.storage.local.set({ recording: true });
1204+
const recordingStartTime = Date.now();
1205+
await setRecordingTimingState({
1206+
recording: true,
1207+
paused: false,
1208+
recordingStartTime,
1209+
pausedAt: null,
1210+
totalPausedMs: 0,
1211+
});
1212+
pausedStateRef.current = false;
11881213
persistSessionState({ status: "recording" });
11891214
setStarted(true);
11901215
} catch (err) {
@@ -1623,7 +1648,14 @@ const CloudRecorder = () => {
16231648
}
16241649
}
16251650
isFinishing.current = true;
1626-
chrome.storage.local.set({ recording: false });
1651+
await setRecordingTimingState({
1652+
recording: false,
1653+
paused: false,
1654+
recordingStartTime: null,
1655+
pausedAt: null,
1656+
totalPausedMs: 0,
1657+
});
1658+
pausedStateRef.current = false;
16271659
stopAllIntervals();
16281660

16291661
await stopAllRecorders();
@@ -2357,6 +2389,7 @@ const CloudRecorder = () => {
23572389
return true;
23582390
} else if (request.type === "pause-recording-tab") {
23592391
if (!isInit.current) return;
2392+
if (pausedStateRef.current) return;
23602393

23612394
// Pause all active recorders
23622395
if (
@@ -2387,8 +2420,14 @@ const CloudRecorder = () => {
23872420
cameraTimer.current.total += now - cameraTimer.current.start;
23882421
cameraTimer.current.paused = true;
23892422
}
2423+
pausedStateRef.current = true;
2424+
void setRecordingTimingState({
2425+
paused: true,
2426+
pausedAt: now,
2427+
});
23902428
} else if (request.type === "resume-recording-tab") {
23912429
if (!isInit.current) return;
2430+
if (!pausedStateRef.current) return;
23922431

23932432
// Resume all paused recorders
23942433
if (screenRecorder.current && screenRecorder.current.state === "paused") {
@@ -2410,6 +2449,23 @@ const CloudRecorder = () => {
24102449
cameraTimer.current.start = now;
24112450
cameraTimer.current.paused = false;
24122451
}
2452+
pausedStateRef.current = false;
2453+
void (async () => {
2454+
try {
2455+
const { pausedAt, totalPausedMs } = await chrome.storage.local.get([
2456+
"pausedAt",
2457+
"totalPausedMs",
2458+
]);
2459+
const additional = pausedAt ? Math.max(0, now - pausedAt) : 0;
2460+
await setRecordingTimingState({
2461+
paused: false,
2462+
pausedAt: null,
2463+
totalPausedMs: (totalPausedMs || 0) + additional,
2464+
});
2465+
} catch (err) {
2466+
console.warn("Failed to update resume timing state:", err);
2467+
}
2468+
})();
24132469
} else if (request.type === "dismiss-recording") {
24142470
if (!isInit.current) return;
24152471
dismissRecording();

0 commit comments

Comments
 (0)