diff --git a/asyncgit/src/diff.rs b/asyncgit/src/diff.rs new file mode 100644 index 00000000..5a52be3f --- /dev/null +++ b/asyncgit/src/diff.rs @@ -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, Option); + +pub struct AsyncDiff { + current: Arc>>, + 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 { + 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 + } +} diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index 7d721dcf..28d6c397 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -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, Option); - -pub struct AsyncDiff { - current: Arc>>, - 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 { - 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 - } -}