Update list to save the task on the struct.

This commit is contained in:
Jason Lee 2024-07-23 10:51:11 +08:00
parent 80bdf20376
commit 652efd187c
2 changed files with 7 additions and 6 deletions

View file

@ -37,7 +37,7 @@ impl ListDelegate for ListItemDeletegate {
let query = query.to_string();
cx.spawn(move |this, mut cx| async move {
// Simulate a slow search.
let sleep = (0.15..0.3).fake();
let sleep = (0.05..0.1).fake();
Timer::after(Duration::from_secs_f64(sleep)).await;
this.update(&mut cx, |this, cx| {

View file

@ -79,6 +79,7 @@ pub struct List<D: ListDelegate> {
scrollbar_state: Rc<Cell<ScrollbarState>>,
selected_index: Option<usize>,
_search_task: Task<()>,
}
impl<D> List<D>
@ -108,6 +109,7 @@ where
max_height: None,
enable_scrollbar: true,
loading: false,
_search_task: Task::Ready(None),
}
}
@ -195,16 +197,15 @@ where
self.set_loading(true, cx);
let search = self.delegate.perform_search(&text, cx);
cx.spawn(|this, mut cx| async move {
self._search_task = cx.spawn(|this, mut cx| async move {
search.await;
// Always wait 100ms to avoid flicker
Timer::after(Duration::from_millis(100)).await;
this.update(&mut cx, |this, cx| {
let _ = this.update(&mut cx, |this, cx| {
this.last_query = Some(text);
this.set_loading(false, cx);
})
})
.detach();
});
});
}
InputEvent::PressEnter => self.action_confirm(&Confirm, cx),
_ => {}