From 888db721e3ba329f66c337f55a6cd281ad91ff74 Mon Sep 17 00:00:00 2001 From: nyx Date: Sun, 8 Dec 2024 03:35:02 -0500 Subject: [PATCH] fix race conditions (#160) - --- src/search.rs | 16 +++++----------- src/ui.rs | 33 ++++++++++++++++++--------------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/src/search.rs b/src/search.rs index 7a9c491..6cae966 100644 --- a/src/search.rs +++ b/src/search.rs @@ -88,10 +88,7 @@ pub async fn search_applications( let cache = APP_CACHE.blocking_read(); if SEARCH_GENERATION.load(Ordering::SeqCst) != current_gen + 1 { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Search superseded", - )); + return Ok::, std::io::Error>(Vec::new()); } let results = match query.chars().next() { @@ -262,18 +259,15 @@ pub async fn search_applications( }; if SEARCH_GENERATION.load(Ordering::SeqCst) != current_gen + 1 { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Search superseded", - )); + return Ok::, std::io::Error>(Vec::new()); } - tx.send(results) - .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Failed to send results")) + tx.send(results).ok(); + Ok::, std::io::Error>(Vec::new()) }); rx.await - .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Failed to receive results")) + .map_err(|_| std::io::Error::new(std::io::ErrorKind::Other, "Search cancelled")) } #[inline(always)] diff --git a/src/ui.rs b/src/ui.rs index 387f990..0745fd1 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -798,21 +798,24 @@ fn update_results_list( model.remove_all(); store.clear(); - store.reserve(max_entries); - - let results = if results.len() > max_entries { - &results[..max_entries] - } else { - &results - }; - - store.extend(results.iter().map(|r| r.app.clone())); - model.extend_from_slice( - &results - .iter() - .map(|r| AppEntryObject::new(r.app.clone())) - .collect::>(), - ); + + if !results.is_empty() { + store.reserve(max_entries); + + let results = if results.len() > max_entries { + &results[..max_entries] + } else { + &results + }; + + store.extend(results.iter().map(|r| r.app.clone())); + model.extend_from_slice( + &results + .iter() + .map(|r| AppEntryObject::new(r.app.clone())) + .collect::>(), + ); + } } } }