refactor(peer): split local library and operation UI events
Replace the `a9f9845` local-update dedup cache with explicit peer event semantics. Local scans now emit `LocalLibraryChanged` when the library changes, while operation mutations emit `ActiveOperationsChanged` from the mutation path. Tauri keeps joining those facts into the existing `games-list-updated` payload, so the frontend contract stays stable. This removes the cache/invalidation coupling between scan emission and operation state. The remaining forced local snapshot is explicit: accepted game directory changes can refresh the UI for an equivalent new path without sending a peer library delta. Operation guard cleanup and liveness cancellation now publish the same active operation snapshot as normal command-handler transitions. The peer CLI JSONL events follow the same split with `local-library-changed` and `active-operations-changed`. Test Plan: - `just fmt` - `CARGO_BUILD_RUSTC_WRAPPER= just test` - `CARGO_BUILD_RUSTC_WRAPPER= just clippy` - `git diff --check` Refs: CLEAN_CODE_PLAN_1.md
This commit is contained in:
@@ -13,7 +13,6 @@ use lanspread_db::db::{Availability, Game, GameDB, GameFileDescription};
|
||||
use lanspread_peer::{
|
||||
ActiveOperation,
|
||||
ActiveOperationKind,
|
||||
InstallOperation,
|
||||
PeerCommand,
|
||||
PeerEvent,
|
||||
PeerGameDB,
|
||||
@@ -747,17 +746,18 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
log::info!("PeerEvent::ListGames received");
|
||||
update_game_db(games, app_handle.clone()).await;
|
||||
}
|
||||
PeerEvent::LocalGamesUpdated {
|
||||
games: local_games,
|
||||
active_operations,
|
||||
} => {
|
||||
log::info!("PeerEvent::LocalGamesUpdated received");
|
||||
PeerEvent::LocalLibraryChanged { games: local_games } => {
|
||||
log::info!("PeerEvent::LocalLibraryChanged received");
|
||||
update_local_games_in_db(local_games, app_handle.clone()).await;
|
||||
}
|
||||
PeerEvent::ActiveOperationsChanged { active_operations } => {
|
||||
log::info!("PeerEvent::ActiveOperationsChanged received");
|
||||
let state = app_handle.state::<LanSpreadState>();
|
||||
{
|
||||
let state = app_handle.state::<LanSpreadState>();
|
||||
let mut ui_active_operations = state.active_operations.write().await;
|
||||
reconcile_active_operations(&mut ui_active_operations, &active_operations);
|
||||
}
|
||||
update_local_games_in_db(local_games, app_handle.clone()).await;
|
||||
emit_games_list(app_handle).await;
|
||||
}
|
||||
PeerEvent::GotGameFiles {
|
||||
id,
|
||||
@@ -773,21 +773,9 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
&id,
|
||||
"PeerEvent::NoPeersHaveGame",
|
||||
);
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
}
|
||||
PeerEvent::DownloadGameFilesBegin { id } => {
|
||||
log::info!("PeerEvent::DownloadGameFilesBegin received");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.insert(id.clone(), UiOperationKind::Downloading);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-download-begin",
|
||||
@@ -808,7 +796,7 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
);
|
||||
}
|
||||
PeerEvent::DownloadGameFilesFinished { id } => {
|
||||
handle_download_finished(app_handle, id).await;
|
||||
handle_download_finished(app_handle, id);
|
||||
}
|
||||
PeerEvent::DownloadGameFilesFailed { id } => {
|
||||
log::warn!("PeerEvent::DownloadGameFilesFailed received");
|
||||
@@ -818,12 +806,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
&id,
|
||||
"PeerEvent::DownloadGameFilesFailed",
|
||||
);
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
}
|
||||
PeerEvent::DownloadGameFilesAllPeersGone { id } => {
|
||||
log::warn!("PeerEvent::DownloadGameFilesAllPeersGone received for {id}");
|
||||
@@ -833,26 +815,10 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
&id,
|
||||
"PeerEvent::DownloadGameFilesAllPeersGone",
|
||||
);
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
}
|
||||
PeerEvent::InstallGameBegin { id, operation } => {
|
||||
let operation_name: &'static str = (&operation).into();
|
||||
log::info!("PeerEvent::InstallGameBegin received for {id}: {operation_name}");
|
||||
let ui_operation = match operation {
|
||||
InstallOperation::Installing => UiOperationKind::Installing,
|
||||
InstallOperation::Updating => UiOperationKind::Updating,
|
||||
};
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.insert(id.clone(), ui_operation);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-install-begin",
|
||||
@@ -862,12 +828,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
}
|
||||
PeerEvent::InstallGameFinished { id } => {
|
||||
log::info!("PeerEvent::InstallGameFinished received for {id}");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-install-finished",
|
||||
@@ -877,12 +837,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
}
|
||||
PeerEvent::InstallGameFailed { id } => {
|
||||
log::warn!("PeerEvent::InstallGameFailed received for {id}");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-install-failed",
|
||||
@@ -892,12 +846,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
}
|
||||
PeerEvent::UninstallGameBegin { id } => {
|
||||
log::info!("PeerEvent::UninstallGameBegin received for {id}");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.insert(id.clone(), UiOperationKind::Uninstalling);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-uninstall-begin",
|
||||
@@ -907,12 +855,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
}
|
||||
PeerEvent::UninstallGameFinished { id } => {
|
||||
log::info!("PeerEvent::UninstallGameFinished received for {id}");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-uninstall-finished",
|
||||
@@ -922,12 +864,6 @@ async fn handle_peer_event(app_handle: &AppHandle, event: PeerEvent) {
|
||||
}
|
||||
PeerEvent::UninstallGameFailed { id } => {
|
||||
log::warn!("PeerEvent::UninstallGameFailed received for {id}");
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
"game-uninstall-failed",
|
||||
@@ -995,7 +931,7 @@ async fn handle_got_game_files(
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_download_finished(app_handle: &AppHandle, id: String) {
|
||||
fn handle_download_finished(app_handle: &AppHandle, id: String) {
|
||||
log::info!("PeerEvent::DownloadGameFilesFinished received");
|
||||
emit_game_id_event(
|
||||
app_handle,
|
||||
@@ -1003,13 +939,6 @@ async fn handle_download_finished(app_handle: &AppHandle, id: String) {
|
||||
&id,
|
||||
"PeerEvent::DownloadGameFilesFinished",
|
||||
);
|
||||
|
||||
app_handle
|
||||
.state::<LanSpreadState>()
|
||||
.active_operations
|
||||
.write()
|
||||
.await
|
||||
.remove(&id);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user