mirror of
https://github.com/rommapp/romm.git
synced 2026-11-30 18:42:34 +01:00
fix(v2): revert local status and handle rejection when a launch write fails
The launch write optimistically mutated the shared rom_user but was fire-and-forget, so a network/authorization/server failure produced an unhandled rejection while the client kept status values that were never persisted. Introduce a shared recordLaunch() helper that snapshots the fields, applies the optimistic update, and on failure reverts them and logs instead of leaving the rejection unhandled. Use it in all three v2 players (EmulatorJS, Ruffle, Stream) so the behavior is consistent. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { RomUserSchema } from "@/__generated__";
|
||||
import { applyLaunchStatus } from "./romStatus";
|
||||
import romApi from "@/services/api/rom";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import { applyLaunchStatus, recordLaunch } from "./romStatus";
|
||||
|
||||
// Minimal RomUserSchema factory — only the fields applyLaunchStatus reads
|
||||
vi.mock("@/services/api/rom", () => ({
|
||||
default: { updateUserRomProps: vi.fn() },
|
||||
}));
|
||||
|
||||
const updateUserRomProps = vi.mocked(romApi.updateUserRomProps);
|
||||
|
||||
// Minimal RomUserSchema factory — only the fields the launch helpers read
|
||||
// matter, the rest are filled with representative defaults.
|
||||
function makeRomUser(overrides: Partial<RomUserSchema> = {}): RomUserSchema {
|
||||
return {
|
||||
@@ -61,3 +69,43 @@ describe("applyLaunchStatus", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
function makeRom(romUser: RomUserSchema): SimpleRom {
|
||||
return { id: 1, rom_user: romUser } as unknown as SimpleRom;
|
||||
}
|
||||
|
||||
describe("recordLaunch", () => {
|
||||
beforeEach(() => {
|
||||
updateUserRomProps.mockReset();
|
||||
});
|
||||
|
||||
it("applies the launch status and persists it with last_played", () => {
|
||||
updateUserRomProps.mockResolvedValue({} as never);
|
||||
const ru = makeRomUser({ status: null });
|
||||
recordLaunch(makeRom(ru));
|
||||
|
||||
expect(ru.now_playing).toBe(true);
|
||||
expect(ru.status).toBe("incomplete");
|
||||
expect(updateUserRomProps).toHaveBeenCalledWith({
|
||||
romId: 1,
|
||||
data: ru,
|
||||
updateLastPlayed: true,
|
||||
});
|
||||
});
|
||||
|
||||
it("reverts the local mutation when the write fails", async () => {
|
||||
updateUserRomProps.mockRejectedValue(new Error("boom"));
|
||||
const ru = makeRomUser({ status: "finished", now_playing: false });
|
||||
recordLaunch(makeRom(ru));
|
||||
|
||||
// Optimistic update is visible synchronously...
|
||||
expect(ru.now_playing).toBe(true);
|
||||
expect(ru.status).toBe("incomplete");
|
||||
|
||||
// ...then reverted once the rejected write settles.
|
||||
await vi.waitFor(() => {
|
||||
expect(ru.now_playing).toBe(false);
|
||||
expect(ru.status).toBe("finished");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
// icons. v2-only: v1 still reads `romStatusMap`'s emoji from
|
||||
// `@/utils`, so that map stays untouched.
|
||||
import type { RomUserSchema, RomUserStatus } from "@/__generated__";
|
||||
import romApi from "@/services/api/rom";
|
||||
import type { SimpleRom } from "@/stores/roms";
|
||||
import type { PlayingStatus } from "@/utils";
|
||||
|
||||
// Advance a ROM's per-user status for a launch. Mutates in place so the
|
||||
@@ -19,6 +21,27 @@ export function applyLaunchStatus(romUser: RomUserSchema): void {
|
||||
}
|
||||
}
|
||||
|
||||
// Persist a launch: optimistically apply the launch status and bump
|
||||
// last_played, reverting the local mutation if the write fails so client
|
||||
// state never drifts from the server. Fire-and-forget by design (the caller
|
||||
// is entering gameplay), but the rejection is handled rather than left to
|
||||
// surface as an unhandled promise.
|
||||
export function recordLaunch(rom: SimpleRom): void {
|
||||
const romUser = rom.rom_user;
|
||||
const before = { now_playing: romUser.now_playing, status: romUser.status };
|
||||
applyLaunchStatus(romUser);
|
||||
romApi
|
||||
.updateUserRomProps({
|
||||
romId: rom.id,
|
||||
data: romUser,
|
||||
updateLastPlayed: true,
|
||||
})
|
||||
.catch((err) => {
|
||||
Object.assign(romUser, before);
|
||||
console.error("Failed to record play status:", err);
|
||||
});
|
||||
}
|
||||
|
||||
// Icon map covering both the enum statuses and the orthogonal boolean
|
||||
// flags (now_playing / backlogged / hidden) so a single menu can present
|
||||
// them together.
|
||||
|
||||
@@ -50,7 +50,7 @@ import { useFullscreenPref } from "@/v2/composables/useFullscreenPref";
|
||||
import { useInputModality } from "@/v2/composables/useInputModality";
|
||||
import type { SliderBtnGroupItem } from "@/v2/lib/primitives/RSliderBtnGroup/types";
|
||||
import storeGalleryRoms from "@/v2/stores/galleryRoms";
|
||||
import { applyLaunchStatus } from "@/v2/utils/romStatus";
|
||||
import { recordLaunch } from "@/v2/utils/romStatus";
|
||||
import { installIOSFullscreenShim } from "@/views/Player/EmulatorJS/utils";
|
||||
|
||||
// Reuse v1's heavy emulator integration — do NOT rewrite this. Lazy so the
|
||||
@@ -226,15 +226,10 @@ async function onPlay() {
|
||||
removeIOSFullscreenShim.value?.();
|
||||
removeIOSFullscreenShim.value = installIOSFullscreenShim();
|
||||
|
||||
if (rom.value?.rom_user && auth.scopes.includes("roms.user.write")) {
|
||||
// Launching marks the game "now playing" and rewinds an empty/finished
|
||||
// status to "incomplete"; protected statuses are left untouched.
|
||||
applyLaunchStatus(rom.value.rom_user);
|
||||
romApi.updateUserRomProps({
|
||||
romId: rom.value.id,
|
||||
data: rom.value.rom_user,
|
||||
updateLastPlayed: true,
|
||||
});
|
||||
if (rom.value && auth.scopes.includes("roms.user.write")) {
|
||||
// Marks the game "now playing" and bumps last_played on launch; protected
|
||||
// statuses are left untouched and a failed write reverts locally.
|
||||
recordLaunch(rom.value);
|
||||
}
|
||||
|
||||
gameRunning.value = true;
|
||||
|
||||
@@ -26,7 +26,7 @@ import { useBackgroundArt } from "@/v2/composables/useBackgroundArt";
|
||||
import { useFullscreenPref } from "@/v2/composables/useFullscreenPref";
|
||||
import storeGalleryRoms from "@/v2/stores/galleryRoms";
|
||||
import { colorCanvas } from "@/v2/tokens";
|
||||
import { applyLaunchStatus } from "@/v2/utils/romStatus";
|
||||
import { recordLaunch } from "@/v2/utils/romStatus";
|
||||
|
||||
const RUFFLE_VERSION = "0.2.0-nightly.2025.8.14";
|
||||
const DEFAULT_BACKGROUND_COLOR = colorCanvas.bgDeep;
|
||||
@@ -147,13 +147,8 @@ function onPlay() {
|
||||
|
||||
// Record the launch only once playback is actually under way, so a
|
||||
// failed player creation / load doesn't leave the game marked playing.
|
||||
if (rom.value.rom_user && auth.scopes.includes("roms.user.write")) {
|
||||
applyLaunchStatus(rom.value.rom_user);
|
||||
romApi.updateUserRomProps({
|
||||
romId: rom.value.id,
|
||||
data: rom.value.rom_user,
|
||||
updateLastPlayed: true,
|
||||
});
|
||||
if (auth.scopes.includes("roms.user.write")) {
|
||||
recordLaunch(rom.value);
|
||||
}
|
||||
|
||||
if (player.fullscreenEnabled && fullscreenOnPlay.value) {
|
||||
|
||||
@@ -30,7 +30,7 @@ import GameCover from "@/v2/components/shared/GameCover.vue";
|
||||
import { useBackgroundArt } from "@/v2/composables/useBackgroundArt";
|
||||
import { useSnackbar } from "@/v2/composables/useSnackbar";
|
||||
import storeGalleryRoms from "@/v2/stores/galleryRoms";
|
||||
import { applyLaunchStatus } from "@/v2/utils/romStatus";
|
||||
import { recordLaunch } from "@/v2/utils/romStatus";
|
||||
|
||||
type PlayerState = "idle" | "loading" | "playing" | "error" | "exited";
|
||||
type ErrorType =
|
||||
@@ -287,15 +287,10 @@ async function handlePlay(): Promise<void> {
|
||||
containerHost.value = session.host;
|
||||
playerState.value = "playing";
|
||||
|
||||
// Mark the game "now playing" and refresh last_played on launch (protected
|
||||
// statuses are left untouched by applyLaunchStatus).
|
||||
if (rom.value.rom_user && auth.scopes.includes("roms.user.write")) {
|
||||
applyLaunchStatus(rom.value.rom_user);
|
||||
romApi.updateUserRomProps({
|
||||
romId: rom.value.id,
|
||||
data: rom.value.rom_user,
|
||||
updateLastPlayed: true,
|
||||
});
|
||||
// Mark the game "now playing" and refresh last_played on launch; protected
|
||||
// statuses are left untouched and a failed write reverts locally.
|
||||
if (auth.scopes.includes("roms.user.write")) {
|
||||
recordLaunch(rom.value);
|
||||
}
|
||||
|
||||
// Wait for the DOM to update and the iframe to exist.
|
||||
|
||||
Reference in New Issue
Block a user