mirror of
https://github.com/gitbutlerapp/gitbutler.git
synced 2024-12-02 07:53:55 +03:00
Add event_sender to App and update function signatures
Implemented the addition of an event_sender property to the App struct which is used by the start_watcher and init functions. Updated the new, init, and start_watcher functions to work with the new event_sender, and removed the now unnecessary events argument. Changes: - Added event_sender property to App struct. - Added event_sender argument to new function. - Removed events argument from init_project, init, and start_watcher functions. - Updated start_watcher and init functions to use the new event_sender.
This commit is contained in:
parent
2856d51979
commit
a47b0dda1f
@ -15,6 +15,7 @@ pub struct App {
|
||||
users_storage: users::Storage,
|
||||
|
||||
deltas_searcher: search::Deltas,
|
||||
events_sender: events::Sender,
|
||||
|
||||
stop_watchers: sync::Arc<sync::Mutex<HashMap<String, Sender<()>>>>,
|
||||
}
|
||||
@ -30,12 +31,16 @@ pub enum AddProjectError {
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new<P: AsRef<std::path::Path>>(local_data_dir: P) -> Result<Self> {
|
||||
pub fn new<P: AsRef<std::path::Path>>(
|
||||
local_data_dir: P,
|
||||
event_sender: events::Sender,
|
||||
) -> Result<Self> {
|
||||
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<events::Event>,
|
||||
) -> 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<events::Event>) -> 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<events::Event>,
|
||||
) -> 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<events::Event>,
|
||||
) -> Result<projects::Project, AddProjectError> {
|
||||
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)
|
||||
)?;
|
||||
|
@ -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<app_events::Event>,
|
||||
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<app_events::Event>,
|
||||
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![])
|
||||
|
@ -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<app_events::Event>,
|
||||
stop: crossbeam_channel::Receiver<()>,
|
||||
events_sender: events::Sender,
|
||||
) -> Result<Self> {
|
||||
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,
|
||||
})
|
||||
|
@ -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,
|
||||
|
@ -226,10 +226,7 @@ async fn update_project(
|
||||
#[tauri::command(async)]
|
||||
async fn add_project(handle: tauri::AppHandle, path: &str) -> Result<projects::Project, Error> {
|
||||
let app = handle.state::<app::App>();
|
||||
|
||||
let (tx, rx) = std::sync::mpsc::channel::<events::Event>();
|
||||
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::<events::Event>();
|
||||
|
||||
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<events::Event>) {
|
||||
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<tauri::Window> {
|
||||
handle.get_window("main")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user