Skip to content

Commit

Permalink
fix race conditions (#160)
Browse files Browse the repository at this point in the history
-
  • Loading branch information
nnyyxxxx committed Dec 8, 2024
1 parent b8efef5 commit 888db72
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
16 changes: 5 additions & 11 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<SearchResult>, std::io::Error>(Vec::new());
}

let results = match query.chars().next() {
Expand Down Expand Up @@ -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::<Vec<SearchResult>, 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::<Vec<SearchResult>, 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)]
Expand Down
33 changes: 18 additions & 15 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
);

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::<Vec<_>>(),
);
}
}
}
}
Expand Down

0 comments on commit 888db72

Please sign in to comment.