From fb2b34aa5d6b3ded0beafae4df623f8d60a2d0ff Mon Sep 17 00:00:00 2001 From: zurdi Date: Fri, 18 Sep 2026 03:22:32 +0200 Subject: [PATCH] fix(player): hold every save before it goes, and drop it once it lands code-review xhigh findings. Holding the bytes was the tick's job, so the paths that skipped the tick skipped it too: Save & Quit and the in-game Save button lost the save outright when the upload failed, and told the player it had been kept. And a capture the tick failed on stayed behind after a later save succeeded, so the app shell uploaded it afterwards as the newest version in the slot, rolling the player back. The hold now lives in the write itself, after the guards, so a voided write holds nothing and a write the server takes leaves nothing behind. The notices are gated on what is actually held. Save & Quit also handed the core's own view of the SRAM to the upload instead of a standalone copy. Alongside: - The store gives up when another tab holds an upgrade off, rather than waiting on an event that never comes and wedging every later pass. - A drain in flight when the shell goes cannot arm a retry that outlives it. - One pass fetches a rom once however many of its captures it is carrying. - The toast host looks at the screen when it mounts, not only when it changes. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01QTsTgdoBinvvxzXeLUjBmV --- frontend/src/services/pending-asset.ts | 25 +++++++++++++++--- .../Notifications/NotificationHost.vue | 2 ++ .../v2/components/Player/SelectSaveDialog.vue | 2 +- .../composables/usePendingAssetSync/index.ts | 10 +++++-- .../src/views/Player/EmulatorJS/Player.vue | 26 +++++++++++++------ 5 files changed, 50 insertions(+), 15 deletions(-) diff --git a/frontend/src/services/pending-asset.ts b/frontend/src/services/pending-asset.ts index 7fd1a0453..0996bf82b 100644 --- a/frontend/src/services/pending-asset.ts +++ b/frontend/src/services/pending-asset.ts @@ -60,6 +60,7 @@ export interface PendingAssetStore { function openDatabase(): Promise { return new Promise((resolve, reject) => { const request = indexedDB.open(DB_NAME, DB_VERSION); + let abandoned = false; request.onupgradeneeded = () => { const db = request.result; for (const name of Array.from(db.objectStoreNames)) { @@ -67,8 +68,19 @@ function openDatabase(): Promise { } db.createObjectStore(STORE_NAME, { keyPath: "id" }); }; - request.onsuccess = () => resolve(request.result); + request.onsuccess = () => { + // The open landed after the wait was given up on; nothing holds the + // connection now, and leaving it open blocks every other tab's upgrade. + if (abandoned) return request.result.close(); + resolve(request.result); + }; request.onerror = () => reject(request.error); + // Another tab on an older version holds the upgrade off, and none of the + // handlers above fires meanwhile: without this the caller waits forever. + request.onblocked = () => { + abandoned = true; + reject(new Error("Pending asset storage is open in another tab")); + }; }); } @@ -254,6 +266,7 @@ async function settle( async function uploadPendingAsset( entry: PendingAsset, + roms: Map, ): Promise { if (accepted.has(entry.id)) return null; if (!entry.bytes?.byteLength) { @@ -261,9 +274,10 @@ async function uploadPendingAsset( return null; } - let rom: DetailedRomSchema | null = null; + let rom: DetailedRomSchema | null = roms.get(entry.romId) ?? null; try { - rom = (await romApi.getRom({ romId: entry.romId })).data; + rom ??= (await romApi.getRom({ romId: entry.romId })).data; + roms.set(entry.romId, rom); const upload = entry.kind === "state" ? await uploadState(entry, rom) @@ -315,9 +329,12 @@ export async function syncPendingAssets( ): Promise { const synced: SyncedAsset[] = []; const dropped: DroppedAsset[] = []; + // Several captures of one game are the common case, and they all need the + // same rom to name themselves and their files. + const roms = new Map(); for (const entry of await pendingAssetStore.list()) { if (!kinds.includes(entry.kind)) continue; - const outcome = await uploadPendingAsset(entry); + const outcome = await uploadPendingAsset(entry, roms); if (!outcome) continue; if (outcome.reason === undefined) synced.push(outcome.asset); else dropped.push({ ...outcome.asset, reason: outcome.reason }); diff --git a/frontend/src/v2/components/Notifications/NotificationHost.vue b/frontend/src/v2/components/Notifications/NotificationHost.vue index f37bf9335..68a759bf1 100644 --- a/frontend/src/v2/components/Notifications/NotificationHost.vue +++ b/frontend/src/v2/components/Notifications/NotificationHost.vue @@ -93,6 +93,8 @@ function trackFullscreen() { fullscreenHost.value = document.fullscreenElement as HTMLElement | null; } document.addEventListener("fullscreenchange", trackFullscreen); +// A host mounted while a game already owns the screen gets no event of its own. +trackFullscreen(); onBeforeUnmount(() => { emitter?.off("snackbarShow", openHandler); diff --git a/frontend/src/v2/components/Player/SelectSaveDialog.vue b/frontend/src/v2/components/Player/SelectSaveDialog.vue index f9c47388c..74845614d 100644 --- a/frontend/src/v2/components/Player/SelectSaveDialog.vue +++ b/frontend/src/v2/components/Player/SelectSaveDialog.vue @@ -1,5 +1,5 @@