fix(ui): derive operation status from snapshots
The launcher was mixing lifecycle event handlers with the games-list snapshot when deciding the card status. That left multiple writers for the same install_status field and made event ordering visible in React. Make games-list-updated active_operations the authoritative source for busy status. Lifecycle events no longer mutate the card status; they only keep their non-status side effects such as rescans and error messages. The only remaining optimistic status is CheckingPeers before the backend emits its next snapshot. Add a frontend reducer test that proves an install stays in Installing while an active install snapshot exists, then settles to Installed only after the active operation clears with installed local state. Test Plan: - git diff --check - just fmt - just frontend-test - just build Refs: local install/download status snapshot cleanup
This commit is contained in:
@@ -26,11 +26,12 @@ Never use normal cargo ... commands, use the just ... commands instead.
|
|||||||
- `just fmt` — format the workspace.
|
- `just fmt` — format the workspace.
|
||||||
- `just clippy` — lint the workspace.
|
- `just clippy` — lint the workspace.
|
||||||
- `just test` — run the workspace unit tests.
|
- `just test` — run the workspace unit tests.
|
||||||
|
- `just frontend-test` — run frontend reducer/unit tests.
|
||||||
- `just fix` — auto-apply cargo/clippy fixes, then format.
|
- `just fix` — auto-apply cargo/clippy fixes, then format.
|
||||||
- `just clean` — wipe the build cache.
|
- `just clean` — wipe the build cache.
|
||||||
- `just peer-cli-build` — build the scripted peer harness.
|
- `just peer-cli-build` — build the scripted peer harness.
|
||||||
- `just peer-cli-image` — build the peer harness Docker image.
|
- `just peer-cli-image` — build the peer harness Docker image.
|
||||||
- `just peer-cli-run NAME` — run one named harness container with persistent state under `target/peer-cli/NAME/`.
|
- `just peer-cli-run NAME` — run one named harness container with persistent state under `.lanspread-peer-cli/NAME/`.
|
||||||
|
|
||||||
## Protocol policy
|
## Protocol policy
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,9 @@ export interface GameActions {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Thin wrappers over the backend `run_game` / `install_game` / `update_game`
|
* Thin wrappers over the backend `run_game` / `install_game` / `update_game`
|
||||||
* / `uninstall_game` / `remove_downloaded_game` commands. We mark peer-backed
|
* / `uninstall_game` / `remove_downloaded_game` commands. Peer-backed downloads
|
||||||
* downloads as "checking peers" and already-downloaded installs as "installing"
|
* are marked as "checking peers" until the backend emits an authoritative
|
||||||
* up-front so the UI doesn't have to wait for the first backend event.
|
* operation snapshot.
|
||||||
*/
|
*/
|
||||||
export const useGameActions = (games: UseGamesResult): GameActions => {
|
export const useGameActions = (games: UseGamesResult): GameActions => {
|
||||||
const play = useCallback(async (id: string) => {
|
const play = useCallback(async (id: string) => {
|
||||||
@@ -32,9 +32,7 @@ export const useGameActions = (games: UseGamesResult): GameActions => {
|
|||||||
if (!success) return;
|
if (!success) return;
|
||||||
|
|
||||||
const game = games.games.find(item => item.id === id);
|
const game = games.games.find(item => item.id === id);
|
||||||
if (game?.downloaded && !game.installed) {
|
if (!game?.downloaded) {
|
||||||
games.markInstalling(id);
|
|
||||||
} else {
|
|
||||||
games.markChecking(id);
|
games.markChecking(id);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import {
|
|||||||
Game,
|
Game,
|
||||||
GamesListPayload,
|
GamesListPayload,
|
||||||
InstallStatus,
|
InstallStatus,
|
||||||
StatusLevel,
|
|
||||||
} from '../lib/types';
|
} from '../lib/types';
|
||||||
import {
|
import {
|
||||||
activeStatusById,
|
activeStatusById,
|
||||||
@@ -17,36 +16,23 @@ import {
|
|||||||
|
|
||||||
interface PendingPatch {
|
interface PendingPatch {
|
||||||
install_status?: InstallStatus;
|
install_status?: InstallStatus;
|
||||||
downloaded?: boolean;
|
|
||||||
installed?: boolean;
|
|
||||||
local_version?: string | null;
|
|
||||||
status_message?: string;
|
|
||||||
status_level?: StatusLevel | undefined;
|
|
||||||
clearStatus?: boolean;
|
clearStatus?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
const applyPatch = (game: Game, patch: PendingPatch): Game => {
|
const applyPatch = (game: Game, patch: PendingPatch): Game => {
|
||||||
let next: Game = { ...game };
|
let next: Game = { ...game };
|
||||||
if (patch.install_status !== undefined) next.install_status = patch.install_status;
|
if (patch.install_status !== undefined) next.install_status = patch.install_status;
|
||||||
if (patch.downloaded !== undefined) next.downloaded = patch.downloaded;
|
|
||||||
if (patch.installed !== undefined) next.installed = patch.installed;
|
|
||||||
if (patch.local_version !== undefined) next.local_version = patch.local_version ?? undefined;
|
|
||||||
if (patch.clearStatus) {
|
if (patch.clearStatus) {
|
||||||
next.status_message = undefined;
|
next.status_message = undefined;
|
||||||
next.status_level = undefined;
|
next.status_level = undefined;
|
||||||
}
|
}
|
||||||
if (patch.status_message !== undefined) {
|
|
||||||
next.status_message = patch.status_message;
|
|
||||||
next.status_level = patch.status_level;
|
|
||||||
}
|
|
||||||
return next;
|
return next;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Owns the games list and reflects every backend event (download/install/
|
* Owns the games list and derives card status from backend snapshots. Returns
|
||||||
* uninstall/remove lifecycle, peer count) into local React state. Returns a
|
* a fire-and-forget `markChecking` helper so action calls can immediately show
|
||||||
* fire-and-forget `markChecking` helper so action calls can immediately show a
|
* a "Checking peers…" state until the next backend snapshot arrives.
|
||||||
* "Checking peers…" state until the backend emits the authoritative outcome.
|
|
||||||
*/
|
*/
|
||||||
export interface UseGamesResult {
|
export interface UseGamesResult {
|
||||||
games: Game[];
|
games: Game[];
|
||||||
@@ -54,7 +40,6 @@ export interface UseGamesResult {
|
|||||||
totalPeerCount: number;
|
totalPeerCount: number;
|
||||||
requestGames: () => Promise<void>;
|
requestGames: () => Promise<void>;
|
||||||
markChecking: (id: string) => void;
|
markChecking: (id: string) => void;
|
||||||
markInstalling: (id: string) => void;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
||||||
@@ -71,14 +56,6 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
));
|
));
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const markInstalling = useCallback((id: string) => {
|
|
||||||
setGames(prev => prev.map(item =>
|
|
||||||
item.id === id
|
|
||||||
? applyPatch(item, { install_status: InstallStatus.Installing, clearStatus: true })
|
|
||||||
: item,
|
|
||||||
));
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
const requestGames = useCallback(async () => {
|
const requestGames = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
await invoke('request_games');
|
await invoke('request_games');
|
||||||
@@ -91,10 +68,6 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
const unlisteners: UnlistenFn[] = [];
|
const unlisteners: UnlistenFn[] = [];
|
||||||
let cancelled = false;
|
let cancelled = false;
|
||||||
|
|
||||||
const updateById = (id: string, patch: PendingPatch) => {
|
|
||||||
setGames(prev => prev.map(item => item.id === id ? applyPatch(item, patch) : item));
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleErrorEvent = (
|
const handleErrorEvent = (
|
||||||
id: string,
|
id: string,
|
||||||
message: string,
|
message: string,
|
||||||
@@ -120,37 +93,16 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
event.payload as GamesListPayload | Game[],
|
event.payload as GamesListPayload | Game[],
|
||||||
);
|
);
|
||||||
const activeStatuses = activeStatusById(payload.active_operations);
|
const activeStatuses = activeStatusById(payload.active_operations);
|
||||||
const hasAuthoritative = payload.active_operations !== undefined;
|
|
||||||
setGames(prev => {
|
setGames(prev => {
|
||||||
const previousById = new Map(prev.map(item => [item.id, item]));
|
const previousById = new Map(prev.map(item => [item.id, item]));
|
||||||
return payload.games.map(game => mergeGameUpdate(
|
return payload.games.map(game => mergeGameUpdate(
|
||||||
game,
|
game,
|
||||||
previousById.get(game.id),
|
previousById.get(game.id),
|
||||||
activeStatuses.get(game.id),
|
activeStatuses.get(game.id),
|
||||||
hasAuthoritative,
|
|
||||||
));
|
));
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// 'game-download-pre' confirms peer metadata was found. The backend may still
|
|
||||||
// reject the download during majority validation (which now emits a terminal fail event),
|
|
||||||
// so keep showing CheckingPeers until 'game-download-begin' reports that transfer started.
|
|
||||||
unlisteners.push(await listen('game-download-pre', (e) => {
|
|
||||||
const id = e.payload as string;
|
|
||||||
updateById(id, { install_status: InstallStatus.CheckingPeers, clearStatus: true });
|
|
||||||
}));
|
|
||||||
|
|
||||||
// 'game-download-begin' signals consensus size validation has completed and file transfer has started.
|
|
||||||
unlisteners.push(await listen('game-download-begin', (e) => {
|
|
||||||
const id = e.payload as string;
|
|
||||||
updateById(id, { install_status: InstallStatus.Downloading, clearStatus: true });
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-download-finished', (e) => {
|
|
||||||
const id = e.payload as string;
|
|
||||||
updateById(id, { install_status: InstallStatus.Installing, clearStatus: true });
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-download-failed', (e) => {
|
unlisteners.push(await listen('game-download-failed', (e) => {
|
||||||
handleErrorEvent(e.payload as string, 'Download failed. Please try again.', {
|
handleErrorEvent(e.payload as string, 'Download failed. Please try again.', {
|
||||||
triggerRescan: true,
|
triggerRescan: true,
|
||||||
@@ -167,18 +119,7 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
handleErrorEvent(e.payload as string, 'No peers currently have this game.');
|
handleErrorEvent(e.payload as string, 'No peers currently have this game.');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
unlisteners.push(await listen('game-install-begin', (e) => {
|
unlisteners.push(await listen('game-install-finished', () => {
|
||||||
const id = e.payload as string;
|
|
||||||
updateById(id, { install_status: InstallStatus.Installing, clearStatus: true });
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-install-finished', (e) => {
|
|
||||||
const id = e.payload as string;
|
|
||||||
updateById(id, {
|
|
||||||
install_status: InstallStatus.Installed,
|
|
||||||
installed: true,
|
|
||||||
clearStatus: true,
|
|
||||||
});
|
|
||||||
rescanRef.current();
|
rescanRef.current();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -186,40 +127,11 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
handleErrorEvent(e.payload as string, 'Install failed. Please try again.');
|
handleErrorEvent(e.payload as string, 'Install failed. Please try again.');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
unlisteners.push(await listen('game-uninstall-begin', (e) => {
|
|
||||||
updateById(e.payload as string, {
|
|
||||||
install_status: InstallStatus.Uninstalling,
|
|
||||||
clearStatus: true,
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-uninstall-finished', (e) => {
|
|
||||||
updateById(e.payload as string, {
|
|
||||||
install_status: InstallStatus.NotInstalled,
|
|
||||||
installed: false,
|
|
||||||
clearStatus: true,
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-uninstall-failed', (e) => {
|
unlisteners.push(await listen('game-uninstall-failed', (e) => {
|
||||||
handleErrorEvent(e.payload as string, 'Uninstall failed. Please try again.');
|
handleErrorEvent(e.payload as string, 'Uninstall failed. Please try again.');
|
||||||
}));
|
}));
|
||||||
|
|
||||||
unlisteners.push(await listen('game-remove-download-begin', (e) => {
|
unlisteners.push(await listen('game-remove-download-finished', () => {
|
||||||
updateById(e.payload as string, {
|
|
||||||
install_status: InstallStatus.Removing,
|
|
||||||
clearStatus: true,
|
|
||||||
});
|
|
||||||
}));
|
|
||||||
|
|
||||||
unlisteners.push(await listen('game-remove-download-finished', (e) => {
|
|
||||||
updateById(e.payload as string, {
|
|
||||||
install_status: InstallStatus.NotInstalled,
|
|
||||||
downloaded: false,
|
|
||||||
installed: false,
|
|
||||||
local_version: null,
|
|
||||||
clearStatus: true,
|
|
||||||
});
|
|
||||||
rescanRef.current();
|
rescanRef.current();
|
||||||
}));
|
}));
|
||||||
|
|
||||||
@@ -257,6 +169,5 @@ export const useGames = (rescanGameDir: () => void): UseGamesResult => {
|
|||||||
totalPeerCount,
|
totalPeerCount,
|
||||||
requestGames,
|
requestGames,
|
||||||
markChecking,
|
markChecking,
|
||||||
markInstalling,
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -17,19 +17,9 @@ const IN_PROGRESS_INSTALL_STATUSES = new Set<InstallStatus>([
|
|||||||
InstallStatus.Removing,
|
InstallStatus.Removing,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const RECONCILED_OPERATION_STATUSES = new Set<InstallStatus>([
|
|
||||||
InstallStatus.Downloading,
|
|
||||||
InstallStatus.Installing,
|
|
||||||
InstallStatus.Uninstalling,
|
|
||||||
InstallStatus.Removing,
|
|
||||||
]);
|
|
||||||
|
|
||||||
export const isInProgress = (status: InstallStatus): boolean =>
|
export const isInProgress = (status: InstallStatus): boolean =>
|
||||||
IN_PROGRESS_INSTALL_STATUSES.has(status);
|
IN_PROGRESS_INSTALL_STATUSES.has(status);
|
||||||
|
|
||||||
const isReconciledOperationStatus = (status: InstallStatus): boolean =>
|
|
||||||
RECONCILED_OPERATION_STATUSES.has(status);
|
|
||||||
|
|
||||||
export const installStatusFromActiveOperation = (op: ActiveOperationKind): InstallStatus => {
|
export const installStatusFromActiveOperation = (op: ActiveOperationKind): InstallStatus => {
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case ActiveOperationKind.Downloading:
|
case ActiveOperationKind.Downloading:
|
||||||
@@ -52,35 +42,23 @@ export const normalizeGamesListPayload = (
|
|||||||
): GamesListPayload => Array.isArray(payload) ? { games: payload } : payload;
|
): GamesListPayload => Array.isArray(payload) ? { games: payload } : payload;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reconcile a freshly received backend snapshot of a game with our prior
|
* Reconcile a freshly received backend snapshot. Core operation status is
|
||||||
* locally-tracked install status. Keeps in-progress operations visible across
|
* derived only from the backend active-operation snapshot plus installed state.
|
||||||
* snapshots that don't yet reflect the running operation.
|
|
||||||
*/
|
*/
|
||||||
export const mergeGameUpdate = (
|
export const mergeGameUpdate = (
|
||||||
incoming: Game,
|
incoming: Game,
|
||||||
previous?: Game,
|
previous?: Game,
|
||||||
activeStatus?: InstallStatus,
|
activeStatus?: InstallStatus,
|
||||||
hasAuthoritativeSnapshot = false,
|
|
||||||
): Game => {
|
): Game => {
|
||||||
let installStatus = InstallStatus.NotInstalled;
|
const installStatus = activeStatus
|
||||||
if (activeStatus !== undefined) {
|
?? (incoming.installed ? InstallStatus.Installed : InstallStatus.NotInstalled);
|
||||||
installStatus = activeStatus;
|
|
||||||
} else if (incoming.installed) {
|
|
||||||
installStatus = InstallStatus.Installed;
|
|
||||||
} else if (
|
|
||||||
previous
|
|
||||||
&& isInProgress(previous.install_status)
|
|
||||||
&& (!hasAuthoritativeSnapshot || previous.install_status === InstallStatus.CheckingPeers)
|
|
||||||
) {
|
|
||||||
installStatus = previous.install_status;
|
|
||||||
}
|
|
||||||
|
|
||||||
const localStateChanged = previous !== undefined
|
const localStateChanged = previous !== undefined
|
||||||
&& (previous.installed !== incoming.installed || previous.downloaded !== incoming.downloaded);
|
&& (previous.installed !== incoming.installed || previous.downloaded !== incoming.downloaded);
|
||||||
const activeStateReconciled = hasAuthoritativeSnapshot
|
const statusChanged = previous !== undefined
|
||||||
&& (activeStatus !== undefined
|
&& previous.install_status !== installStatus;
|
||||||
|| (previous !== undefined && isReconciledOperationStatus(previous.install_status)));
|
const clearStatus = localStateChanged
|
||||||
const clearStatus = localStateChanged || activeStateReconciled;
|
|| (statusChanged && (activeStatus !== undefined || isInProgress(previous.install_status)));
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...incoming,
|
...incoming,
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import {
|
||||||
|
activeStatusById,
|
||||||
|
mergeGameUpdate,
|
||||||
|
} from '../src/lib/gameState.ts';
|
||||||
|
import {
|
||||||
|
ActiveOperationKind,
|
||||||
|
GameAvailability,
|
||||||
|
InstallStatus,
|
||||||
|
type Game,
|
||||||
|
} from '../src/lib/types.ts';
|
||||||
|
|
||||||
|
const assertEquals = <T>(actual: T, expected: T, message: string) => {
|
||||||
|
if (actual !== expected) {
|
||||||
|
throw new Error(`${message}: expected ${expected}, got ${actual}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const game = (overrides: Partial<Game> = {}): Game => ({
|
||||||
|
id: 'game',
|
||||||
|
name: 'Game',
|
||||||
|
description: '',
|
||||||
|
size: 0,
|
||||||
|
downloaded: false,
|
||||||
|
installed: false,
|
||||||
|
availability: GameAvailability.LocalOnly,
|
||||||
|
install_status: InstallStatus.NotInstalled,
|
||||||
|
peer_count: 1,
|
||||||
|
...overrides,
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test('snapshot keeps installing visible until installed state settles', () => {
|
||||||
|
const fromDownloading = game({
|
||||||
|
install_status: InstallStatus.Downloading,
|
||||||
|
});
|
||||||
|
const installing = mergeGameUpdate(
|
||||||
|
game({ downloaded: true }),
|
||||||
|
fromDownloading,
|
||||||
|
InstallStatus.Installing,
|
||||||
|
);
|
||||||
|
const installedWhileActive = mergeGameUpdate(
|
||||||
|
game({ downloaded: true, installed: true }),
|
||||||
|
installing,
|
||||||
|
InstallStatus.Installing,
|
||||||
|
);
|
||||||
|
const settled = mergeGameUpdate(
|
||||||
|
game({ downloaded: true, installed: true }),
|
||||||
|
installedWhileActive,
|
||||||
|
);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
installing.install_status,
|
||||||
|
InstallStatus.Installing,
|
||||||
|
'active install snapshot should render Installing',
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
installedWhileActive.install_status,
|
||||||
|
InstallStatus.Installing,
|
||||||
|
'installed local state should not override an active install snapshot',
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
settled.install_status,
|
||||||
|
InstallStatus.Installed,
|
||||||
|
'cleared active snapshot with installed local state should render Installed',
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
Deno.test('active operation snapshot is the source of busy status', () => {
|
||||||
|
const statuses = activeStatusById([
|
||||||
|
{ id: 'game', operation: ActiveOperationKind.Downloading },
|
||||||
|
{ id: 'other', operation: ActiveOperationKind.Updating },
|
||||||
|
]);
|
||||||
|
|
||||||
|
assertEquals(
|
||||||
|
statuses.get('game'),
|
||||||
|
InstallStatus.Downloading,
|
||||||
|
'download operation should render Downloading',
|
||||||
|
);
|
||||||
|
assertEquals(
|
||||||
|
statuses.get('other'),
|
||||||
|
InstallStatus.Installing,
|
||||||
|
'update operation should render Installing',
|
||||||
|
);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user