mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 09:28:21 +00:00
cleanup
This commit is contained in:
parent
b0b9fb570d
commit
d7001a88a3
2 changed files with 78 additions and 74 deletions
71
asyncgit/src/diff.rs
Normal file
71
asyncgit/src/diff.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use crate::{sync, Diff};
|
||||
use crossbeam_channel::Sender;
|
||||
use std::{
|
||||
collections::hash_map::DefaultHasher,
|
||||
hash::{Hash, Hasher},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
#[derive(Default, Hash)]
|
||||
struct DiffRequest(String, bool);
|
||||
|
||||
struct Request<R, A>(R, Option<A>);
|
||||
|
||||
pub struct AsyncDiff {
|
||||
current: Arc<Mutex<Request<u64, Diff>>>,
|
||||
sender: Sender<()>,
|
||||
}
|
||||
|
||||
impl AsyncDiff {
|
||||
///
|
||||
pub fn new(sender: Sender<()>) -> Self {
|
||||
Self {
|
||||
current: Arc::new(Mutex::new(Request(0, None))),
|
||||
sender,
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn request(
|
||||
&mut self,
|
||||
file_path: String,
|
||||
stage: bool,
|
||||
) -> Option<Diff> {
|
||||
let request = DiffRequest(file_path.clone(), stage);
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
request.hash(&mut hasher);
|
||||
let hash = hasher.finish();
|
||||
|
||||
{
|
||||
let mut current = self.current.lock().unwrap();
|
||||
|
||||
if current.0 == hash {
|
||||
return current.1.clone();
|
||||
}
|
||||
|
||||
current.0 = hash;
|
||||
current.1 = None;
|
||||
}
|
||||
|
||||
let arc_clone = Arc::clone(&self.current);
|
||||
let sender = self.sender.clone();
|
||||
rayon_core::spawn(move || {
|
||||
let res = sync::diff::get_diff(file_path.clone(), stage);
|
||||
let mut notify = false;
|
||||
{
|
||||
let mut current = arc_clone.lock().unwrap();
|
||||
if current.0 == hash {
|
||||
current.1 = Some(res);
|
||||
notify = true;
|
||||
}
|
||||
}
|
||||
|
||||
if notify {
|
||||
sender.send(()).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
@ -1,77 +1,10 @@
|
|||
mod diff;
|
||||
pub mod sync;
|
||||
|
||||
use crossbeam_channel::Sender;
|
||||
use std::{
|
||||
collections::hash_map::DefaultHasher,
|
||||
hash::{Hash, Hasher},
|
||||
sync::{Arc, Mutex},
|
||||
pub use crate::{
|
||||
diff::AsyncDiff,
|
||||
sync::{
|
||||
diff::{Diff, DiffLine, DiffLineType},
|
||||
status::{StatusItem, StatusItemType, StatusType},
|
||||
},
|
||||
};
|
||||
use sync::diff::get_diff;
|
||||
pub use sync::{
|
||||
diff::{Diff, DiffLine, DiffLineType},
|
||||
status::{StatusItem, StatusItemType, StatusType},
|
||||
};
|
||||
|
||||
#[derive(Default, Hash)]
|
||||
struct DiffRequest(String, bool);
|
||||
|
||||
struct Request<R, A>(R, Option<A>);
|
||||
|
||||
pub struct AsyncDiff {
|
||||
current: Arc<Mutex<Request<u64, Diff>>>,
|
||||
sender: Sender<()>,
|
||||
}
|
||||
|
||||
impl AsyncDiff {
|
||||
///
|
||||
pub fn new(sender: Sender<()>) -> Self {
|
||||
Self {
|
||||
current: Arc::new(Mutex::new(Request(0, None))),
|
||||
sender,
|
||||
}
|
||||
}
|
||||
|
||||
///
|
||||
pub fn request(
|
||||
&mut self,
|
||||
file_path: String,
|
||||
stage: bool,
|
||||
) -> Option<Diff> {
|
||||
let request = DiffRequest(file_path.clone(), stage);
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
request.hash(&mut hasher);
|
||||
let hash = hasher.finish();
|
||||
|
||||
{
|
||||
let mut current = self.current.lock().unwrap();
|
||||
|
||||
if current.0 == hash {
|
||||
return current.1.clone();
|
||||
}
|
||||
|
||||
current.0 = hash;
|
||||
current.1 = None;
|
||||
}
|
||||
|
||||
let arc_clone = Arc::clone(&self.current);
|
||||
let sender = self.sender.clone();
|
||||
rayon_core::spawn(move || {
|
||||
let res = get_diff(file_path.clone(), stage);
|
||||
let mut notify = false;
|
||||
{
|
||||
let mut current = arc_clone.lock().unwrap();
|
||||
if current.0 == hash {
|
||||
current.1 = Some(res);
|
||||
notify = true;
|
||||
}
|
||||
}
|
||||
|
||||
if notify {
|
||||
sender.send(()).unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue