From 3b56d0cd6cd07f09bfc5342d1bbe313d38649236 Mon Sep 17 00:00:00 2001 From: Sam Dornan Date: Tue, 27 Oct 2026 08:42:44 +0000 Subject: [PATCH] fix(steam): keep the newest owned list and trim docstrings A slow earlier request for the owned Steam apps could overwrite a newer reply, so only the latest request now writes. Also trims two docstrings to the one-sentence rule. Addresses Greptile's second review. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01XGA3D2oZ1hq3QUdnubMNXh --- .../handler/database/steam_library_handler.py | 5 +--- backend/handler/steam_library_handler.py | 4 ---- frontend/src/stores/steamLibrary.test.ts | 23 +++++++++++++++++++ frontend/src/stores/steamLibrary.ts | 6 ++++- 4 files changed, 29 insertions(+), 9 deletions(-) diff --git a/backend/handler/database/steam_library_handler.py b/backend/handler/database/steam_library_handler.py index 035e516b2..998c6860a 100644 --- a/backend/handler/database/steam_library_handler.py +++ b/backend/handler/database/steam_library_handler.py @@ -30,10 +30,7 @@ class DBSteamLibraryHandler(DBBaseHandler): steam_app_ids: Sequence[int], session: Session = INJECTED_SESSION, ) -> dict[int, int]: - """The rom that stands for each Steam app on the given platforms. - - A rom with a file wins over a store-only one, then the oldest row. - """ + """Each Steam app's rom on the given platforms, preferring one with a file.""" rows: list[tuple[int | None, int, str | None]] = [] ids = list(steam_app_ids) for start in range(0, len(ids), STEAM_ID_CHUNK_SIZE): diff --git a/backend/handler/steam_library_handler.py b/backend/handler/steam_library_handler.py index 58e725360..7cf1af03e 100644 --- a/backend/handler/steam_library_handler.py +++ b/backend/handler/steam_library_handler.py @@ -192,10 +192,6 @@ def _last_played(game: SteamOwnedGame) -> datetime | None: async def sync_steam_library(user: User) -> SteamSyncResult: """Make the user's Steam library entries match what their account owns. - Owned games already in the library are linked to their rom; the rest get a - store-only rom. Store-only roms still unmatched are listed for a metadata - scan, since a library scan never reaches a game without a file. - Raises: SteamLibraryError: The account isn't linked, Steam can't list its games, or the account changed during the sync. diff --git a/frontend/src/stores/steamLibrary.test.ts b/frontend/src/stores/steamLibrary.test.ts index 24f0669b9..c552eacac 100644 --- a/frontend/src/stores/steamLibrary.test.ts +++ b/frontend/src/stores/steamLibrary.test.ts @@ -56,6 +56,29 @@ describe("steamLibrary", () => { expect(library.owns(400)).toBe(true); }); + it("keeps the newest reply when an older one arrives late", async () => { + let answerFirst: (value: unknown) => void = () => {}; + vi.mocked(userApi.fetchSteamLibrary) + .mockReturnValueOnce( + new Promise((resolve) => { + answerFirst = resolve; + }) as never, + ) + .mockResolvedValueOnce({ data: [400] } as never); + storeAuth().setCurrentUser( + userFixture({ id: 1, steam_id: "76561197960287930" }), + ); + library = useSteamLibrary(); + void library.load(); + await flushPromises(); + + answerFirst({ data: [220] }); + await flushPromises(); + + expect(library.owns(400)).toBe(true); + expect(library.owns(220)).toBe(false); + }); + it("loads nothing without a linked account", async () => { storeAuth().setCurrentUser(userFixture({ id: 1 })); library = useSteamLibrary(); diff --git a/frontend/src/stores/steamLibrary.ts b/frontend/src/stores/steamLibrary.ts index 8bafd80ab..c0097558e 100644 --- a/frontend/src/stores/steamLibrary.ts +++ b/frontend/src/stores/steamLibrary.ts @@ -27,13 +27,17 @@ const useSteamLibrary = defineStore("steamLibrary", () => { return ownedAppIds.value.has(appId); } + // Only the newest request may write, so a slow earlier reply can't win. + let latestRequest = 0; + async function load() { const user = auth.user; const requested = account.value; if (!user || !requested) return; + const request = ++latestRequest; try { const { data } = await userApi.fetchSteamLibrary({ id: user.id }); - if (account.value !== requested) return; + if (request !== latestRequest || account.value !== requested) return; ownedAppIds.value = new Set(data); loadedFor.value = requested; } catch (error) {