diff --git a/src-tauri/src/app/app.rs b/src-tauri/src/app/app.rs index 83bbbee15..d2bc73277 100644 --- a/src-tauri/src/app/app.rs +++ b/src-tauri/src/app/app.rs @@ -15,6 +15,7 @@ pub struct App { users_storage: users::Storage, deltas_searcher: search::Deltas, + events_sender: events::Sender, stop_watchers: sync::Arc>>>, } @@ -30,12 +31,16 @@ pub enum AddProjectError { } impl App { - pub fn new>(local_data_dir: P) -> Result { + pub fn new>( + local_data_dir: P, + event_sender: events::Sender, + ) -> Result { let local_data_dir = local_data_dir.as_ref(); let storage = storage::Storage::from_path(local_data_dir.clone()); let deltas_searcher = search::Deltas::at(local_data_dir.clone()).context("failed to open deltas searcher")?; Ok(Self { + events_sender: event_sender, local_data_dir: local_data_dir.to_path_buf(), projects_storage: projects::Storage::new(storage.clone()), users_storage: users::Storage::new(storage.clone()), @@ -58,9 +63,8 @@ impl App { pub fn init_project( &self, project: &projects::Project, - events: std::sync::mpsc::Sender, ) -> Result<()> { - self.start_watcher(&project, events.clone()) + self.start_watcher(&project) .with_context(|| { format!("failed to start watcher for project {}", project.id.clone()) })?; @@ -68,13 +72,13 @@ impl App { Ok(()) } - pub fn init(&self, events: std::sync::mpsc::Sender) -> Result<()> { + pub fn init(&self) -> Result<()> { for project in self .projects_storage .list_projects() .with_context(|| "failed to list projects")? { - if let Err(e) = self.init_project(&project, events.clone()) { + if let Err(e) = self.init_project(&project) { log::error!("failed to init project {}: {:#}", project.id, e); } @@ -110,13 +114,13 @@ impl App { fn start_watcher( &self, project: &projects::Project, - events: std::sync::mpsc::Sender, ) -> Result<()> { let project = project.clone(); let users_storage = self.users_storage.clone(); let projects_storage = self.projects_storage.clone(); let local_data_dir = self.local_data_dir.clone(); let deltas_searcher = self.deltas_searcher.clone(); + let events_sender = self.events_sender.clone(); let (stop_tx, stop_rx) = bounded(1); self.stop_watchers @@ -140,8 +144,8 @@ impl App { projects_storage, &gb_repository, deltas_searcher, - events, stop_rx, + events_sender, ) .expect("failed to create watcher"); @@ -175,7 +179,6 @@ impl App { pub fn add_project( &self, path: &str, - events: std::sync::mpsc::Sender, ) -> Result { let all_projects = self.projects_storage.list_projects().map_err(|e| AddProjectError::Other(e) @@ -196,7 +199,7 @@ impl App { AddProjectError::Other(e) )?; - self.init_project(&project, events.clone()) + self.init_project(&project) .context("failed to init project").map_err(|e| AddProjectError::Other(e) )?; diff --git a/src-tauri/src/app/watcher/handlers/mod.rs b/src-tauri/src/app/watcher/handlers/mod.rs index b0ef50d00..ce453e821 100644 --- a/src-tauri/src/app/watcher/handlers/mod.rs +++ b/src-tauri/src/app/watcher/handlers/mod.rs @@ -11,8 +11,6 @@ mod check_current_session_tests; #[cfg(test)] mod project_file_change_tests; -use std::sync; - use anyhow::{Context, Result}; use crate::{app::gb_repository, events as app_events, projects, search}; @@ -31,7 +29,7 @@ pub struct Handler<'handler> { chech_fetch_project_handler: check_fetch_project::Handler, searcher: search::Deltas, - events: sync::mpsc::Sender, + events_sender: app_events::Sender, } impl<'handler> Handler<'handler> { @@ -40,9 +38,10 @@ impl<'handler> Handler<'handler> { project_store: projects::Storage, gb_repository: &'handler gb_repository::Repository, searcher: search::Deltas, - events: sync::mpsc::Sender, + events_sender: app_events::Sender, ) -> Self { Self { + events_sender, gb_repository, file_change_handler: file_change::Handler::new(), @@ -71,7 +70,6 @@ impl<'handler> Handler<'handler> { ), searcher, - events, } } @@ -86,13 +84,13 @@ impl<'handler> Handler<'handler> { .handle(path.clone()) .with_context(|| format!("failed to handle project file change event: {:?}", path)), events::Event::Session((project, session)) => { - self.events + self.events_sender .send(app_events::Event::session(&project, &session)) .context("failed to send session event")?; Ok(vec![]) } events::Event::Deltas((project, session, path, deltas)) => { - self.events + self.events_sender .send(app_events::Event::detlas( &project, &session, &deltas, &path, )) @@ -104,19 +102,19 @@ impl<'handler> Handler<'handler> { .handle(path) .context("failed to handle git file change event"), events::Event::GitActivity(project) => { - self.events + self.events_sender .send(app_events::Event::git_activity(&project)) .context("failed to send git activity event")?; Ok(vec![]) } events::Event::GitHeadChange((project, head)) => { - self.events + self.events_sender .send(app_events::Event::git_head(&project, &head)) .context("failed to send git head event")?; Ok(vec![]) } events::Event::GitIndexChange(project) => { - self.events + self.events_sender .send(app_events::Event::git_index(&project)) .context("failed to send git index event")?; Ok(vec![]) diff --git a/src-tauri/src/app/watcher/watcher.rs b/src-tauri/src/app/watcher/watcher.rs index 07de5984a..2eb1d6864 100644 --- a/src-tauri/src/app/watcher/watcher.rs +++ b/src-tauri/src/app/watcher/watcher.rs @@ -1,9 +1,7 @@ -use std::sync; - use anyhow::Result; use crossbeam_channel::{select, unbounded}; -use crate::{app::gb_repository, events as app_events, projects, search}; +use crate::{app::gb_repository, events, projects, search}; use super::{dispatchers, handlers}; @@ -20,8 +18,8 @@ impl<'watcher> Watcher<'watcher> { project_store: projects::Storage, gb_repository: &'watcher gb_repository::Repository, deltas_searcher: search::Deltas, - events: sync::mpsc::Sender, stop: crossbeam_channel::Receiver<()>, + events_sender: events::Sender, ) -> Result { Ok(Self { project_id: project.id.clone(), @@ -31,7 +29,7 @@ impl<'watcher> Watcher<'watcher> { project_store, gb_repository, deltas_searcher, - events, + events_sender, ), stop, }) diff --git a/src-tauri/src/events.rs b/src-tauri/src/events.rs index 7e819623d..7bb8d20ff 100644 --- a/src-tauri/src/events.rs +++ b/src-tauri/src/events.rs @@ -1,8 +1,28 @@ +use anyhow::{Context, Result}; +use tauri::Manager; + use crate::{ app::{deltas, sessions}, projects, }; +#[derive(Clone)] +pub struct Sender { + app_handle: tauri::AppHandle, +} + +impl Sender { + pub fn new(app_handle: tauri::AppHandle) -> Self { + Self { app_handle } + } + + pub fn send(&self, event: Event) -> Result<()> { + self.app_handle + .emit_all(&event.name, Some(event.payload)) + .context("emit event") + } +} + #[derive(Debug)] pub struct Event { pub name: String, diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 283e709ec..e28c6f70b 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -226,10 +226,7 @@ async fn update_project( #[tauri::command(async)] async fn add_project(handle: tauri::AppHandle, path: &str) -> Result { let app = handle.state::(); - - let (tx, rx) = std::sync::mpsc::channel::(); - let project = app.add_project(path, tx)?; - watch_events(handle, rx); + let project = app.add_project(path)?; Ok(project) } @@ -497,7 +494,10 @@ fn main() { window.open_devtools(); let app: app::App = - app::App::new(tauri_app.path_resolver().app_local_data_dir().unwrap()) + app::App::new( + tauri_app.path_resolver().app_local_data_dir().unwrap(), + events::Sender::new(tauri_app.handle()) + ) .expect("failed to initialize app"); // TODO: REMOVE THIS @@ -599,32 +599,14 @@ fn init(app_handle: tauri::AppHandle) -> Result<()> { sentry::configure_scope(|scope| scope.set_user(Some(user.clone().into()))) } - let (events_tx, events_rx) = std::sync::mpsc::channel::(); - app.start_pty_server() .context("failed to start pty server")?; - app.init(events_tx).context("failed to init app")?; - - watch_events(app_handle, events_rx); + app.init().context("failed to init app")?; Ok(()) } -fn watch_events(handle: tauri::AppHandle, rx: std::sync::mpsc::Receiver) { - tauri::async_runtime::spawn_blocking(move || { - while let Ok(event) = rx.recv() { - if let Some(window) = handle.get_window("main") { - log::info!("Emitting event: {}", event.name); - match window.emit(&event.name, event.payload) { - Err(e) => log::error!("Failed to emit event: {:#}", e), - _ => {} - } - } - } - }); -} - fn get_window(handle: &tauri::AppHandle) -> Option { handle.get_window("main") }