Skip to content

Commit b9e4631

Browse files
author
Stephan Dilly
committed
allow async job to return dynamic notification
1 parent e5688e3 commit b9e4631

File tree

8 files changed

+45
-40
lines changed

8 files changed

+45
-40
lines changed

asyncgit/src/asyncjob/mod.rs

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,30 @@ use std::sync::{Arc, Mutex};
88

99
/// trait that defines an async task we can run on a threadpool
1010
pub trait AsyncJob: Send + Sync + Clone {
11+
/// defines what notification to send after finish running job
12+
type Notification: Copy + Send + 'static;
13+
1114
/// can run a synchronous time intensive task
12-
fn run(&mut self);
15+
fn run(&mut self) -> Self::Notification;
1316
}
1417

1518
/// Abstraction for a FIFO task queue that will only queue up **one** `next` job.
1619
/// It keeps overwriting the next job until it is actually taken to be processed
1720
#[derive(Debug, Clone)]
18-
pub struct AsyncSingleJob<J: AsyncJob, T: Copy + Send + 'static> {
21+
pub struct AsyncSingleJob<J: AsyncJob> {
1922
next: Arc<Mutex<Option<J>>>,
2023
last: Arc<Mutex<Option<J>>>,
21-
sender: Sender<T>,
24+
sender: Sender<J::Notification>,
2225
pending: Arc<Mutex<()>>,
23-
notification: T,
2426
}
2527

