BlueGit

ict rusty-teams

fix(sidebar): load the synced sidebar off the UI thread

- SidebarChanged read all chats (1281) on the UI thread: 38 ms median, up to 1 s in diagnostics.log
- load sidebar and unread counts in the background, bursts load once more
- a local sidebar reload during the load discards the stale result

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-11-10 · 3 weeks ago

Hash
ec36c5a502e794e64a02170c027bb2d557d4b762
Parent
03526c4c
4 files changed +214 −35
crates/app-state/src/app_state.rs +29 −9
@@ -148,6 +148,7 @@ pub struct AppState {
148 148 pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,
149 149 pub typing: TypingState,
150 150 typing_timer_running: bool,
151 pub(crate) sidebar_load: crate::sidebar_load::SidebarLoad,
151 152 pub local_previews: HashMap<String, LocalPreview>,
152 153 pub quick_view: Option<QuickView>,
153 154 pub scheduled: Vec<ScheduledDraft>,
@@ -305,6 +306,7 @@ impl AppState {
305 306 restorable_deletes: Default::default(),
306 307 typing: TypingState::default(),
307 308 typing_timer_running: false,
309 sidebar_load: Default::default(),
308 310 local_previews: HashMap::new(),
309 311 quick_view: None,
310 312 scheduled: Vec::new(),
@@ -585,7 +587,7 @@ impl AppState {
585 587 cx.notify();
586 588 }
587 589
588 pub fn reload_directory(&mut self) {
590 fn reload_directory_without_unread(&mut self) {
589 591 self.directory.me = data::me(&self.store);
590 592 self.own_email = self
591 593 .store
@@ -614,14 +616,13 @@ impl AppState {
614 616 }
615 617 self.resolve_pending_avatars();
616 618 self.sync_presence();
617 self.directory.unread_counts = match &self.engine {
618 Some(engine) => data::unread_counts(engine, &self.sidebar.chats),
619 None => Default::default(),
620 };
621 self.directory.unread_importance = match &self.engine {
622 Some(engine) => data::unread_importance(engine, &self.sidebar.chats),
623 None => Default::default(),
624 };
619 }
620
621 pub fn reload_directory(&mut self) {
622 self.reload_directory_without_unread();
623 let unread = crate::sidebar_load::unread_of(self.engine.as_deref(), &self.sidebar.chats);
624 self.directory.unread_counts = unread.counts;
625 self.directory.unread_importance = unread.importance;
625 626 }
626 627
627 628 pub fn request_avatars(&mut self, user_ids: Vec<String>, cx: &mut Context<Self>) {
@@ -1086,6 +1087,7 @@ impl AppState {
1086 1087 }
1087 1088
1088 1089 pub fn reload_sidebar(&mut self, cx: &mut Context<Self>) {
1090 self.sidebar_load.local_change();
1089 1091 if let Ok(sidebar) = self.store.sidebar() {
1090 1092 self.sidebar = sidebar;
1091 1093 self.apply_bot_titles();
@@ -1099,6 +1101,24 @@ impl AppState {
1099 1101 cx.notify();
1100 1102 }
1101 1103
1104 pub(crate) fn adopt_loaded_sidebar(
1105 &mut self,
1106 loaded: crate::sidebar_load::LoadedSidebar,
1107 cx: &mut Context<Self>,
1108 ) {
1109 self.sidebar = loaded.sidebar;
1110 self.apply_bot_titles();
1111 self.calls_list.invalidate();
1112 if !self.mode.demo {
1113 self.reload_directory_without_unread();
1114 self.directory.unread_counts = loaded.unread.counts;
1115 self.directory.unread_importance = loaded.unread.importance;
1116 self.load_own_time_zone(cx);
1117 }
1118 cx.emit(AppEvent::Sidebar);
1119 cx.notify();
1120 }
1121
1102 1122 pub fn apply(&mut self, event: BackendEvent, cx: &mut Context<Self>) {
1103 1123 match event {
1104 1124 BackendEvent::Connection(connection) => {
@@ -148,6 +148,7 @@ pub struct AppState {
148 pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,
148 pub restorable_deletes: crate::restorable_deletes::RestorableDeletes,
149 pub typing: TypingState,
149 pub typing: TypingState,
150 typing_timer_running: bool,
150 typing_timer_running: bool,
151 pub(crate) sidebar_load: crate::sidebar_load::SidebarLoad,
151 pub local_previews: HashMap<String, LocalPreview>,
152 pub local_previews: HashMap<String, LocalPreview>,
152 pub quick_view: Option<QuickView>,
153 pub quick_view: Option<QuickView>,
153 pub scheduled: Vec<ScheduledDraft>,
154 pub scheduled: Vec<ScheduledDraft>,
@@ -305,6 +306,7 @@ impl AppState {
305 restorable_deletes: Default::default(),
306 restorable_deletes: Default::default(),
306 typing: TypingState::default(),
307 typing: TypingState::default(),
307 typing_timer_running: false,
308 typing_timer_running: false,
309 sidebar_load: Default::default(),
308 local_previews: HashMap::new(),
310 local_previews: HashMap::new(),
309 quick_view: None,
311 quick_view: None,
310 scheduled: Vec::new(),
312 scheduled: Vec::new(),
@@ -585,7 +587,7 @@ impl AppState {
585 cx.notify();
587 cx.notify();
586 }
588 }
587
589
588 pub fn reload_directory(&mut self) {
590 fn reload_directory_without_unread(&mut self) {
589 self.directory.me = data::me(&self.store);
591 self.directory.me = data::me(&self.store);
590 self.own_email = self
592 self.own_email = self
591 .store
593 .store
@@ -614,14 +616,13 @@ impl AppState {
614 }
616 }
615 self.resolve_pending_avatars();
617 self.resolve_pending_avatars();
616 self.sync_presence();
618 self.sync_presence();
617 self.directory.unread_counts = match &self.engine {
619 }
618 Some(engine) => data::unread_counts(engine, &self.sidebar.chats),
620
619 None => Default::default(),
621 pub fn reload_directory(&mut self) {
620 };
622 self.reload_directory_without_unread();
621 self.directory.unread_importance = match &self.engine {
623 let unread = crate::sidebar_load::unread_of(self.engine.as_deref(), &self.sidebar.chats);
622 Some(engine) => data::unread_importance(engine, &self.sidebar.chats),
624 self.directory.unread_counts = unread.counts;
623 None => Default::default(),
625 self.directory.unread_importance = unread.importance;
624 };
625 }
626 }
626
627
627 pub fn request_avatars(&mut self, user_ids: Vec<String>, cx: &mut Context<Self>) {
628 pub fn request_avatars(&mut self, user_ids: Vec<String>, cx: &mut Context<Self>) {
@@ -1086,6 +1087,7 @@ impl AppState {
1086 }
1087 }
1087
1088
1088 pub fn reload_sidebar(&mut self, cx: &mut Context<Self>) {
1089 pub fn reload_sidebar(&mut self, cx: &mut Context<Self>) {
1090 self.sidebar_load.local_change();
1089 if let Ok(sidebar) = self.store.sidebar() {
1091 if let Ok(sidebar) = self.store.sidebar() {
1090 self.sidebar = sidebar;
1092 self.sidebar = sidebar;
1091 self.apply_bot_titles();
1093 self.apply_bot_titles();
@@ -1099,6 +1101,24 @@ impl AppState {
1099 cx.notify();
1101 cx.notify();
1100 }
1102 }
1101
1103
1104 pub(crate) fn adopt_loaded_sidebar(
1105 &mut self,
1106 loaded: crate::sidebar_load::LoadedSidebar,
1107 cx: &mut Context<Self>,
1108 ) {
1109 self.sidebar = loaded.sidebar;
1110 self.apply_bot_titles();
1111 self.calls_list.invalidate();
1112 if !self.mode.demo {
1113 self.reload_directory_without_unread();
1114 self.directory.unread_counts = loaded.unread.counts;
1115 self.directory.unread_importance = loaded.unread.importance;
1116 self.load_own_time_zone(cx);
1117 }
1118 cx.emit(AppEvent::Sidebar);
1119 cx.notify();
1120 }
1121
1102 pub fn apply(&mut self, event: BackendEvent, cx: &mut Context<Self>) {
1122 pub fn apply(&mut self, event: BackendEvent, cx: &mut Context<Self>) {
1103 match event {
1123 match event {
1104 BackendEvent::Connection(connection) => {
1124 BackendEvent::Connection(connection) => {
crates/app-state/src/chat_actions.rs +2 −26
@@ -429,7 +429,7 @@ impl AppState {
429 429 }
430 430 }
431 431
432 fn leave_removed_selection(&mut self, next: Option<String>, cx: &mut Context<Self>) {
432 pub(crate) fn leave_removed_selection(&mut self, next: Option<String>, cx: &mut Context<Self>) {
433 433 match next {
434 434 Some(next_id) => self.select(Selection::Chat(next_id), cx),
435 435 None => {
@@ -441,37 +441,13 @@ impl AppState {
441 441 }
442 442 }
443 443
444 pub fn reload_sidebar_after_sync(&mut self, cx: &mut Context<Self>) {
445 let selected = match &self.selection {
446 Some(Selection::Chat(chat_id)) => Some(chat_id.clone()),
447 _ => None,
448 };
449 let is_listed =
450 |state: &Self, chat_id: &str| state.sidebar.chats.iter().any(|chat| chat.id == chat_id);
451 let was_listed = selected
452 .as_deref()
453 .is_some_and(|chat_id| is_listed(self, chat_id));
454 let next = selected
455 .as_deref()
456 .filter(|_| was_listed)
457 .and_then(|chat_id| self.next_chat_after(chat_id));
458 self.reload_sidebar(cx);
459 let removed = was_listed
460 && selected
461 .as_deref()
462 .is_some_and(|chat_id| !is_listed(self, chat_id));
463 if removed {
464 self.leave_removed_selection(next, cx);
465 }
466 }
467
468 444 fn restore_chat_locally(&mut self, chat: &ChatRecord, cx: &mut Context<Self>) {
469 445 let _ = self.store.upsert_chats(std::slice::from_ref(chat));
470 446 let _ = self.store.set_chat_muted(&chat.id, chat.muted);
471 447 self.reload_sidebar(cx);
472 448 }
473 449
474 fn next_chat_after(&self, chat_id: &str) -> Option<String> {
450 pub(crate) fn next_chat_after(&self, chat_id: &str) -> Option<String> {
475 451 next_chat_id(&self.chat_sections(), chat_id)
476 452 }
477 453
@@ -429,7 +429,7 @@ impl AppState {
429 }
429 }
430 }
430 }
431
431
432 fn leave_removed_selection(&mut self, next: Option<String>, cx: &mut Context<Self>) {
432 pub(crate) fn leave_removed_selection(&mut self, next: Option<String>, cx: &mut Context<Self>) {
433 match next {
433 match next {
434 Some(next_id) => self.select(Selection::Chat(next_id), cx),
434 Some(next_id) => self.select(Selection::Chat(next_id), cx),
435 None => {
435 None => {
@@ -441,37 +441,13 @@ impl AppState {
441 }
441 }
442 }
442 }
443
443
444 pub fn reload_sidebar_after_sync(&mut self, cx: &mut Context<Self>) {
445 let selected = match &self.selection {
446 Some(Selection::Chat(chat_id)) => Some(chat_id.clone()),
447 _ => None,
448 };
449 let is_listed =
450 |state: &Self, chat_id: &str| state.sidebar.chats.iter().any(|chat| chat.id == chat_id);
451 let was_listed = selected
452 .as_deref()
453 .is_some_and(|chat_id| is_listed(self, chat_id));
454 let next = selected
455 .as_deref()
456 .filter(|_| was_listed)
457 .and_then(|chat_id| self.next_chat_after(chat_id));
458 self.reload_sidebar(cx);
459 let removed = was_listed
460 && selected
461 .as_deref()
462 .is_some_and(|chat_id| !is_listed(self, chat_id));
463 if removed {
464 self.leave_removed_selection(next, cx);
465 }
466 }
467
468 fn restore_chat_locally(&mut self, chat: &ChatRecord, cx: &mut Context<Self>) {
444 fn restore_chat_locally(&mut self, chat: &ChatRecord, cx: &mut Context<Self>) {
469 let _ = self.store.upsert_chats(std::slice::from_ref(chat));
445 let _ = self.store.upsert_chats(std::slice::from_ref(chat));
470 let _ = self.store.set_chat_muted(&chat.id, chat.muted);
446 let _ = self.store.set_chat_muted(&chat.id, chat.muted);
471 self.reload_sidebar(cx);
447 self.reload_sidebar(cx);
472 }
448 }
473
449
474 fn next_chat_after(&self, chat_id: &str) -> Option<String> {
450 pub(crate) fn next_chat_after(&self, chat_id: &str) -> Option<String> {
475 next_chat_id(&self.chat_sections(), chat_id)
451 next_chat_id(&self.chat_sections(), chat_id)
476 }
452 }
477
453
crates/app-state/src/lib.rs +1 −0
@@ -12,6 +12,7 @@ pub mod card_refresh;
12 12 pub mod card_state;
13 13 pub mod channel_menu;
14 14 pub mod chat_actions;
15 mod sidebar_load;
15 16 pub mod channel_filter;
16 17 pub mod chat_filter;
17 18 pub mod find_in_chat;
@@ -12,6 +12,7 @@ pub mod card_refresh;
12pub mod card_state;
12pub mod card_state;
13pub mod channel_menu;
13pub mod channel_menu;
14pub mod chat_actions;
14pub mod chat_actions;
15mod sidebar_load;
15pub mod channel_filter;
16pub mod channel_filter;
16pub mod chat_filter;
17pub mod chat_filter;
17pub mod find_in_chat;
18pub mod find_in_chat;
crates/app-state/src/sidebar_load.rs added +182 −0
@@ -0,0 +1,182 @@
1 use std::collections::HashMap;
2
3 use gpui_kit::*;
4 use store::{ChatRecord, MessageImportance, Sidebar, Store};
5
6 use crate::app_state::{AppState, Selection};
7 use crate::backend::Engine;
8 use crate::data;
9
10 #[derive(Default)]
11 pub struct SidebarLoad {
12 running: bool,
13 requested_again: bool,
14 local_changes: u64,
15 }
16
17 impl SidebarLoad {
18 pub fn local_change(&mut self) {
19 self.local_changes += 1;
20 }
21 }
22
23 #[derive(Default)]
24 pub struct Unread {
25 pub counts: HashMap<String, u32>,
26 pub importance: HashMap<String, MessageImportance>,
27 }
28
29 pub struct LoadedSidebar {
30 pub sidebar: Sidebar,
31 pub unread: Unread,
32 }
33
34 pub fn unread_of(engine: Option<&Engine>, chats: &[ChatRecord]) -> Unread {
35 match engine {
36 Some(engine) => Unread {
37 counts: data::unread_counts(engine, chats),
38 importance: data::unread_importance(engine, chats),
39 },
40 None => Unread::default(),
41 }
42 }
43
44 fn load(store: &Store, engine: Option<&Engine>) -> Option<LoadedSidebar> {
45 let sidebar = store.sidebar().ok()?;
46 let unread = unread_of(engine, &sidebar.chats);
47 Some(LoadedSidebar { sidebar, unread })
48 }
49
50 impl AppState {
51 pub fn reload_sidebar_after_sync(&mut self, cx: &mut Context<Self>) {
52 if self.sidebar_load.running {
53 self.sidebar_load.requested_again = true;
54 return;
55 }
56 self.sidebar_load.running = true;
57 cx.spawn(async move |this, cx| {
58 loop {
59 let Ok((store, engine, local_changes)) = this.update(cx, |state, _| {
60 state.sidebar_load.requested_again = false;
61 (
62 state.store.clone(),
63 state.engine.clone(),
64 state.sidebar_load.local_changes,
65 )
66 }) else {
67 return;
68 };
69 let loaded = cx
70 .background_spawn(async move { load(&store, engine.as_deref()) })
71 .await;
72 let again = this.update(cx, |state, cx| {
73 let stale = state.sidebar_load.local_changes != local_changes;
74 if let Some(loaded) = loaded.filter(|_| !stale) {
75 state.adopt_synced_sidebar(loaded, cx);
76 }
77 let again = state.sidebar_load.requested_again || stale;
78 state.sidebar_load.running = again;
79 again
80 });
81 if !matches!(again, Ok(true)) {
82 return;
83 }
84 }
85 })
86 .detach();
87 }
88
89 fn adopt_synced_sidebar(&mut self, loaded: LoadedSidebar, cx: &mut Context<Self>) {
90 let selected = match &self.selection {
91 Some(Selection::Chat(chat_id)) => Some(chat_id.clone()),
92 _ => None,
93 };
94 let is_listed =
95 |chats: &[ChatRecord], chat_id: &str| chats.iter().any(|chat| chat.id == chat_id);
96 let was_listed = selected
97 .as_deref()
98 .is_some_and(|chat_id| is_listed(&self.sidebar.chats, chat_id));
99 let removed = was_listed
100 && selected
101 .as_deref()
102 .is_some_and(|chat_id| !is_listed(&loaded.sidebar.chats, chat_id));
103 let next = selected
104 .as_deref()
105 .filter(|_| removed)
106 .and_then(|chat_id| self.next_chat_after(chat_id));
107 self.adopt_loaded_sidebar(loaded, cx);
108 if removed {
109 self.leave_removed_selection(next, cx);
110 }
111 }
112 }
113
114 #[cfg(test)]
115 mod tests {
116 use std::sync::Arc;
117
118 use gpui_kit::{AppContext, TestAppContext};
119 use store::{ChatRecord, Store};
120 use teams_core::CoreEvent;
121
122 use crate::app_state::{AppState, Mode, Selection};
123 use crate::backend::BackendEvent;
124
125 fn chat(id: &str) -> ChatRecord {
126 ChatRecord {
127 id: id.into(),
128 kind: "group".into(),
129 title: id.into(),
130 ..Default::default()
131 }
132 }
133
134 fn listed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) -> Vec<String> {
135 cx.update(|cx| app.read(cx).sidebar.chats.iter().map(|chat| chat.id.clone()).collect())
136 }
137
138 fn sidebar_changed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) {
139 cx.update(|cx| {
140 app.update(cx, |state, cx| {
141 state.apply(BackendEvent::Core(CoreEvent::SidebarChanged), cx)
142 })
143 });
144 }
145
146 #[gpui_kit::test]
147 fn a_synced_sidebar_loads_in_the_background_and_bursts_load_once_more(
148 cx: &mut TestAppContext,
149 ) {
150 cx.update(gpui_kit::init);
151 let store = Arc::new(Store::open_in_memory().unwrap());
152 let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
153 store.upsert_chats(&[chat("first")]).unwrap();
154 sidebar_changed(&app, cx);
155 assert!(listed(&app, cx).is_empty());
156 store.upsert_chats(&[chat("second")]).unwrap();
157 sidebar_changed(&app, cx);
158 cx.run_until_parked();
159 let mut chats = listed(&app, cx);
160 chats.sort();
161 assert_eq!(chats, vec!["first".to_owned(), "second".to_owned()]);
162 }
163
164 #[gpui_kit::test]
165 fn a_selected_chat_gone_after_sync_moves_the_selection_on(cx: &mut TestAppContext) {
166 cx.update(gpui_kit::init);
167 let store = Arc::new(Store::open_in_memory().unwrap());
168 store.upsert_chats(&[chat("kept"), chat("gone")]).unwrap();
169 let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
170 cx.update(|cx| {
171 app.update(cx, |state, cx| {
172 state.reload_sidebar(cx);
173 state.selection = Some(Selection::Chat("gone".into()));
174 })
175 });
176 store.remove_chats(&["gone".to_owned()]).unwrap();
177 sidebar_changed(&app, cx);
178 cx.run_until_parked();
179 let selection = cx.update(|cx| app.read(cx).selection.clone());
180 assert_eq!(selection, Some(Selection::Chat("kept".into())));
181 }
182 }
@@ -0,0 +1,182 @@
1use std::collections::HashMap;
2
3use gpui_kit::*;
4use store::{ChatRecord, MessageImportance, Sidebar, Store};
5
6use crate::app_state::{AppState, Selection};
7use crate::backend::Engine;
8use crate::data;
9
10#[derive(Default)]
11pub struct SidebarLoad {
12 running: bool,
13 requested_again: bool,
14 local_changes: u64,
15}
16
17impl SidebarLoad {
18 pub fn local_change(&mut self) {
19 self.local_changes += 1;
20 }
21}
22
23#[derive(Default)]
24pub struct Unread {
25 pub counts: HashMap<String, u32>,
26 pub importance: HashMap<String, MessageImportance>,
27}
28
29pub struct LoadedSidebar {
30 pub sidebar: Sidebar,
31 pub unread: Unread,
32}
33
34pub fn unread_of(engine: Option<&Engine>, chats: &[ChatRecord]) -> Unread {
35 match engine {
36 Some(engine) => Unread {
37 counts: data::unread_counts(engine, chats),
38 importance: data::unread_importance(engine, chats),
39 },
40 None => Unread::default(),
41 }
42}
43
44fn load(store: &Store, engine: Option<&Engine>) -> Option<LoadedSidebar> {
45 let sidebar = store.sidebar().ok()?;
46 let unread = unread_of(engine, &sidebar.chats);
47 Some(LoadedSidebar { sidebar, unread })
48}
49
50impl AppState {
51 pub fn reload_sidebar_after_sync(&mut self, cx: &mut Context<Self>) {
52 if self.sidebar_load.running {
53 self.sidebar_load.requested_again = true;
54 return;
55 }
56 self.sidebar_load.running = true;
57 cx.spawn(async move |this, cx| {
58 loop {
59 let Ok((store, engine, local_changes)) = this.update(cx, |state, _| {
60 state.sidebar_load.requested_again = false;
61 (
62 state.store.clone(),
63 state.engine.clone(),
64 state.sidebar_load.local_changes,
65 )
66 }) else {
67 return;
68 };
69 let loaded = cx
70 .background_spawn(async move { load(&store, engine.as_deref()) })
71 .await;
72 let again = this.update(cx, |state, cx| {
73 let stale = state.sidebar_load.local_changes != local_changes;
74 if let Some(loaded) = loaded.filter(|_| !stale) {
75 state.adopt_synced_sidebar(loaded, cx);
76 }
77 let again = state.sidebar_load.requested_again || stale;
78 state.sidebar_load.running = again;
79 again
80 });
81 if !matches!(again, Ok(true)) {
82 return;
83 }
84 }
85 })
86 .detach();
87 }
88
89 fn adopt_synced_sidebar(&mut self, loaded: LoadedSidebar, cx: &mut Context<Self>) {
90 let selected = match &self.selection {
91 Some(Selection::Chat(chat_id)) => Some(chat_id.clone()),
92 _ => None,
93 };
94 let is_listed =
95 |chats: &[ChatRecord], chat_id: &str| chats.iter().any(|chat| chat.id == chat_id);
96 let was_listed = selected
97 .as_deref()
98 .is_some_and(|chat_id| is_listed(&self.sidebar.chats, chat_id));
99 let removed = was_listed
100 && selected
101 .as_deref()
102 .is_some_and(|chat_id| !is_listed(&loaded.sidebar.chats, chat_id));
103 let next = selected
104 .as_deref()
105 .filter(|_| removed)
106 .and_then(|chat_id| self.next_chat_after(chat_id));
107 self.adopt_loaded_sidebar(loaded, cx);
108 if removed {
109 self.leave_removed_selection(next, cx);
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use std::sync::Arc;
117
118 use gpui_kit::{AppContext, TestAppContext};
119 use store::{ChatRecord, Store};
120 use teams_core::CoreEvent;
121
122 use crate::app_state::{AppState, Mode, Selection};
123 use crate::backend::BackendEvent;
124
125 fn chat(id: &str) -> ChatRecord {
126 ChatRecord {
127 id: id.into(),
128 kind: "group".into(),
129 title: id.into(),
130 ..Default::default()
131 }
132 }
133
134 fn listed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) -> Vec<String> {
135 cx.update(|cx| app.read(cx).sidebar.chats.iter().map(|chat| chat.id.clone()).collect())
136 }
137
138 fn sidebar_changed(app: &gpui_kit::Entity<AppState>, cx: &mut TestAppContext) {
139 cx.update(|cx| {
140 app.update(cx, |state, cx| {
141 state.apply(BackendEvent::Core(CoreEvent::SidebarChanged), cx)
142 })
143 });
144 }
145
146 #[gpui_kit::test]
147 fn a_synced_sidebar_loads_in_the_background_and_bursts_load_once_more(
148 cx: &mut TestAppContext,
149 ) {
150 cx.update(gpui_kit::init);
151 let store = Arc::new(Store::open_in_memory().unwrap());
152 let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
153 store.upsert_chats(&[chat("first")]).unwrap();
154 sidebar_changed(&app, cx);
155 assert!(listed(&app, cx).is_empty());
156 store.upsert_chats(&[chat("second")]).unwrap();
157 sidebar_changed(&app, cx);
158 cx.run_until_parked();
159 let mut chats = listed(&app, cx);
160 chats.sort();
161 assert_eq!(chats, vec!["first".to_owned(), "second".to_owned()]);
162 }
163
164 #[gpui_kit::test]
165 fn a_selected_chat_gone_after_sync_moves_the_selection_on(cx: &mut TestAppContext) {
166 cx.update(gpui_kit::init);
167 let store = Arc::new(Store::open_in_memory().unwrap());
168 store.upsert_chats(&[chat("kept"), chat("gone")]).unwrap();
169 let app = cx.update(|cx| cx.new(|_| AppState::new(store.clone(), Mode::default())));
170 cx.update(|cx| {
171 app.update(cx, |state, cx| {
172 state.reload_sidebar(cx);
173 state.selection = Some(Selection::Chat("gone".into()));
174 })
175 });
176 store.remove_chats(&["gone".to_owned()]).unwrap();
177 sidebar_changed(&app, cx);
178 cx.run_until_parked();
179 let selection = cx.update(|cx| app.read(cx).selection.clone());
180 assert_eq!(selection, Some(Selection::Chat("kept".into())));
181 }
182}