voelin-core: load contacts before handling the next command
The contacts read from the database replaced the list in memory when the read finished on the writer thread. A SetContact handled meanwhile was lost (its write was queued behind the read), which made contacts_are_kept_in_the_database fail under load. The engine now awaits the read at start and after AttachHistory before its next command. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
2 files changed
+14
-16
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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<SessionId, session::SessionHandle> = 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) => {
|
||||
|
||||
Reference in new issue
Block a user