26-
impl<J: 'static + AsyncJob, T: Copy + Send + 'static>
27-
AsyncSingleJob<J, T>
28-
{
28+
impl<J: 'static + AsyncJob> AsyncSingleJob<J> {
2929
///
30-
pub fn new(sender: Sender<T>, value: T) -> Self {
30+
pub fn new(sender: Sender<J::Notification>) -> Self {
3131
Self {
3232
next: Arc::new(Mutex::new(None)),
3333
last: Arc::new(Mutex::new(None)),
3434
pending: Arc::new(Mutex::new(())),
35-
notification: value,
3635
sender,
3736
}
3837
}
@@ -96,13 +95,13 @@ impl<J: 'static + AsyncJob, T: Copy + Send + 'static>
9695
{
9796
let _pending = self.pending.lock()?;
9897

99-
task.run();
98+
let notification = task.run();
10099

101100
if let Ok(mut last) = self.last.lock() {
102101
*last = Some(task);
103102
}
104103

105-
self.sender.send(self.notification)?;
104+
self.sender.send(notification)?;
106105
}
107106

108107
self.check_for_job();
@@ -143,8 +142,12 @@ mod test {
143142
value_to_add: u32,
144143
}
145144

145+
type TestNotificaton = ();
146+
146147
impl AsyncJob for TestJob {
147-
fn run(&mut self) {
148+
type Notification = TestNotificaton;
149+
150+
fn run(&mut self) -> Self::Notification {
148151
println!("[job] wait");
149152

150153
while !self.finish.load(Ordering::SeqCst) {
@@ -161,17 +164,17 @@ mod test {
161164
self.v.fetch_add(self.value_to_add, Ordering::SeqCst);
162165

163166
println!("[job] value: {}", res);
167+
168+
()
164169
}
165170
}
166171

167-
type Notificaton = ();
168-
169172
#[test]
170173
fn test_overwrite() {
171174
let (sender, receiver) = unbounded();
172175

173-
let mut job: AsyncSingleJob<TestJob, Notificaton> =
174-
AsyncSingleJob::new(sender, ());
176+
let mut job: AsyncSingleJob<TestJob> =
177+
AsyncSingleJob::new(sender);
175178

176179
let task = TestJob {
177180
v: Arc::new(AtomicU32::new(1)),
@@ -199,7 +202,7 @@ mod test {
199202
);
200203
}
201204

202-
fn wait_for_job(job: &AsyncSingleJob<TestJob, Notificaton>) {
205+
fn wait_for_job(job: &AsyncSingleJob<TestJob>) {
203206
while job.is_pending() {
204207
thread::sleep(Duration::from_millis(10));
205208
}
@@ -209,8 +212,8 @@ mod test {
209212
fn test_cancel() {
210213
let (sender, receiver) = unbounded();
211214

212-
let mut job: AsyncSingleJob<TestJob, Notificaton> =
213-
AsyncSingleJob::new(sender, ());
215+
let mut job: AsyncSingleJob<TestJob> =
216+
AsyncSingleJob::new(sender);
214217

215218
let task = TestJob {
216219
v: Arc::new(AtomicU32::new(1)),

asyncgit/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ pub enum AsyncGitNotification {
8383
Fetch,
8484
///
8585
Blame,
86+
///
87+
RemoteTags,
8688
}
8789

8890
/// current working directory `./`

asyncgit/src/remote_tags.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
error::Result,
66
sync::cred::BasicAuthCredential,
77
sync::remotes::{get_default_remote, tags_missing_remote},
8-
CWD,
8+
AsyncGitNotification, CWD,
99
};
1010

1111
use std::sync::{Arc, Mutex};
@@ -50,7 +50,9 @@ impl AsyncRemoteTagsJob {
5050
}
5151

5252
impl AsyncJob for AsyncRemoteTagsJob {
53-
fn run(&mut self) {
53+
type Notification = AsyncGitNotification;
54+
55+
fn run(&mut self) -> Self::Notification {
5456
if let Ok(mut state) = self.state.lock() {
5557
*state = state.take().map(|state| match state {
5658
JobState::Request(basic_credential) => {
@@ -70,5 +72,7 @@ impl AsyncJob for AsyncRemoteTagsJob {
7072
}
7173
});
7274
}
75+
76+
AsyncGitNotification::RemoteTags
7377
}
7478
}

src/app.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl App {
179179
),
180180
tags_popup: TagListComponent::new(
181181
&queue,
182-
sender_app,
182+
sender,
183183
theme.clone(),
184184
key_config.clone(),
185185
),

src/components/syntax_text.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ use tui::{
3232

3333
pub struct SyntaxTextComponent {
3434
current_file: Option<(String, Either<ui::SyntaxText, String>)>,
35-
async_highlighting:
36-
AsyncSingleJob<AsyncSyntaxJob, AsyncAppNotification>,
35+
async_highlighting: AsyncSingleJob<AsyncSyntaxJob>,
3736
key_config: SharedKeyConfig,
3837
paragraph_state: Cell<ParagraphState>,
3938
focused: bool,
@@ -48,10 +47,7 @@ impl SyntaxTextComponent {
4847
theme: SharedTheme,
4948
) -> Self {
5049
Self {
51-
async_highlighting: AsyncSingleJob::new(
52-
sender.clone(),
53-
AsyncAppNotification::SyntaxHighlighting,
54-
),
50+
async_highlighting: AsyncSingleJob::new(sender.clone()),
5551
current_file: None,
5652
paragraph_state: Cell::new(ParagraphState::default()),
5753
focused: false,

src/components/taglist.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::{
88
queue::{Action, InternalEvent, Queue},
99
strings,
1010
ui::{self, Size},
11-
AsyncAppNotification, AsyncNotification,
11+
AsyncNotification,
1212
};
1313
use anyhow::Result;
1414
use asyncgit::{
@@ -46,8 +46,7 @@ pub struct TagListComponent {
4646
current_height: std::cell::Cell<usize>,
4747
missing_remote_tags: Option<Vec<String>>,
4848
basic_credential: Option<BasicAuthCredential>,
49-
async_remote_tags:
50-
AsyncSingleJob<AsyncRemoteTagsJob, AsyncAppNotification>,
49+
async_remote_tags: AsyncSingleJob<AsyncRemoteTagsJob>,
5150
key_config: SharedKeyConfig,
5251
}
5352

@@ -251,7 +250,7 @@ impl Component for TagListComponent {
251250
impl TagListComponent {
252251
pub fn new(
253252
queue: &Queue,
254-
sender: &Sender<AsyncAppNotification>,
253+
sender: &Sender<AsyncGitNotification>,
255254
theme: SharedTheme,
256255
key_config: SharedKeyConfig,
257256
) -> Self {
@@ -264,10 +263,7 @@ impl TagListComponent {
264263
current_height: std::cell::Cell::new(0),
265264
basic_credential: None,
266265
missing_remote_tags: None,
267-
async_remote_tags: AsyncSingleJob::new(
268-
sender.clone(),
269-
AsyncAppNotification::RemoteTags,
270-
),
266+
async_remote_tags: AsyncSingleJob::new(sender.clone()),
271267
key_config,
272268
}
273269
}
@@ -301,7 +297,7 @@ impl TagListComponent {
301297
pub fn update(&mut self, ev: AsyncNotification) {
302298
if matches!(
303299
ev,
304-
AsyncNotification::App(AsyncAppNotification::RemoteTags)
300+
AsyncNotification::Git(AsyncGitNotification::RemoteTags)
305301
) {
306302
if let Some(job) = self.async_remote_tags.take_last() {
307303
if let Some(Ok(missing_remote_tags)) = job.result() {

src/main.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ pub enum QueueEvent {
8080
pub enum AsyncAppNotification {
8181
///
8282
SyntaxHighlighting,
83-
///
84-
RemoteTags,
8583
}
8684

8785
#[derive(Clone, Copy, Debug, PartialEq)]

src/ui/syntax_text.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use syntect::{
1616
};
1717
use tui::text::{Span, Spans};
1818

19+
use crate::AsyncAppNotification;
20+
1921
struct SyntaxLine {
2022
items: Vec<(Style, usize, Range<usize>)>,
2123
}
@@ -175,7 +177,9 @@ impl AsyncSyntaxJob {
175177
}
176178

177179
impl AsyncJob for AsyncSyntaxJob {
178-
fn run(&mut self) {
180+
type Notification = AsyncAppNotification;
181+
182+
fn run(&mut self) -> Self::Notification {
179183
if let Ok(mut state) = self.state.lock() {
180184
*state = state.take().map(|state| match state {
181185
JobState::Request((content, path)) => {
@@ -186,5 +190,7 @@ impl AsyncJob for AsyncSyntaxJob {
186190
JobState::Response(res) => JobState::Response(res),
187191
});
188192
}
193+
194+
AsyncAppNotification::SyntaxHighlighting
189195
}
190196
}

0 commit comments

Comments
 (0)