add emit event for the watcher

This commit is contained in:
Nikita Galaiko 2023-07-04 09:24:32 +02:00
parent 6473751a60
commit 06b760d998
5 changed files with 76 additions and 104 deletions

View File

@ -20,7 +20,7 @@ impl Sender {
}
}
#[derive(Debug)]
#[derive(Debug, PartialEq, Eq)]
pub struct Event {
name: String,
payload: serde_json::Value,

View File

@ -1,6 +1,6 @@
use std::{fmt::Display, path, time};
use crate::{bookmarks, deltas, sessions};
use crate::{bookmarks, deltas, events, sessions};
#[derive(Debug, PartialEq)]
pub enum Event {
@ -11,10 +11,6 @@ pub enum Event {
FileChange(path::PathBuf),
GitFileChange(path::PathBuf),
GitIndexChange,
GitActivity,
GitFetch,
GitHeadChange(String),
ProjectFileChange(path::PathBuf),
@ -24,21 +20,20 @@ pub enum Event {
Bookmark(bookmarks::Bookmark),
IndexAll,
Emit(events::Event),
}
impl Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Event::Emit(event) => write!(f, "Emit({:?})", event),
Event::IndexAll => write!(f, "IndexAll"),
Event::Tick(ts) => write!(f, "Tick({:?})", ts),
Event::FetchGitbutlerData(ts) => write!(f, "FetchGitbutlerData({:?})", ts),
Event::Flush(session) => write!(f, "Flush({})", session.id),
Event::GitFetch => write!(f, "GitFetch"),
Event::FileChange(_) => write!(f, "FileChange"),
Event::GitFileChange(_) => write!(f, "GitFileChange"),
Event::GitIndexChange => write!(f, "GitIndexChange"),
Event::GitActivity => write!(f, "GitActivity"),
Event::GitHeadChange(head) => write!(f, "GitHeadChange({})", head),
Event::ProjectFileChange(path) => write!(f, "ProjectFileChange({})", path.display()),
Event::Session(session) => write!(f, "Session({})", session.id),
Event::Bookmark(_) => write!(f, "Bookmark"),

View File

@ -1,6 +1,6 @@
use anyhow::{Context, Result};
use crate::{project_repository, projects};
use crate::{events as app_events, project_repository, projects};
use super::events;
@ -35,24 +35,33 @@ impl Handler {
match path.as_ref().to_str().unwrap() {
"FETCH_HEAD" => {
log::info!("{}: git fetch", project.id);
Ok(vec![events::Event::GitFetch])
Ok(vec![events::Event::Emit(app_events::Event::git_fetch(
&project.id,
))])
}
"logs/HEAD" => {
log::info!("{}: git activity", project.id);
Ok(vec![events::Event::GitActivity])
Ok(vec![events::Event::Emit(app_events::Event::git_activity(
&project.id,
))])
}
"HEAD" => {
log::info!("{}: git head changed", project.id);
let head_ref = project_repository.get_head()?;
if let Some(head) = head_ref.name() {
Ok(vec![events::Event::GitHeadChange(head.to_string())])
Ok(vec![events::Event::Emit(app_events::Event::git_head(
&project.id,
head,
))])
} else {
Ok(vec![])
}
}
"index" => {
log::info!("{}: git index changed", project.id);
Ok(vec![events::Event::GitIndexChange])
Ok(vec![events::Event::Emit(app_events::Event::git_index(
&project.id,
))])
}
_ => Ok(vec![]),
}

View File

@ -20,7 +20,6 @@ pub struct Handler {
sessions_database: sessions::Database,
deltas_database: deltas::Database,
bookmarks_database: bookmarks::Database,
events_sender: app_events::Sender,
}
impl Handler {
@ -35,7 +34,6 @@ impl Handler {
sessions_database: sessions::Database,
deltas_database: deltas::Database,
bookmarks_database: bookmarks::Database,
events_sender: app_events::Sender,
) -> Self {
Self {
local_data_dir,
@ -47,7 +45,6 @@ impl Handler {
sessions_database,
deltas_database,
bookmarks_database,
events_sender,
}
}
@ -60,7 +57,12 @@ impl Handler {
self.deltas_database
.insert(&self.project_id, session_id, file_path, deltas)
.context("failed to insert deltas into database")?;
Ok(vec![])
Ok(vec![events::Event::Emit(app_events::Event::deltas(
&self.project_id,
session_id,
deltas,
path::Path::new(file_path),
))])
}
pub fn index_file(
@ -72,17 +74,25 @@ impl Handler {
self.files_database
.insert(&self.project_id, session_id, file_path, content)
.context("failed to insert file into database")?;
Ok(vec![])
Ok(vec![events::Event::Emit(app_events::Event::file(
&self.project_id,
session_id,
file_path,
content,
))])
}
pub fn index_bookmark(&self, bookmark: &bookmarks::Bookmark) -> Result<Vec<events::Event>> {
let updated = self.bookmarks_database.upsert(bookmark)?;
self.deltas_searcher.index_bookmark(bookmark)?;
if let Some(updated) = updated {
self.events_sender
.send(app_events::Event::bookmark(&self.project_id, &updated))?;
Ok(vec![events::Event::Emit(app_events::Event::bookmark(
&self.project_id,
&updated,
))])
} else {
Ok(vec![])
}
Ok(vec![])
}
pub fn reindex(&self) -> Result<Vec<events::Event>> {
@ -136,7 +146,10 @@ impl Handler {
.insert(&self.project_id, &[session])
.context("failed to insert session into database")?;
let mut events: Vec<events::Event> = vec![];
let mut events: Vec<events::Event> = vec![events::Event::Emit(app_events::Event::session(
&self.project_id,
session,
))];
for (file_path, content) in session_reader
.files(None)

View File

@ -52,7 +52,7 @@ impl<'handler> Handler {
) -> Self {
Self {
project_id: project_id.clone(),
events_sender: events_sender.clone(),
events_sender,
file_change_handler: file_change::Handler::new(),
project_file_handler: project_file_change::Handler::new(
@ -97,7 +97,6 @@ impl<'handler> Handler {
sessions_database,
deltas_database,
bookmarks_database,
events_sender,
),
}
}
@ -112,48 +111,28 @@ impl<'handler> Handler {
.file_change_handler
.handle(path.clone())
.with_context(|| format!("failed to handle file change event: {:?}", path)),
events::Event::ProjectFileChange(path) => self
.project_file_handler
.handle(path.clone())
.with_context(|| format!("failed to handle project file change event: {:?}", path)),
events::Event::GitFileChange(path) => self
.git_file_change_handler
.handle(path)
.context("failed to handle git file change event"),
events::Event::GitActivity => {
self.events_sender
.send(app_events::Event::git_activity(&self.project_id))
.context("failed to send git activity event")?;
Ok(vec![])
}
events::Event::GitHeadChange(head) => {
self.events_sender
.send(app_events::Event::git_head(&self.project_id, &head))
.context("failed to send git head event")?;
Ok(vec![])
}
events::Event::GitFetch => {
self.events_sender
.send(app_events::Event::git_fetch(&self.project_id))
.context("failed to send git fetch event")?;
Ok(vec![])
}
events::Event::GitIndexChange => {
self.events_sender
.send(app_events::Event::git_index(&self.project_id))
.context("failed to send git index event")?;
Ok(vec![])
}
events::Event::FetchGitbutlerData(tick) => self
.fetch_gitbutler_handler
.handle(tick)
.context("failed to fetch gitbutler data"),
events::Event::Tick(tick) => {
let one = match self.check_current_session_handler.handle(tick) {
Ok(events) => events,
Err(err) => {
log::error!(
"{}: failed to check current session: {:#?}",
"{}: failed to check current session: {:#}",
self.project_id,
err
);
@ -165,7 +144,7 @@ impl<'handler> Handler {
Ok(events) => events,
Err(err) => {
log::error!(
"{}: failed to fetch project data: {:#?}",
"{}: failed to fetch project data: {:#}",
self.project_id,
err
);
@ -177,7 +156,7 @@ impl<'handler> Handler {
Ok(events) => events,
Err(err) => {
log::error!(
"{}: failed to fetch gitbutler data: {:#?}",
"{}: failed to fetch gitbutler data: {:#}",
self.project_id,
err
);
@ -191,64 +170,40 @@ impl<'handler> Handler {
.chain(three.into_iter())
.collect())
}
events::Event::Flush(session) => self
.flush_session_handler
.handle(&session)
.context("failed to handle flush session event"),
events::Event::SessionFile((session_id, file_path, contents)) => {
let file_events = self
.index_handler
.index_file(&session_id, file_path.to_str().unwrap(), &contents)
.context("failed to index file")?;
self.events_sender
.send(app_events::Event::file(
&self.project_id,
&session_id,
file_path.to_str().unwrap(),
&contents,
))
.context("failed to send file event")?;
Ok(file_events)
}
events::Event::Session(session) => {
let session_events = self
.index_handler
.index_session(&session)
.context("failed to index session")?;
self.events_sender
.send(app_events::Event::session(&self.project_id, &session))
.context("failed to send session event")?;
Ok(session_events)
}
events::Event::SessionDelta((session_id, path, delta)) => {
let deltas = vec![delta];
let delta_events = self
.index_handler
.index_deltas(&session_id, path.to_str().unwrap(), &deltas)
.context("failed to index deltas")?;
self.events_sender
.send(app_events::Event::deltas(
&self.project_id,
&session_id,
&deltas,
&path,
))
.context("failed to send deltas event")?;
Ok(delta_events)
}
events::Event::Bookmark(bookmark) => {
let bookmark_events = self
.index_handler
.index_bookmark(&bookmark)
.context("failed to index bookmark")?;
self.events_sender
.send(app_events::Event::bookmark(&self.project_id, &bookmark))
.context("failed to send bookmark event")?;
Ok(bookmark_events)
}
events::Event::SessionFile((session_id, file_path, contents)) => self
.index_handler
.index_file(&session_id, file_path.to_str().unwrap(), &contents)
.context("failed to index file"),
events::Event::Session(session) => self
.index_handler
.index_session(&session)
.context("failed to index session"),
events::Event::SessionDelta((session_id, path, delta)) => self
.index_handler
.index_deltas(&session_id, path.to_str().unwrap(), &vec![delta])
.context("failed to index deltas"),
events::Event::Bookmark(bookmark) => self
.index_handler
.index_bookmark(&bookmark)
.context("failed to index bookmark"),
events::Event::IndexAll => self.index_handler.reindex(),
events::Event::Emit(event) => {
self.events_sender
.send(event)
.context("failed to send event")?;
Ok(vec![])
}
}
}
}