fix(discovery): publish cold results after their icons are ready
This commit is contained in:
1 parent
9932de1c73
commit
a529a4cb35
6 files changed
+177
-45
No files matched your search
+2
-10
@@ -104,22 +104,14 @@ impl App {
|
||||
if self.focused == FocusedArea::Content {
|
||||
orphan_cleanup = orphan_cleanup.or_else(|| discovery.take_orphan_cleanup());
|
||||
}
|
||||
discovery.list.drain_pending();
|
||||
discovery.list.request_image_loads(&self.picker);
|
||||
discovery.list.drain_image_loads(&self.picker);
|
||||
discovery.drain_list(&self.picker);
|
||||
}
|
||||
self.datapacks_discovery_state.drain_pending();
|
||||
if self.focused == FocusedArea::Content {
|
||||
orphan_cleanup =
|
||||
orphan_cleanup.or_else(|| self.datapacks_discovery_state.take_orphan_cleanup());
|
||||
}
|
||||
self.datapacks_discovery_state.list.drain_pending();
|
||||
self.datapacks_discovery_state
|
||||
.list
|
||||
.request_image_loads(&self.picker);
|
||||
self.datapacks_discovery_state
|
||||
.list
|
||||
.drain_image_loads(&self.picker);
|
||||
self.datapacks_discovery_state.drain_list(&self.picker);
|
||||
if let Some(popup) = self.datapacks_discovery_state.version_popup.as_mut() {
|
||||
popup.worlds.request_image_loads(&self.picker);
|
||||
popup.worlds.drain_image_loads(&self.picker);
|
||||
|
||||
@@ -1712,13 +1712,22 @@ fn search_refresh_keeps_rows_until_the_diff_arrives() {
|
||||
assert!(!state.list.loading);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn filtered_refresh_applies_a_provider_page_in_one_frame() {
|
||||
#[tokio::test]
|
||||
async fn filtered_refresh_waits_for_finished_icons_before_switching_rows() {
|
||||
let mut state = DiscoveryState::new(ContentKind::Mod);
|
||||
let picker = ratatui_image::picker::Picker::halfblocks();
|
||||
let mut png = std::io::Cursor::new(Vec::new());
|
||||
image::DynamicImage::new_rgba8(1, 1)
|
||||
.write_to(&mut png, image::ImageFormat::Png)
|
||||
.unwrap();
|
||||
let instance = instance("one", "1.21.1");
|
||||
let initial = state.begin_search(&instance);
|
||||
initial.stream.upsert(project_entry(project("old"), None));
|
||||
initial
|
||||
.stream
|
||||
.upsert(project_entry(project("another"), None));
|
||||
drain_discovery_rows(&mut state);
|
||||
state.list.list_state.selected = Some(1);
|
||||
|
||||
state.filter_panel_selected = state.category_start();
|
||||
state.apply_selected_filter();
|
||||
@@ -1728,20 +1737,49 @@ fn filtered_refresh_applies_a_provider_page_in_one_frame() {
|
||||
let stems = (0..8)
|
||||
.map(|index| {
|
||||
let mut project = project(&format!("new-{index}"));
|
||||
project.icon_bytes = Some(vec![index as u8]);
|
||||
if index == 7 {
|
||||
project.icon_url = Some("https://example.invalid/icon".to_owned());
|
||||
} else {
|
||||
project.icon_bytes = Some(png.get_ref().clone());
|
||||
}
|
||||
let stem = project.id.clone();
|
||||
refresh.stream.upsert(project_entry(project, None));
|
||||
stem
|
||||
})
|
||||
.collect();
|
||||
state.list.drain_pending();
|
||||
assert_eq!(state.list.entries.len(), 1);
|
||||
state.drain_list(&picker);
|
||||
assert_eq!(state.list.entries.len(), 2);
|
||||
assert_eq!(state.list.entries[0].name, "old");
|
||||
refresh.stream.order(stems);
|
||||
state.list.drain_pending();
|
||||
|
||||
DiscoveryState::push_result(
|
||||
&refresh.pending,
|
||||
refresh.generation,
|
||||
0,
|
||||
Ok(DiscoveryPageResult {
|
||||
received: 8,
|
||||
total_hits: 8,
|
||||
}),
|
||||
);
|
||||
state.drain_pending();
|
||||
state.drain_list(&picker);
|
||||
assert_eq!(state.list.entries[0].name, "old");
|
||||
assert!(state.preparing_list.as_ref().unwrap().has_pending_icons());
|
||||
refresh.stream.send_icon_unavailable(
|
||||
"new-7".to_owned(),
|
||||
"new-7".into(),
|
||||
Some(("modrinth".to_owned(), "new-7".to_owned())),
|
||||
);
|
||||
tokio::time::timeout(std::time::Duration::from_secs(3), async {
|
||||
while state.preparing_list.is_some() {
|
||||
state.drain_list(&picker);
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(state.list.entries.len(), 8);
|
||||
assert!(state.list.has_pending_icons());
|
||||
assert_eq!(state.list.list_state.selected, Some(1));
|
||||
assert!(!state.list.has_pending_icons());
|
||||
assert_eq!(state.list.filtered_indices().len(), 8);
|
||||
assert!(
|
||||
state
|
||||
@@ -1790,6 +1828,7 @@ fn discovery_activity_tracks_search_pages_and_icon_loading() {
|
||||
#[test]
|
||||
fn rapidly_cycling_a_category_discards_superseded_rows_and_results() {
|
||||
let mut state = DiscoveryState::new(ContentKind::Mod);
|
||||
let picker = ratatui_image::picker::Picker::halfblocks();
|
||||
let instance = instance("one", "1.21.1");
|
||||
let first = state.begin_search(&instance);
|
||||
assert!(
|
||||
@@ -1826,7 +1865,7 @@ fn rapidly_cycling_a_category_discards_superseded_rows_and_results() {
|
||||
include
|
||||
.stream
|
||||
.upsert(project_entry(project("wrong-filter"), None));
|
||||
state.list.drain_pending();
|
||||
state.drain_list(&picker);
|
||||
assert_eq!(state.list.entries[0].name, "original");
|
||||
state.apply_selected_filter(); // exclude before include returns
|
||||
assert!(
|
||||
@@ -1843,7 +1882,17 @@ fn rapidly_cycling_a_category_discards_superseded_rows_and_results() {
|
||||
.stream
|
||||
.upsert(project_entry(project("excluded"), None));
|
||||
exclude.stream.order(vec!["excluded".to_owned()]);
|
||||
state.list.drain_pending();
|
||||
DiscoveryState::push_result(
|
||||
&exclude.pending,
|
||||
exclude.generation,
|
||||
0,
|
||||
Ok(DiscoveryPageResult {
|
||||
received: 1,
|
||||
total_hits: 1,
|
||||
}),
|
||||
);
|
||||
state.drain_pending();
|
||||
state.drain_list(&picker);
|
||||
assert_eq!(state.list.entries[0].name, "excluded");
|
||||
}
|
||||
|
||||
|
||||
@@ -839,6 +839,46 @@ fn source_refresh_reuses_a_decoded_icon_only_for_the_same_provider() {
|
||||
assert!(!state.requested_images.contains("alpha"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn discovery_icon_decode_survives_installed_version_binding() {
|
||||
let mut state = ContentListState::default();
|
||||
let stream = state.start_source_stream("discovery");
|
||||
let mut project = entry("Alpha");
|
||||
project.icon_bytes = Some(vec![1]);
|
||||
project.provider_project = Some(crate::instance::ProviderProject {
|
||||
provider: "modrinth".to_owned(),
|
||||
project_id: "123".to_owned(),
|
||||
version_id: String::new(),
|
||||
});
|
||||
stream.upsert(project.clone());
|
||||
state.drain_pending();
|
||||
state.entries[0]
|
||||
.provider_project
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.version_id = "installed".to_owned();
|
||||
let mut icon_lines = crate::instance::content::fallback_icon();
|
||||
icon_lines[0][0].symbol = 'X';
|
||||
state
|
||||
.pending_images
|
||||
.lock()
|
||||
.unwrap()
|
||||
.push(PendingContentImage {
|
||||
file_stem: project.file_stem,
|
||||
path: project.path,
|
||||
source: project.provider_project,
|
||||
icon_bytes: vec![1],
|
||||
icon_lines,
|
||||
image: None,
|
||||
});
|
||||
state.drain_image_loads(&ratatui_image::picker::Picker::halfblocks());
|
||||
assert!(!state.has_pending_icons());
|
||||
assert_eq!(
|
||||
state.entries[0].icon_lines.as_ref().unwrap()[0][0].symbol,
|
||||
'X'
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn provider_icons_are_requested_only_for_visible_missing_icons() {
|
||||
let mut state = ContentListState::default();
|
||||
|
||||
@@ -914,6 +914,7 @@ pub struct DiscoveryState {
|
||||
pub kind: ContentKind,
|
||||
pub modpacks: bool,
|
||||
pub list: ContentListState,
|
||||
preparing_list: Option<ContentListState>,
|
||||
pub search: crate::tui::widgets::search::SearchState,
|
||||
pub sort: crate::instance::content::provider::DiscoverySort,
|
||||
pub sort_reversed: bool,
|
||||
@@ -960,7 +961,6 @@ pub struct DiscoveryState {
|
||||
retry_page_at: Option<std::time::Instant>,
|
||||
page_retry_attempt: u32,
|
||||
active_search_key: Option<DiscoverySearchKey>,
|
||||
// ponytail: keep four searches in memory; persist them only if cold-start discovery is too slow.
|
||||
cached_searches: Vec<(DiscoverySearchKey, CachedDiscoverySearch)>,
|
||||
}
|
||||
|
||||
@@ -1132,6 +1132,7 @@ impl DiscoveryState {
|
||||
kind,
|
||||
modpacks: false,
|
||||
list: ContentListState::default(),
|
||||
preparing_list: None,
|
||||
search: crate::tui::widgets::search::SearchState::default(),
|
||||
sort: Default::default(),
|
||||
sort_reversed: false,
|
||||
@@ -1335,13 +1336,17 @@ impl DiscoveryState {
|
||||
|
||||
pub fn set_unavailable(&mut self, instance: &InstanceConfig) {
|
||||
let context = discovery_context(instance);
|
||||
if self.context.as_deref() == Some(&context) && self.list.entries.is_empty() {
|
||||
if self.context.as_deref() == Some(&context)
|
||||
&& self.list.entries.is_empty()
|
||||
&& self.preparing_list.is_none()
|
||||
{
|
||||
return;
|
||||
}
|
||||
self.context = None;
|
||||
self.active_search_key = None;
|
||||
self.cached_searches.clear();
|
||||
drop(self.begin_search(instance));
|
||||
self.preparing_list = None;
|
||||
self.stream = None;
|
||||
self.page_loading = false;
|
||||
self.exhausted = true;
|
||||
@@ -1385,6 +1390,7 @@ impl DiscoveryState {
|
||||
let changed = self.active_search_key.as_ref() != Some(&key);
|
||||
if changed
|
||||
&& self.active_search_key.is_some()
|
||||
&& self.preparing_list.is_none()
|
||||
&& self.error.is_none()
|
||||
&& !self.list.has_pending_icons()
|
||||
&& (self.next_offset > 0 || self.exhausted)
|
||||
@@ -1423,6 +1429,7 @@ impl DiscoveryState {
|
||||
.chain(cached.iter().flat_map(|search| search.entries.iter())),
|
||||
);
|
||||
self.active_search_key = Some(key);
|
||||
self.preparing_list = None;
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
self.project_page = None;
|
||||
self.version_popup = None;
|
||||
@@ -1452,13 +1459,18 @@ impl DiscoveryState {
|
||||
self.page_loading = false;
|
||||
self.list.refresh_source_stream(context)
|
||||
} else if reconcile {
|
||||
self.list.refresh_source_stream(context)
|
||||
self.list.cancel_source_stream();
|
||||
let mut preparing = ContentListState::default();
|
||||
let stream = preparing.start_source_stream(context);
|
||||
preparing.stage_source_rows_until_order();
|
||||
preparing.search.query.clone_from(&self.search.query);
|
||||
preparing.set_search_filtering(false);
|
||||
self.preparing_list = Some(preparing);
|
||||
stream
|
||||
} else {
|
||||
self.list.start_source_stream(context)
|
||||
};
|
||||
if reconcile {
|
||||
self.list.stage_source_rows_until_order();
|
||||
} else {
|
||||
if !reconcile {
|
||||
self.list.show_source_rows_progressively();
|
||||
}
|
||||
self.list.search.query.clone_from(&self.search.query);
|
||||
@@ -1486,8 +1498,8 @@ impl DiscoveryState {
|
||||
}
|
||||
self.page_loading = true;
|
||||
self.retry_page_at = None;
|
||||
let known_projects = self
|
||||
.list
|
||||
let list = self.preparing_list.as_ref().unwrap_or(&self.list);
|
||||
let known_projects = list
|
||||
.entries
|
||||
.iter()
|
||||
.filter_map(|entry| {
|
||||
@@ -1500,7 +1512,7 @@ impl DiscoveryState {
|
||||
})
|
||||
.collect();
|
||||
let cached_icons = cached_icons(
|
||||
self.list.entries.iter().chain(
|
||||
list.entries.iter().chain(
|
||||
self.cached_searches
|
||||
.iter()
|
||||
.flat_map(|(_, search)| search.entries.iter()),
|
||||
@@ -1512,7 +1524,7 @@ impl DiscoveryState {
|
||||
limit: self.request_limit(),
|
||||
pending: self.pending.clone(),
|
||||
stream: self.stream.clone()?,
|
||||
reconcile: false,
|
||||
reconcile: self.preparing_list.is_some(),
|
||||
cached_icons,
|
||||
known_projects,
|
||||
sort: self.sort,
|
||||
@@ -1807,7 +1819,11 @@ impl DiscoveryState {
|
||||
}
|
||||
}
|
||||
let mut changed = false;
|
||||
for entry in &mut self.list.entries {
|
||||
for entry in self.list.entries.iter_mut().chain(
|
||||
self.preparing_list
|
||||
.iter_mut()
|
||||
.flat_map(|list| &mut list.entries),
|
||||
) {
|
||||
if let Some(source) = entry.provider_project.as_mut() {
|
||||
source.version_id = installed_identity(source)
|
||||
.map(|installed| installed.version_id)
|
||||
@@ -2069,12 +2085,36 @@ impl DiscoveryState {
|
||||
{
|
||||
return Some("Loading game versions...");
|
||||
}
|
||||
if self.list.has_pending_icons() {
|
||||
if self.list.has_pending_icons() || self.preparing_list.is_some() {
|
||||
return Some("Loading Discovery icons...");
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) fn drain_list(&mut self, picker: &ratatui_image::picker::Picker) {
|
||||
if let Some(preparing) = self.preparing_list.as_mut() {
|
||||
self.list.request_image_loads(picker);
|
||||
self.list.drain_image_loads(picker);
|
||||
preparing.drain_pending();
|
||||
preparing.request_image_loads(picker);
|
||||
preparing.drain_image_loads(picker);
|
||||
if !self.page_loading
|
||||
&& preparing.source_order_ready()
|
||||
&& !preparing.has_pending_icons()
|
||||
{
|
||||
let mut ready = self.preparing_list.take().unwrap();
|
||||
ready.list_state.selected = self.list.list_state.selected;
|
||||
ready.clamp_selected_index();
|
||||
self.list = ready;
|
||||
crate::feedback::request_redraw();
|
||||
}
|
||||
} else {
|
||||
self.list.drain_pending();
|
||||
self.list.request_image_loads(picker);
|
||||
self.list.drain_image_loads(picker);
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_selected_sort(&mut self) {
|
||||
if self.local_mode {
|
||||
let selected = self.sort_panel_selected + 5;
|
||||
@@ -2289,6 +2329,9 @@ impl DiscoveryState {
|
||||
self.generation = self.generation.wrapping_add(1);
|
||||
self.list.cancel_source_stream();
|
||||
self.stream = None;
|
||||
if self.preparing_list.take().is_some() {
|
||||
self.active_search_key = None;
|
||||
}
|
||||
let now = std::time::Instant::now();
|
||||
self.search_changed_at = Some(if debounce { now } else { now - SEARCH_DEBOUNCE });
|
||||
}
|
||||
@@ -2379,6 +2422,7 @@ impl DiscoveryState {
|
||||
error.message
|
||||
);
|
||||
} else if pending.offset == 0 {
|
||||
self.preparing_list = None;
|
||||
self.total_hits = 0;
|
||||
self.error = Some(error.message);
|
||||
self.exhausted = true;
|
||||
@@ -2574,6 +2618,7 @@ impl DiscoveryState {
|
||||
|
||||
fn should_load_more(&self) -> bool {
|
||||
if self.page_loading
|
||||
|| (self.preparing_list.is_some() && self.retry_page_at.is_none())
|
||||
|| self.exhausted
|
||||
|| self.error.is_some()
|
||||
|| self.stream.is_none()
|
||||
@@ -2963,7 +3008,6 @@ pub(crate) fn project_identity(project: &DiscoveryProject) -> String {
|
||||
}
|
||||
|
||||
fn project_identity_parts(title: &str, slug: &str) -> String {
|
||||
// ponytail: provider APIs expose no shared project id; title plus slug avoids
|
||||
// hiding unrelated projects while still matching normal cross-provider copies.
|
||||
let normalize = |value: &str| {
|
||||
value
|
||||
|
||||
@@ -298,7 +298,7 @@ pub struct ContentListState {
|
||||
stream_order: ContentStreamOrder,
|
||||
progressive_source_stream: bool,
|
||||
staged_source_updates: Option<Vec<ContentStreamUpdate>>,
|
||||
show_pending_source_rows: bool,
|
||||
source_order_ready: bool,
|
||||
preview_count: usize,
|
||||
// file watcher: notify callback spawns background work,
|
||||
// precomputed diff lands here for the UI to pick up
|
||||
@@ -355,7 +355,7 @@ impl Default for ContentListState {
|
||||
stream_order: ContentStreamOrder::default(),
|
||||
progressive_source_stream: false,
|
||||
staged_source_updates: None,
|
||||
show_pending_source_rows: false,
|
||||
source_order_ready: false,
|
||||
preview_count: 0,
|
||||
watcher_diff: Arc::new(Mutex::new(None)),
|
||||
_watcher: None,
|
||||
@@ -698,7 +698,7 @@ impl ContentListState {
|
||||
self.stream_order = ContentStreamOrder::Source;
|
||||
self.progressive_source_stream = false;
|
||||
self.staged_source_updates = None;
|
||||
self.show_pending_source_rows = false;
|
||||
self.source_order_ready = false;
|
||||
self.preview_count = 0;
|
||||
self.loaded_for = Some(source.into());
|
||||
ContentStream { sender }
|
||||
@@ -720,6 +720,10 @@ impl ContentListState {
|
||||
self.staged_source_updates = Some(Vec::new());
|
||||
}
|
||||
|
||||
pub(crate) fn source_order_ready(&self) -> bool {
|
||||
self.source_order_ready
|
||||
}
|
||||
|
||||
fn start_stream_with_order(
|
||||
&mut self,
|
||||
source: impl Into<String>,
|
||||
@@ -744,7 +748,7 @@ impl ContentListState {
|
||||
self.stream_order = order;
|
||||
self.progressive_source_stream = false;
|
||||
self.staged_source_updates = None;
|
||||
self.show_pending_source_rows = false;
|
||||
self.source_order_ready = false;
|
||||
self.preview_count = 0;
|
||||
ContentStream { sender }
|
||||
}
|
||||
@@ -850,7 +854,14 @@ impl ContentListState {
|
||||
entry.file_stem == result.file_stem
|
||||
&& entry.path == result.path
|
||||
&& (matches!(self.stream_order, ContentStreamOrder::Sorted)
|
||||
|| entry.provider_project == result.source)
|
||||
|| entry
|
||||
.provider_project
|
||||
.as_ref()
|
||||
.map(|project| (&project.provider, &project.project_id))
|
||||
== result
|
||||
.source
|
||||
.as_ref()
|
||||
.map(|project| (&project.provider, &project.project_id)))
|
||||
&& entry.icon_bytes.as_ref() == Some(&result.icon_bytes)
|
||||
}) {
|
||||
self.pending_entry_images.remove(&result.file_stem);
|
||||
@@ -881,7 +892,6 @@ impl ContentListState {
|
||||
if ordered {
|
||||
staged = std::mem::take(updates).into();
|
||||
self.staged_source_updates = None;
|
||||
self.show_pending_source_rows = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1018,6 +1028,7 @@ impl ContentListState {
|
||||
}
|
||||
Ok(ContentStreamUpdate::Order(file_stems)) => {
|
||||
received = true;
|
||||
self.source_order_ready = true;
|
||||
let positions = file_stems
|
||||
.iter()
|
||||
.enumerate()
|
||||
@@ -1435,8 +1446,7 @@ impl ContentListState {
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|(_, entry)| {
|
||||
(self.show_pending_source_rows
|
||||
|| !self.pending_entry_images.contains(&entry.file_stem))
|
||||
!self.pending_entry_images.contains(&entry.file_stem)
|
||||
&& (!self.filter_search
|
||||
|| self.search.matches(&entry.name)
|
||||
|| self.search.matches(&entry.description))
|
||||
@@ -1717,7 +1727,6 @@ impl ContentListState {
|
||||
}
|
||||
|
||||
// try cache first
|
||||
// ponytail: cached file stats are refreshed by watcher updates; revalidate on restore if
|
||||
// files modified while another instance was active become a real problem.
|
||||
if let Some(cached) = self.cache.remove(instance_name) {
|
||||
self.entries = cached.entries;
|
||||
|
||||
@@ -110,9 +110,7 @@ pub fn open() {
|
||||
pub fn drain(picker: &ratatui_image::picker::Picker) {
|
||||
if let Ok(mut state) = DISCOVERY_STATE.lock() {
|
||||
state.drain_pending();
|
||||
state.list.drain_pending();
|
||||
state.list.request_image_loads(picker);
|
||||
state.list.drain_image_loads(picker);
|
||||
state.drain_list(picker);
|
||||
if state.search_due() {
|
||||
drop(state);
|
||||
start_discovery_search();
|
||||
|
||||
Reference in new issue
Block a user