minor refactor

This commit is contained in:
Nikita Galaiko 2023-11-17 10:24:35 +01:00 committed by GitButler
parent 763d346196
commit 3f7cb92598
4 changed files with 78 additions and 97 deletions

View File

@ -53,13 +53,12 @@ pub async fn commit_virtual_branch(
code: Code::Validation,
message: "Malformed ownership".to_string(),
})?;
let result = handle
let oid = handle
.state::<Controller>()
.create_commit(&project_id, &branch_id, message, ownership.as_ref())
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(oid)
}
#[tauri::command(async)]
@ -93,13 +92,12 @@ pub async fn create_virtual_branch(
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let result = handle
let branch_id = handle
.state::<Controller>()
.create_virtual_branch(&project_id, &branch)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(branch_id)
}
#[tauri::command(async)]
@ -113,13 +111,12 @@ pub async fn create_virtual_branch_from_branch(
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let result = handle
let branch_id = handle
.state::<Controller>()
.create_virtual_branch_from_branch(&project_id, &branch)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(branch_id)
}
#[tauri::command(async)]
@ -137,13 +134,12 @@ pub async fn merge_virtual_branch_upstream(
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.merge_virtual_branch_upstream(&project_id, &branch_id)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -189,7 +185,7 @@ pub async fn set_base_branch(
.state::<assets::Proxy>()
.proxy_base_branch(base_branch)
.await;
emit_vbranches(&handle, &project_id);
emit_vbranches(&handle, &project_id).await;
Ok(base_branch)
}
@ -200,13 +196,12 @@ pub async fn update_base_branch(handle: AppHandle, project_id: &str) -> Result<(
code: Code::Validation,
message: "Malformed project id".into(),
})?;
let result = handle
handle
.state::<Controller>()
.update_base_branch(&project_id)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -220,13 +215,12 @@ pub async fn update_virtual_branch(
code: Code::Validation,
message: "Malformed project id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.update_virtual_branch(&project_id, branch)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -244,13 +238,12 @@ pub async fn delete_virtual_branch(
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.delete_virtual_branch(&project_id, &branch_id)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -264,13 +257,12 @@ pub async fn apply_branch(handle: AppHandle, project_id: &str, branch: &str) ->
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.apply_virtual_branch(&project_id, &branch_id)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -288,13 +280,12 @@ pub async fn unapply_branch(
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.unapply_virtual_branch(&project_id, &branch_id)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -312,13 +303,12 @@ pub async fn unapply_ownership(
code: Code::Validation,
message: "Malformed ownership".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.unapply_ownership(&project_id, &ownership)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -337,13 +327,12 @@ pub async fn push_virtual_branch(
code: Code::Validation,
message: "Malformed branch id".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.push_virtual_branch(&project_id, &branch_id, with_force)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -429,13 +418,12 @@ pub async fn reset_virtual_branch(
code: Code::Validation,
message: "Malformed commit oid".to_string(),
})?;
let result = handle
handle
.state::<Controller>()
.reset_virtual_branch(&project_id, &branch_id, target_commit_oid)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
#[tauri::command(async)]
@ -458,13 +446,12 @@ pub async fn cherry_pick_onto_virtual_branch(
code: Code::Validation,
message: "Malformed commit oid".to_string(),
})?;
let result = handle
let oid = handle
.state::<Controller>()
.cherry_pick(&project_id, &branch_id, target_commit_oid)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(oid)
}
#[tauri::command(async)]
@ -487,13 +474,12 @@ pub async fn amend_virtual_branch(
code: Code::Validation,
message: "Malformed ownership".into(),
})?;
let result = handle
let oid = handle
.state::<Controller>()
.amend(&project_id, &branch_id, &ownership)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(oid)
}
#[tauri::command(async)]
@ -536,25 +522,20 @@ pub async fn squash_branch_commit(
code: Code::Validation,
message: "Malformed commit oid".into(),
})?;
let result = handle
handle
.state::<Controller>()
.squash(&project_id, &branch_id, target_commit_oid)
.await
.map_err(Into::into);
emit_vbranches(&handle, &project_id);
result
.await?;
emit_vbranches(&handle, &project_id).await;
Ok(())
}
fn emit_vbranches(handle: &AppHandle, project_id: &projects::ProjectId) {
match futures::executor::block_on(async {
handle
.state::<watcher::Watchers>()
.post(watcher::Event::VirtualBranch(*project_id))
.await
}) {
Ok(()) => {}
Err(error) => {
tracing::error!(?error);
}
async fn emit_vbranches(handle: &AppHandle, project_id: &projects::ProjectId) {
if let Err(error) = handle
.state::<watcher::Watchers>()
.post(watcher::Event::CalculateVirtualBranches(*project_id))
.await
{
tracing::error!(?error);
}
}

View File

@ -31,8 +31,8 @@ pub enum Event {
Emit(events::Event),
Analytics(analytics::Event),
VirtualBranch(ProjectId),
SessionProcessing(ProjectId, path::PathBuf),
CalculateVirtualBranches(ProjectId),
CalculateDeltas(ProjectId, path::PathBuf),
}
impl Event {
@ -51,8 +51,8 @@ impl Event {
| Event::Session(project_id, _)
| Event::SessionFile((project_id, _, _, _))
| Event::SessionDelta((project_id, _, _, _))
| Event::VirtualBranch(project_id)
| Event::SessionProcessing(project_id, _)
| Event::CalculateVirtualBranches(project_id)
| Event::CalculateDeltas(project_id, _)
| Event::PushGitbutlerData(project_id)
| Event::PushProjectToGitbutler(project_id) => project_id,
}
@ -93,8 +93,8 @@ impl Display for Event {
delta.timestamp_ms
)
}
Event::VirtualBranch(pid) => write!(f, "VirtualBranch({})", pid),
Event::SessionProcessing(project_id, path) => {
Event::CalculateVirtualBranches(pid) => write!(f, "VirtualBranch({})", pid),
Event::CalculateDeltas(project_id, path) => {
write!(f, "SessionProcessing({}, {})", project_id, path.display())
}
Event::PushGitbutlerData(pid) => write!(f, "PushGitbutlerData({})", pid),

View File

@ -134,12 +134,12 @@ impl Handler {
))])
}
events::Event::VirtualBranch(project_id) => self
events::Event::CalculateVirtualBranches(project_id) => self
.virtual_branch_handler
.handle(project_id)
.context("failed to handle virtual branch event"),
events::Event::SessionProcessing(project_id, path) => self
events::Event::CalculateDeltas(project_id, path) => self
.session_processing_handler
.handle(path, project_id)
.context(format!(

View File

@ -34,12 +34,12 @@ impl Handler {
project_id: &ProjectId,
) -> Result<Vec<events::Event>> {
let path = path.as_ref().to_path_buf();
let mut events = vec![events::Event::SessionProcessing(*project_id, path)];
let mut events = vec![events::Event::CalculateDeltas(*project_id, path)];
let rate_limiter = self.limit.get_or_init(|| RateLimiter::direct(self.quota));
if rate_limiter.check().is_ok() {
events.push(events::Event::VirtualBranch(*project_id));
events.push(events::Event::CalculateVirtualBranches(*project_id));
}
Ok(events)
}