mirror of
https://github.com/rommapp/romm.git
synced 2026-12-03 02:24:09 +01:00
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) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QTsTgdoBinvvxzXeLUjBmV
This commit is contained in:
@@ -60,6 +60,7 @@ export interface PendingAssetStore {
|
||||
function openDatabase(): Promise<IDBDatabase> {
|
||||
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<IDBDatabase> {
|
||||
}
|
||||
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<number, DetailedRomSchema>,
|
||||
): Promise<AssetOutcome | null> {
|
||||
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<PendingSyncResult> {
|
||||
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<number, DetailedRomSchema>();
|
||||
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 });
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
<script setup lang="ts">
|
||||
// v2 SelectSaveDialog — listens for `selectSaveDialog`, lists the ROM's saves
|
||||
// v2 SelectSaveDialog: listens for `selectSaveDialog`, lists the ROM's saves
|
||||
// and emits `saveSelected` on pick (consumed by the player). The list is the
|
||||
// launch screen's, so a save reads the same before the game boots and inside it.
|
||||
import { RBtn, RDialog } from "@v2/lib";
|
||||
|
||||
@@ -55,6 +55,7 @@ export function installPendingAssetSync() {
|
||||
const { isOffline } = useServerConnection();
|
||||
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
let disposed = false;
|
||||
|
||||
function stopRetrying() {
|
||||
if (timer === null) return;
|
||||
@@ -117,7 +118,9 @@ export function installPendingAssetSync() {
|
||||
await refresh(synced);
|
||||
}
|
||||
stopRetrying();
|
||||
if (await hasPendingAssets()) {
|
||||
// The shell can go while a pass is on the wire, and a retry armed after
|
||||
// that would outlive it and keep firing for the life of the document.
|
||||
if (!disposed && (await hasPendingAssets())) {
|
||||
timer = setTimeout(() => void drain(), RETRY_MS);
|
||||
}
|
||||
} finally {
|
||||
@@ -130,5 +133,8 @@ export function installPendingAssetSync() {
|
||||
watch([() => playingStore.playing, isOffline], () => void drain(), {
|
||||
immediate: true,
|
||||
});
|
||||
onScopeDispose(stopRetrying);
|
||||
onScopeDispose(() => {
|
||||
disposed = true;
|
||||
stopRetrying();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -117,6 +117,14 @@ async function forgetPendingSave() {
|
||||
pendingSave = null;
|
||||
await pendingAssetStore.clear(pendingSaveId);
|
||||
}
|
||||
// Whether the browser is holding these exact bytes, so a notice only promises
|
||||
// a later sync for a save that really was kept.
|
||||
function isSaveHeld(saveFile: Uint8Array): boolean {
|
||||
return (
|
||||
pendingSave !== null &&
|
||||
bytesEqual(new Uint8Array(pendingSave.bytes), saveFile)
|
||||
);
|
||||
}
|
||||
// The tick re-offers a save whose upload failed on every pass, so what was
|
||||
// said for these bytes is remembered: every save the game writes is announced,
|
||||
// every retry of the same one is not.
|
||||
@@ -139,6 +147,10 @@ function writeSave(
|
||||
const bytes = new Uint8Array(file.saveFile);
|
||||
inFlightSave = bytes;
|
||||
try {
|
||||
// Held before the attempt and dropped once the server answers, so no
|
||||
// path can upload a save without the browser keeping a copy, and none
|
||||
// can succeed while an older capture stays behind to be synced later.
|
||||
await rememberPendingSave(file.saveFile, file.screenshotFile);
|
||||
const save = await saveSave({
|
||||
rom: romRef.value,
|
||||
save: sessionSaveRef.value,
|
||||
@@ -150,6 +162,7 @@ function writeSave(
|
||||
sessionSaveRef.value = save;
|
||||
saveTracker.markUploaded(bytes);
|
||||
}
|
||||
if (save) await forgetPendingSave();
|
||||
return save;
|
||||
} finally {
|
||||
inFlightSave = null;
|
||||
@@ -457,10 +470,8 @@ function installAutoSaveSync() {
|
||||
const screenshotFile =
|
||||
storedScreenshotFor(pendingSave, saveBytes) ??
|
||||
(await captureScreenshot());
|
||||
await rememberPendingSave(saveBytes, screenshotFile);
|
||||
const save = await writeSave({ saveFile: saveBytes, screenshotFile });
|
||||
if (save) {
|
||||
await forgetPendingSave();
|
||||
heldBackSave = null;
|
||||
romsStore.update(romRef.value);
|
||||
displayMessage("Save synced with server", {
|
||||
@@ -468,8 +479,9 @@ function installAutoSaveSync() {
|
||||
tone: "success",
|
||||
icon: "mdi-cloud-sync",
|
||||
});
|
||||
// A write voided by a save being loaded is not the server refusing it.
|
||||
} else if (!saveLoading && !bytesEqual(saveFile, heldBackSave)) {
|
||||
// A write voided by a save being loaded holds nothing back, so there
|
||||
// is nothing to promise for it either.
|
||||
} else if (isSaveHeld(saveFile) && !bytesEqual(saveFile, heldBackSave)) {
|
||||
announceSaveHeldBack(saveFile);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -499,10 +511,8 @@ async function flushPendingSave() {
|
||||
// The exit takes no frame of its own; it carries the one stored when the
|
||||
// game wrote these bytes, if a sync got that far.
|
||||
const screenshotFile = storedScreenshotFor(pendingSave, saveBytes);
|
||||
await rememberPendingSave(saveBytes, screenshotFile);
|
||||
try {
|
||||
if (await writeSave({ saveFile: saveBytes, screenshotFile })) {
|
||||
await forgetPendingSave();
|
||||
romsStore.update(romRef.value);
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -600,7 +610,7 @@ window.EJS_onSaveSave = async function ({
|
||||
tone: "success",
|
||||
icon: "mdi-cloud-sync",
|
||||
});
|
||||
} else {
|
||||
} else if (isSaveHeld(new Uint8Array(saveFile))) {
|
||||
// Asked for by hand, so it answers every time, and the tick behind it
|
||||
// knows these bytes have been spoken for.
|
||||
announceSaveHeldBack(new Uint8Array(saveFile));
|
||||
@@ -812,7 +822,7 @@ window.EJS_onGameStart = async () => {
|
||||
// when the sync tick uploads it.
|
||||
await Promise.all([
|
||||
saveState({ rom: romRef.value, stateFile, screenshotFile }),
|
||||
writeSaveIfChanged({ saveFile }),
|
||||
writeSaveIfChanged({ saveFile: toArrayBuffer(saveFile) }),
|
||||
]);
|
||||
|
||||
romsStore.update(romRef.value);
|
||||
|
||||
Reference in New Issue
Block a user