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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XGA3D2oZ1hq3QUdnubMNXh
This commit is contained in:
Sam Dornan
2026-10-27 08:42:44 +00:00
committed by Claude
parent c4ad36537f
commit 3b56d0cd6c
4 changed files with 29 additions and 9 deletions

View File

@@ -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):

View File

@@ -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.

View File

@@ -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();

View File

@@ -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) {