diff --git a/asyncgit/src/asyncjob/mod.rs b/asyncgit/src/asyncjob/mod.rs index a98565e6..8317ba65 100644 --- a/asyncgit/src/asyncjob/mod.rs +++ b/asyncgit/src/asyncjob/mod.rs @@ -8,31 +8,30 @@ use std::sync::{Arc, Mutex}; /// trait that defines an async task we can run on a threadpool pub trait AsyncJob: Send + Sync + Clone { + /// defines what notification to send after finish running job + type Notification: Copy + Send + 'static; + /// can run a synchronous time intensive task - fn run(&mut self); + fn run(&mut self) -> Self::Notification; } /// Abstraction for a FIFO task queue that will only queue up **one** `next` job. /// It keeps overwriting the next job until it is actually taken to be processed #[derive(Debug, Clone)] -pub struct AsyncSingleJob { +pub struct AsyncSingleJob { next: Arc>>, last: Arc>>, - sender: Sender, + sender: Sender, pending: Arc>, - notification: T, } -impl - AsyncSingleJob -{ +impl AsyncSingleJob { /// - pub fn new(sender: Sender, value: T) -> Self { + pub fn new(sender: Sender) -> Self { Self { next: Arc::new(Mutex::new(None)), last: Arc::new(Mutex::new(None)), pending: Arc::new(Mutex::new(())), - notification: value, sender, } } @@ -96,13 +95,13 @@ impl { let _pending = self.pending.lock()?; - task.run(); + let notification = task.run(); if let Ok(mut last) = self.last.lock() { *last = Some(task); } - self.sender.send(self.notification)?; + self.sender.send(notification)?; } self.check_for_job(); @@ -143,8 +142,12 @@ mod test { value_to_add: u32, } + type TestNotificaton = (); + impl AsyncJob for TestJob { - fn run(&mut self) { + type Notification = TestNotificaton; + + fn run(&mut self) -> Self::Notification { println!("[job] wait"); while !self.finish.load(Ordering::SeqCst) { @@ -161,17 +164,17 @@ mod test { self.v.fetch_add(self.value_to_add, Ordering::SeqCst); println!("[job] value: {}", res); + + () } } - type Notificaton = (); - #[test] fn test_overwrite() { let (sender, receiver) = unbounded(); - let mut job: AsyncSingleJob = - AsyncSingleJob::new(sender, ()); + let mut job: AsyncSingleJob = + AsyncSingleJob::new(sender); let task = TestJob { v: Arc::new(AtomicU32::new(1)), @@ -199,7 +202,7 @@ mod test { ); } - fn wait_for_job(job: &AsyncSingleJob) { + fn wait_for_job(job: &AsyncSingleJob) { while job.is_pending() { thread::sleep(Duration::from_millis(10)); } @@ -209,8 +212,8 @@ mod test { fn test_cancel() { let (sender, receiver) = unbounded(); - let mut job: AsyncSingleJob = - AsyncSingleJob::new(sender, ()); + let mut job: AsyncSingleJob = + AsyncSingleJob::new(sender); let task = TestJob { v: Arc::new(AtomicU32::new(1)), diff --git a/asyncgit/src/lib.rs b/asyncgit/src/lib.rs index 6c356e2f..586470f3 100644 --- a/asyncgit/src/lib.rs +++ b/asyncgit/src/lib.rs @@ -83,6 +83,8 @@ pub enum AsyncGitNotification { Fetch, /// Blame, + /// + RemoteTags, } /// current working directory `./` diff --git a/asyncgit/src/remote_tags.rs b/asyncgit/src/remote_tags.rs index 524fea8a..b3da2964 100644 --- a/asyncgit/src/remote_tags.rs +++ b/asyncgit/src/remote_tags.rs @@ -5,7 +5,7 @@ use crate::{ error::Result, sync::cred::BasicAuthCredential, sync::remotes::{get_default_remote, tags_missing_remote}, - CWD, + AsyncGitNotification, CWD, }; use std::sync::{Arc, Mutex}; @@ -50,7 +50,9 @@ impl AsyncRemoteTagsJob { } impl AsyncJob for AsyncRemoteTagsJob { - fn run(&mut self) { + type Notification = AsyncGitNotification; + + fn run(&mut self) -> Self::Notification { if let Ok(mut state) = self.state.lock() { *state = state.take().map(|state| match state { JobState::Request(basic_credential) => { @@ -70,5 +72,7 @@ impl AsyncJob for AsyncRemoteTagsJob { } }); } + + AsyncGitNotification::RemoteTags } } diff --git a/src/app.rs b/src/app.rs index 50defa26..a5506e8f 100644 --- a/src/app.rs +++ b/src/app.rs @@ -179,7 +179,7 @@ impl App { ), tags_popup: TagListComponent::new( &queue, - sender_app, + sender, theme.clone(), key_config.clone(), ), diff --git a/src/components/syntax_text.rs b/src/components/syntax_text.rs index 7a5dddc7..f183ea20 100644 --- a/src/components/syntax_text.rs +++ b/src/components/syntax_text.rs @@ -32,8 +32,7 @@ use tui::{ pub struct SyntaxTextComponent { current_file: Option<(String, Either)>, - async_highlighting: - AsyncSingleJob, + async_highlighting: AsyncSingleJob, key_config: SharedKeyConfig, paragraph_state: Cell, focused: bool, @@ -48,10 +47,7 @@ impl SyntaxTextComponent { theme: SharedTheme, ) -> Self { Self { - async_highlighting: AsyncSingleJob::new( - sender.clone(), - AsyncAppNotification::SyntaxHighlighting, - ), + async_highlighting: AsyncSingleJob::new(sender.clone()), current_file: None, paragraph_state: Cell::new(ParagraphState::default()), focused: false, diff --git a/src/components/taglist.rs b/src/components/taglist.rs index 7512a9fb..ffa693a2 100644 --- a/src/components/taglist.rs +++ b/src/components/taglist.rs @@ -8,7 +8,7 @@ use crate::{ queue::{Action, InternalEvent, Queue}, strings, ui::{self, Size}, - AsyncAppNotification, AsyncNotification, + AsyncNotification, }; use anyhow::Result; use asyncgit::{ @@ -46,8 +46,7 @@ pub struct TagListComponent { current_height: std::cell::Cell, missing_remote_tags: Option>, basic_credential: Option, - async_remote_tags: - AsyncSingleJob, + async_remote_tags: AsyncSingleJob, key_config: SharedKeyConfig, } @@ -251,7 +250,7 @@ impl Component for TagListComponent { impl TagListComponent { pub fn new( queue: &Queue, - sender: &Sender, + sender: &Sender, theme: SharedTheme, key_config: SharedKeyConfig, ) -> Self { @@ -264,10 +263,7 @@ impl TagListComponent { current_height: std::cell::Cell::new(0), basic_credential: None, missing_remote_tags: None, - async_remote_tags: AsyncSingleJob::new( - sender.clone(), - AsyncAppNotification::RemoteTags, - ), + async_remote_tags: AsyncSingleJob::new(sender.clone()), key_config, } } @@ -301,7 +297,7 @@ impl TagListComponent { pub fn update(&mut self, ev: AsyncNotification) { if matches!( ev, - AsyncNotification::App(AsyncAppNotification::RemoteTags) + AsyncNotification::Git(AsyncGitNotification::RemoteTags) ) { if let Some(job) = self.async_remote_tags.take_last() { if let Some(Ok(missing_remote_tags)) = job.result() { diff --git a/src/main.rs b/src/main.rs index 6b0bdfab..8e59dd44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,8 +80,6 @@ pub enum QueueEvent { pub enum AsyncAppNotification { /// SyntaxHighlighting, - /// - RemoteTags, } #[derive(Clone, Copy, Debug, PartialEq)] diff --git a/src/ui/syntax_text.rs b/src/ui/syntax_text.rs index 3dcad68c..8e0f71eb 100644 --- a/src/ui/syntax_text.rs +++ b/src/ui/syntax_text.rs @@ -16,6 +16,8 @@ use syntect::{ }; use tui::text::{Span, Spans}; +use crate::AsyncAppNotification; + struct SyntaxLine { items: Vec<(Style, usize, Range)>, } @@ -175,7 +177,9 @@ impl AsyncSyntaxJob { } impl AsyncJob for AsyncSyntaxJob { - fn run(&mut self) { + type Notification = AsyncAppNotification; + + fn run(&mut self) -> Self::Notification { if let Ok(mut state) = self.state.lock() { *state = state.take().map(|state| match state { JobState::Request((content, path)) => { @@ -186,5 +190,7 @@ impl AsyncJob for AsyncSyntaxJob { JobState::Response(res) => JobState::Response(res), }); } + + AsyncAppNotification::SyntaxHighlighting } }