Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(chrome-extension): serialize shared recording state writes

Richie McIlroy0a401c84ba07
diff --git a/apps/chrome-extension/src/shared/storage.test.ts b/apps/chrome-extension/src/shared/storage.test.ts
index f60cf32d7..ac3ac82fe 100644
--- a/apps/chrome-extension/src/shared/storage.test.ts
+++ b/apps/chrome-extension/src/shared/storage.test.ts
@@ -3,8 +3,10 @@ import {
type FailedRecording,
loadFailedRecordings,
loadOverlayTokens,
+ loadSharedRecordingState,
loadSharedUiState,
registerOverlayToken,
+ saveSharedRecordingState,
updateSharedUiState,
upsertFailedRecording,
} from "./storage";
@@ -13,8 +15,11 @@ import {
// concurrent read-modify-write calls interleave (both read before either
// writes). The fake reproduces that timing so the tests fail without the
// per-key write queue in storage.ts.
-const createAsyncStorageArea = () => {
+const createAsyncStorageArea = (
+ getSetDelayMs: (writeIndex: number) => number = () => 0,
+) => {
const data = new Map<string, unknown>();
+ let writeIndex = 0;
return {
get(keys: string[], callback: (items: Record<string, unknown>) => void) {
const snapshot: Record<string, unknown> = {};
@@ -24,12 +29,14 @@ const createAsyncStorageArea = () => {
setTimeout(() => callback(snapshot), 0);
},
set(items: Record<string, unknown>, callback: () => void) {
+ const delayMs = getSetDelayMs(writeIndex);
+ writeIndex += 1;
setTimeout(() => {
for (const [key, value] of Object.entries(items)) {
data.set(key, value);
}
callback();
- }, 0);
+ }, delayMs);
},
remove(keys: string[] | string, callback: () => void) {
setTimeout(() => {
@@ -113,4 +120,30 @@ describe("storage read-modify-write serialization", () => {
"session-b",
]);
});
+
+ it("keeps the newest recording status when an earlier write is slower", async () => {
+ (globalThis as { chrome?: unknown }).chrome = {
+ storage: {
+ local: createAsyncStorageArea(),
+ session: createAsyncStorageArea((writeIndex) =>
+ writeIndex === 0 ? 20 : 0,
+ ),
+ },
+ };
+
+ await Promise.all([
+ saveSharedRecordingState({
+ status: { phase: "creating" },
+ plan: null,
+ updatedAt: 1,
+ }),
+ saveSharedRecordingState({
+ status: { phase: "idle" },
+ plan: null,
+ updatedAt: 2,
+ }),
+ ]);
+
+ expect((await loadSharedRecordingState())?.status.phase).toBe("idle");
+ });
});
diff --git a/apps/chrome-extension/src/shared/storage.ts b/apps/chrome-extension/src/shared/storage.ts
index f0d369ee3..124636644 100644
--- a/apps/chrome-extension/src/shared/storage.ts
+++ b/apps/chrome-extension/src/shared/storage.ts
@@ -481,7 +481,9 @@ export const loadSharedRecordingState =
};
export const saveSharedRecordingState = (state: SharedRecordingState) =>
- setSession({ [RECORDING_STATE_KEY]: state });
+ withKeyLock(RECORDING_STATE_KEY, () =>
+ setSession({ [RECORDING_STATE_KEY]: state }),
+ );
export const loadSharedUiState = async (): Promise<SharedUiState> => {
const result = await getSession([SHARED_UI_STATE_KEY]);