Skip to content

Commit

Permalink
make update_progress simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst committed Sep 3, 2023
1 parent 52dfefe commit 42043bd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.PHONY: debug build-release release-linux-musl test clippy clippy-pedantic install install-debug

ARGS=-l
# ARGS=-l -d ~/code/extern/kubernetes
# ARGS=-l -d ~/code/extern/linux
# ARGS=-l -d ~/code/git-bare-test.git -w ~/code/git-bare-test

Expand Down
21 changes: 15 additions & 6 deletions asyncgit/src/asyncjob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ use crossbeam_channel::Sender;
use std::sync::{Arc, Mutex, RwLock};

/// Passed to `AsyncJob::run` allowing sending intermediate progress notifications
pub struct RunParams<T: Copy + Send, P: Clone + Send + Sync> {
pub struct RunParams<
T: Copy + Send,
P: Clone + Send + Sync + PartialEq,
> {
sender: Sender<T>,
progress: Arc<RwLock<P>>,
}

impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
impl<T: Copy + Send, P: Clone + Send + Sync + PartialEq>
RunParams<T, P>
{
/// send an intermediate update notification.
/// do not confuse this with the return value of `run`.
/// `send` should only be used about progress notifications
Expand All @@ -24,9 +29,13 @@ impl<T: Copy + Send, P: Clone + Send + Sync> RunParams<T, P> {
}

/// set the current progress
pub fn set_progress(&self, p: P) -> Result<()> {
*(self.progress.write()?) = p;
Ok(())
pub fn set_progress(&self, p: P) -> Result<bool> {
Ok(if *self.progress.read()? == p {
false
} else {
*(self.progress.write()?) = p;
true
})
}
}

Expand All @@ -35,7 +44,7 @@ pub trait AsyncJob: Send + Sync + Clone {
/// defines what notification type is used to communicate outside
type Notification: Copy + Send;
/// type of progress
type Progress: Clone + Default + Send + Sync;
type Progress: Clone + Default + Send + Sync + PartialEq;

/// can run a synchronous time intensive task.
/// the returned notification is used to tell interested parties
Expand Down
29 changes: 14 additions & 15 deletions asyncgit/src/filter_commits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,14 @@ impl AsyncCommitFilterJob {
let total_amount = commits.len();
let start = Instant::now();

let mut progress = ProgressPercent::new(0, total_amount);

let result = commits
.into_iter()
.enumerate()
.filter_map(|(idx, c)| {
let new_progress =
ProgressPercent::new(idx, total_amount);

if new_progress != progress {
Self::update_progress(params, new_progress);
progress = new_progress;
}
Self::update_progress(
params,
ProgressPercent::new(idx, total_amount),
);

(*self.filter)(repo, &c)
.ok()
Expand All @@ -115,12 +110,16 @@ impl AsyncCommitFilterJob {
params: &RunParams<AsyncGitNotification, ProgressPercent>,
new_progress: ProgressPercent,
) {
if let Err(e) = params.set_progress(new_progress) {
log::error!("progress error: {e}");
} else if let Err(e) =
params.send(AsyncGitNotification::CommitFilter)
{
log::error!("send error: {e}");
match params.set_progress(new_progress) {
Err(e) => log::error!("progress error: {e}"),
Ok(result) if result => {
if let Err(e) =
params.send(AsyncGitNotification::CommitFilter)
{
log::error!("send error: {e}");
}
}
_ => (),
}
}
}
Expand Down

0 comments on commit 42043bd

Please sign in to comment.