From f1c20bd631c1879a6342d738600352ed3b0a77bf Mon Sep 17 00:00:00 2001 From: Wojciech Kozlowski Date: Sun, 6 Oct 2024 16:11:59 +0200 Subject: [PATCH] Some rearrangements --- src/tui/app/machine/fetch_state.rs | 61 ++++++++++++++++++------------ src/tui/app/machine/match_state.rs | 10 ++--- src/tui/app/machine/mod.rs | 4 +- 3 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 746f8b0..0a23524 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -22,28 +22,41 @@ use crate::tui::{ pub type MbApiReceiver = mpsc::Receiver; pub struct FetchState { - search_rx: MbApiReceiver, + search_rx: Option, lookup_rx: Option, + fetch_rx: Option, +} + +macro_rules! try_recv_mb_api_receiver { + ($rx:expr) => { + if let Some(rx) = &($rx) { + match rx.try_recv() { + x @ Ok(_) | x @ Err(TryRecvError::Empty) => return x, + Err(TryRecvError::Disconnected) => { + ($rx).take(); + } + } + } + }; } impl FetchState { - pub fn new(search_rx: MbApiReceiver) -> Self { + pub fn search(search_rx: MbApiReceiver) -> Self { FetchState { - search_rx, + search_rx: Some(search_rx), lookup_rx: None, + fetch_rx: None, } } fn try_recv(&mut self) -> Result { - if let Some(lookup_rx) = &self.lookup_rx { - match lookup_rx.try_recv() { - x @ Ok(_) | x @ Err(TryRecvError::Empty) => return x, - Err(TryRecvError::Disconnected) => { - self.lookup_rx.take(); - } - } + try_recv_mb_api_receiver!(self.lookup_rx); + try_recv_mb_api_receiver!(self.search_rx); + + match &self.fetch_rx { + Some(fetch_rx) => fetch_rx.try_recv(), + None => Err(TryRecvError::Disconnected), } - self.search_rx.try_recv() } } @@ -79,7 +92,7 @@ impl AppMachine { }; let (fetch_tx, fetch_rx) = mpsc::channel::(); - let fetch = FetchState::new(fetch_rx); + let fetch = FetchState::search(fetch_rx); let mb = &*inner.musicbrainz; let result = match inner.selection.category() { @@ -203,10 +216,10 @@ impl AppMachine { artist_mbid: &MbArtistRef, album: &Album, ) -> Result<(), FetchError> { - if !matches!(album.meta.info.musicbrainz, MbRefOption::None) { + let requests = Self::search_albums_requests(artist_id, artist_mbid, slice::from_ref(album)); + if requests.is_empty() { return Err(FetchError::NothingToFetch); } - let requests = Self::search_albums_requests(artist_id, artist_mbid, slice::from_ref(album)); Ok(musicbrainz.submit_background_job(result_sender, requests)?) } @@ -312,7 +325,7 @@ mod tests { let (fetch_tx, fetch_rx) = mpsc::channel(); let (lookup_tx, lookup_rx) = mpsc::channel(); - let mut fetch = FetchState::new(fetch_rx); + let mut fetch = FetchState::search(fetch_rx); fetch.lookup_rx.replace(lookup_rx); let artist = COLLECTION[3].meta.clone(); @@ -494,7 +507,7 @@ mod tests { let inner = AppInner::new(music_hoard, mb_job_sender); let (_fetch_tx, fetch_rx) = mpsc::channel(); - let fetch = FetchState::new(fetch_rx); + let fetch = FetchState::search(fetch_rx); AppMachine::app_lookup_album(inner, fetch, &artist_id, &album_id, mbid()); } @@ -548,7 +561,7 @@ mod tests { let inner = AppInner::new(music_hoard, mb_job_sender); let (_fetch_tx, fetch_rx) = mpsc::channel(); - let fetch = FetchState::new(fetch_rx); + let fetch = FetchState::search(fetch_rx); AppMachine::app_lookup_artist(inner, fetch, &artist, mbid()); } @@ -597,7 +610,7 @@ mod tests { let inner = AppInner::new(music_hoard, mb_job_sender); let (_fetch_tx, fetch_rx) = mpsc::channel(); - let fetch = FetchState::new(fetch_rx); + let fetch = FetchState::search(fetch_rx); let app = AppMachine::app_lookup_artist(inner, fetch, &artist, mbid()); assert!(matches!(app, AppState::Error(_))); @@ -615,7 +628,7 @@ mod tests { tx.send(fetch_result).unwrap(); let inner = inner(music_hoard(COLLECTION.clone())); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let mut app = AppMachine::app_fetch_next(inner, fetch); assert!(matches!(app, AppState::Match(_))); @@ -638,7 +651,7 @@ mod tests { tx.send(fetch_result).unwrap(); let inner = inner(music_hoard(COLLECTION.clone())); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let app = AppMachine::app_fetch_next(inner, fetch); assert!(matches!(app, AppState::Error(_))); } @@ -648,7 +661,7 @@ mod tests { let (_tx, rx) = mpsc::channel::(); let inner = inner(music_hoard(COLLECTION.clone())); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let app = AppMachine::app_fetch_next(inner, fetch); assert!(matches!(app, AppState::Fetch(_))); } @@ -668,7 +681,7 @@ mod tests { collection[0].albums.clear(); let (_, rx) = mpsc::channel::(); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let app = AppMachine::app_fetch_next(inner(music_hoard(collection)), fetch); assert!(matches!(app, AppState::Browse(_))); @@ -679,7 +692,7 @@ mod tests { let (tx, rx) = mpsc::channel::(); let inner = inner(music_hoard(COLLECTION.clone())); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let app = AppMachine::app_fetch_next(inner, fetch); assert!(matches!(app, AppState::Fetch(_))); @@ -696,7 +709,7 @@ mod tests { fn abort() { let (_, rx) = mpsc::channel::(); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let app = AppMachine::fetch_state(inner(music_hoard(COLLECTION.clone())), fetch); let app = app.abort(); diff --git a/src/tui/app/machine/match_state.rs b/src/tui/app/machine/match_state.rs index 5b453a6..8a28fd4 100644 --- a/src/tui/app/machine/match_state.rs +++ b/src/tui/app/machine/match_state.rs @@ -357,7 +357,7 @@ mod tests { fn fetch_state() -> FetchState { let (_, rx) = mpsc::channel(); - FetchState::new(rx) + FetchState::search(rx) } fn match_state(match_state_info: EntityMatches) -> MatchState { @@ -388,7 +388,7 @@ mod tests { fn match_state_flow(mut matches_info: EntityMatches, len: usize) { // tx must exist for rx to return Empty rather than Disconnected. let (_tx, rx) = mpsc::channel(); - let app_matches = MatchState::new(matches_info.clone(), FetchState::new(rx)); + let app_matches = MatchState::new(matches_info.clone(), FetchState::search(rx)); let mut music_hoard = music_hoard(vec![]); let artist_id = ArtistId::new("Artist"); @@ -487,7 +487,7 @@ mod tests { let matches_info = artist_match(); let (_tx, rx) = mpsc::channel(); - let app_matches = MatchState::new(matches_info.clone(), FetchState::new(rx)); + let app_matches = MatchState::new(matches_info.clone(), FetchState::search(rx)); let mut music_hoard = music_hoard(vec![]); match matches_info { @@ -511,7 +511,7 @@ mod tests { let matches_info = album_match(); let (_tx, rx) = mpsc::channel(); - let app_matches = MatchState::new(matches_info.clone(), FetchState::new(rx)); + let app_matches = MatchState::new(matches_info.clone(), FetchState::search(rx)); let mut music_hoard = music_hoard(vec![]); match matches_info { @@ -535,7 +535,7 @@ mod tests { let matches_info = artist_match(); let (_tx, rx) = mpsc::channel(); - let app_matches = MatchState::new(matches_info.clone(), FetchState::new(rx)); + let app_matches = MatchState::new(matches_info.clone(), FetchState::search(rx)); let mut music_hoard = music_hoard(vec![]); match matches_info { diff --git a/src/tui/app/machine/mod.rs b/src/tui/app/machine/mod.rs index 660f922..19f930c 100644 --- a/src/tui/app/machine/mod.rs +++ b/src/tui/app/machine/mod.rs @@ -493,7 +493,7 @@ mod tests { let (_, rx) = mpsc::channel(); let inner = app.unwrap_browse().inner; - let state = FetchState::new(rx); + let state = FetchState::search(rx); app = AppMachine::new(inner, state).into(); let state = app.state(); @@ -518,7 +518,7 @@ mod tests { assert!(app.is_running()); let (_, rx) = mpsc::channel(); - let fetch = FetchState::new(rx); + let fetch = FetchState::search(rx); let artist = ArtistMeta::new(ArtistId::new("Artist")); let info = EntityMatches::artist_lookup(artist.clone(), Entity::new(artist.clone())); app =