diff --git a/desktop/crates/radicle-tauri/src/commands/startup.rs b/desktop/crates/radicle-tauri/src/commands/startup.rs index 493142469..811d860fd 100644 --- a/desktop/crates/radicle-tauri/src/commands/startup.rs +++ b/desktop/crates/radicle-tauri/src/commands/startup.rs @@ -3,7 +3,7 @@ use std::sync::Once; use tauri::{AppHandle, Emitter, Manager}; use radicle::cob::cache::COBS_DB_FILE; -use radicle::node::{Handle, NOTIFICATIONS_DB_FILE, Node}; +use radicle::node::{Handle, NOTIFICATIONS_DB_FILE, Node, POLICIES_DB_FILE}; use radicle_types::binaries::GitInfo; use radicle_types::config::{Config, Version}; @@ -43,7 +43,16 @@ pub(crate) fn startup(app: AppHandle) -> Result { let cobs_db = radicle_types::outbound::sqlite::Sqlite::reader(profile.cobs().join(COBS_DB_FILE))?; - crate::notifications::spawn(app.app_handle().clone(), inbox_db.clone()); + crate::db_changes::spawn( + app.app_handle().clone(), + vec![ + ( + profile.node().join(NOTIFICATIONS_DB_FILE), + "notifications_changed", + ), + (profile.node().join(POLICIES_DB_FILE), "policies_changed"), + ], + ); let inbox_service = domain::inbox::service::Service::new(inbox_db); let patch_service = domain::patch::service::Service::new(cobs_db.clone()); let issue_service = domain::issue::service::Service::new(cobs_db); diff --git a/desktop/crates/radicle-tauri/src/db_changes.rs b/desktop/crates/radicle-tauri/src/db_changes.rs new file mode 100644 index 000000000..fa16fe7f4 --- /dev/null +++ b/desktop/crates/radicle-tauri/src/db_changes.rs @@ -0,0 +1,74 @@ +use std::path::PathBuf; +use std::sync::Once; +use std::time::Duration; + +use radicle_types::outbound::sqlite::Sqlite; +use tauri::{AppHandle, Emitter}; + +/// Tells the frontend when another process changes a database the app reads, +/// by emitting the event named with it. The node's events don't cover these: +/// the `rad` CLI or another instance of the app may clear notifications or +/// change seeding policies, and the node writes notifications as it fetches. +pub(crate) fn spawn(app: AppHandle, databases: Vec<(PathBuf, &'static str)>) { + static WATCHING: Once = Once::new(); + WATCHING.call_once(|| { + std::thread::spawn(move || { + let mut watched: Vec = databases + .into_iter() + .map(|(path, event)| Watched { + path, + event, + db: None, + last: None, + failing: false, + }) + .collect(); + loop { + for watched in &mut watched { + watched.check(&app); + } + std::thread::sleep(Duration::from_secs(2)); + } + }); + }); +} + +struct Watched { + path: PathBuf, + event: &'static str, + /// Opened on first use, and again after a failure, such as when the + /// database doesn't exist yet. + db: Option, + last: Option, + failing: bool, +} + +impl Watched { + fn check(&mut self, app: &AppHandle) { + let version = match &self.db { + Some(db) => db.data_version(), + None => Sqlite::reader(&self.path).and_then(|db| { + let version = db.data_version(); + self.db = Some(db); + version + }), + }; + match version { + Ok(version) => { + if self.last.is_some_and(|last| last != version) { + let _ = app.emit(self.event, ()); + } + self.last = Some(version); + self.failing = false; + } + // Logged once until it works again. + Err(e) => { + if !self.failing { + log::warn!("Not able to watch for `{}`: {e}", self.event); + } + self.db = None; + self.failing = true; + } + } + } +} diff --git a/desktop/crates/radicle-tauri/src/lib.rs b/desktop/crates/radicle-tauri/src/lib.rs index f2fccb1b4..43e1874b8 100644 --- a/desktop/crates/radicle-tauri/src/lib.rs +++ b/desktop/crates/radicle-tauri/src/lib.rs @@ -1,5 +1,5 @@ mod commands; -mod notifications; +mod db_changes; mod ref_updates; use std::sync::atomic::{AtomicBool, Ordering}; diff --git a/desktop/crates/radicle-tauri/src/notifications.rs b/desktop/crates/radicle-tauri/src/notifications.rs deleted file mode 100644 index 7f80ec4b3..000000000 --- a/desktop/crates/radicle-tauri/src/notifications.rs +++ /dev/null @@ -1,37 +0,0 @@ -use std::sync::Once; -use std::time::Duration; - -use radicle_types::outbound::sqlite::Sqlite; -use tauri::{AppHandle, Emitter}; - -/// Tells the frontend when another process changes the notifications, such as -/// the `rad` CLI or another instance of the app clearing some. The node's -/// events only cover notifications it adds while fetching. -pub(crate) fn spawn(app: AppHandle, db: Sqlite) { - static WATCHING: Once = Once::new(); - WATCHING.call_once(|| { - std::thread::spawn(move || { - let mut last = None; - let mut failing = false; - loop { - match db.data_version() { - Ok(version) => { - if last.is_some_and(|last| last != version) { - let _ = app.emit("notifications_changed", ()); - } - last = Some(version); - failing = false; - } - // Logged once until it works again. - Err(e) => { - if !failing { - log::warn!("Not able to watch the notifications: {e}"); - } - failing = true; - } - } - std::thread::sleep(Duration::from_secs(2)); - } - }); - }); -} diff --git a/desktop/src/lib/startup.svelte.ts b/desktop/src/lib/startup.svelte.ts index 48d9ac81d..0f8a81bb1 100644 --- a/desktop/src/lib/startup.svelte.ts +++ b/desktop/src/lib/startup.svelte.ts @@ -43,6 +43,10 @@ const invalidate = batch(100, false); // job updates often, so job lists refresh at most every few seconds. const invalidateJobs = batch(5_000, true); +function isRepoInfo(key: Key): boolean { + return key[0] === "repo" && key[2] === "info"; +} + function isJobs(key: Key): boolean { return key[0] === "repo" && key[2] === "jobs"; } @@ -104,6 +108,14 @@ export const createEventEmittersOnce = once(async (): Promise => { listen("notifications_changed", () => { invalidate([keys.notifications()]); }), + // Seeding or unseeding with the `rad` CLI or another instance of the app + // changes the repository list and each repository's seeded state. + listen("policies_changed", () => { + invalidate([keys.repos()]); + void queryClient.invalidateQueries({ + predicate: query => isRepoInfo(query.queryKey as Key), + }); + }), // The backend may have missed events while it wasn't subscribed. listen("ref_resync", () => { clearEntityCaches();