diff --git a/crates/voelin-core/src/contacts.rs b/crates/voelin-core/src/contacts.rs index fd51969..40815cc 100644 --- a/crates/voelin-core/src/contacts.rs +++ b/crates/voelin-core/src/contacts.rs @@ -110,19 +110,16 @@ impl Contacts { } /// Read the contacts of the current database (after it was attached). - pub fn load(&self) { - let this = self.clone(); - self.history.current().run_then( - false, - |s| s.contacts(), - move |result| match result { - Ok(list) => { - this.lock().contacts = list.into_iter().map(|c| (c.uid.clone(), c)).collect(); - this.changed(); - } - Err(e) => warn!("cannot read contacts: {e}"), - }, - ); + /// The list read replaces the one in memory, so the engine awaits this + /// before its next command: a contact set meanwhile would be lost. + pub async fn load(&self) { + match self.history.current().run(false, |s| s.contacts()).await { + Ok(list) => { + self.lock().contacts = list.into_iter().map(|c| (c.uid.clone(), c)).collect(); + self.changed(); + } + Err(e) => warn!("cannot read contacts: {e}"), + } } /// Add or change a contact. @@ -410,7 +407,7 @@ mod tests { history.current().run(false, |s| s.put_contact(&friend("a="))).await.unwrap(); let contacts = Contacts::new(events, history.clone()); let mut revision = contacts.watch(); - contacts.load(); + contacts.load().await; let loaded = loop { if let Event::ContactsChanged { contacts } = rx.recv().await.unwrap() { break contacts; diff --git a/crates/voelin-core/src/lib.rs b/crates/voelin-core/src/lib.rs index c2de11e..b09d146 100644 --- a/crates/voelin-core/src/lib.rs +++ b/crates/voelin-core/src/lib.rs @@ -676,7 +676,6 @@ impl Engine { history, cache: SharedCache::new(Cache::new(Cache::default_dir())), }; - shared.contacts.load(); tokio::spawn(run(rx, events.clone(), frames.clone(), shared.clone())); Self { commands, events, frames, runtime: tokio::runtime::Handle::current(), shared } } @@ -763,6 +762,8 @@ async fn run( engine: Shared, ) { let Shared { settings: shared, history, cache, contacts } = engine.clone(); + // Before any command: a contact set meanwhile would be lost. + contacts.load().await; let mut sessions: HashMap = HashMap::new(); let mut current = shared.current(); // Pruning: at start, then hourly (and when the setting changes). @@ -851,7 +852,7 @@ async fn run( history.replace(new.clone()); prune_history(¤t, &new); // The contacts live in the same database. - contacts.load(); + contacts.load().await; continue; } Command::AttachCache(dir) => {