From 2596c0c4c3088b4bb58db3a2834f399cc4c145b3 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Fri, 9 Apr 2021 15:13:20 +0530 Subject: [PATCH] Remove task priority Since we are now blocking on task inputs, the priority is no longer required. --- benches/navigation.rs | 7 ------- src/app.rs | 30 +++--------------------------- src/auto_refresher.rs | 2 +- src/event_reader.rs | 5 ++--- src/explorer.rs | 2 -- src/main.rs | 5 ++--- src/pipe_reader.rs | 3 +-- src/pwd_watcher.rs | 2 +- tests/test_task.rs | 29 ----------------------------- 9 files changed, 10 insertions(+), 75 deletions(-) delete mode 100644 tests/test_task.rs diff --git a/benches/navigation.rs b/benches/navigation.rs index 4318d55..a0987f5 100644 --- a/benches/navigation.rs +++ b/benches/navigation.rs @@ -11,7 +11,6 @@ fn criterion_benchmark(c: &mut Criterion) { let app = app::App::create("/tmp/xplr_bench".into()) .expect("failed to create app") .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::ChangeDirectory("/tmp/xplr_bench".into())), None, )) @@ -21,7 +20,6 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { app.clone() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::FocusNext), None, )) @@ -33,7 +31,6 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { app.clone() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::FocusPrevious), None, )) @@ -45,7 +42,6 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { app.clone() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::FocusFirst), None, )) @@ -57,7 +53,6 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { app.clone() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::FocusLast), None, )) @@ -69,13 +64,11 @@ fn criterion_benchmark(c: &mut Criterion) { b.iter(|| { app.clone() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::Back), None, )) .unwrap() .handle_task(app::Task::new( - 1, app::MsgIn::External(app::ExternalMsg::Enter), None, )) diff --git a/src/app.rs b/src/app.rs index 6b70890..b30a86b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -643,37 +643,13 @@ pub enum MsgOut { #[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct Task { - priority: usize, msg: MsgIn, key: Option, - created_at: DateTime, } impl Task { - pub fn new(priority: usize, msg: MsgIn, key: Option) -> Self { - Self { - priority, - msg, - key, - created_at: Utc::now(), - } - } -} - -impl Ord for Task { - fn cmp(&self, other: &Self) -> Ordering { - // Notice that the we flip the ordering on costs. - // In case of a tie we compare positions - this step is necessary - // to make implementations of `PartialEq` and `Ord` consistent. - other - .priority - .cmp(&self.priority) - .then_with(|| other.created_at.cmp(&self.created_at)) - } -} -impl PartialOrd for Task { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) + pub fn new(msg: MsgIn, key: Option) -> Self { + Self { msg, key } } } @@ -926,7 +902,7 @@ impl App { .unwrap_or_else(|| default.map(|a| a.messages).unwrap_or_default()); for msg in msgs { - self = self.enqueue(Task::new(0, MsgIn::External(msg), Some(key))); + self = self.enqueue(Task::new(MsgIn::External(msg), Some(key))); } Ok(self) diff --git a/src/auto_refresher.rs b/src/auto_refresher.rs index 54b7fd7..99c42ed 100644 --- a/src/auto_refresher.rs +++ b/src/auto_refresher.rs @@ -5,7 +5,7 @@ use std::time::Duration; pub fn start_auto_refreshing(tx: Sender) { thread::spawn(move || loop { - tx.send(Task::new(3, MsgIn::External(ExternalMsg::Refresh), None)) + tx.send(Task::new(MsgIn::External(ExternalMsg::Refresh), None)) .unwrap(); thread::sleep(Duration::from_secs(1)); }); diff --git a/src/event_reader.rs b/src/event_reader.rs index 74bbd76..a426958 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -19,18 +19,17 @@ pub fn keep_reading(tx_msg_in: Sender, rx_event_reader: Receiver) { Ok(Event::Key(key)) => { let key = Key::from_event(key); let msg = MsgIn::Internal(InternalMsg::HandleKey(key)); - tx_msg_in.send(Task::new(0, msg, Some(key))).unwrap(); + tx_msg_in.send(Task::new(msg, Some(key))).unwrap(); } Ok(Event::Resize(_, _)) => { let msg = MsgIn::External(ExternalMsg::Refresh); - tx_msg_in.send(Task::new(0, msg, None)).unwrap(); + tx_msg_in.send(Task::new(msg, None)).unwrap(); } Ok(_) => {} Err(e) => { tx_msg_in .send(Task::new( - 0, MsgIn::External(ExternalMsg::LogError(e.to_string())), None, )) diff --git a/src/explorer.rs b/src/explorer.rs index f3d9cf0..9732890 100644 --- a/src/explorer.rs +++ b/src/explorer.rs @@ -45,7 +45,6 @@ pub fn explore( let dir = DirectoryBuffer::new(parent.clone(), nodes, focus_index); tx.send(Task::new( - 1, MsgIn::Internal(InternalMsg::AddDirectory(parent, dir)), None, )) @@ -53,7 +52,6 @@ pub fn explore( }) .unwrap_or_else(|e| { tx.send(Task::new( - 1, MsgIn::External(ExternalMsg::LogError(e.to_string())), None, )) diff --git a/src/main.rs b/src/main.rs index a61a874..8d30971 100644 --- a/src/main.rs +++ b/src/main.rs @@ -101,7 +101,6 @@ fn main() -> Result<()> { ); tx_msg_in.send(app::Task::new( - 0, app::MsgIn::External(app::ExternalMsg::LogInfo(msg)), None, ))?; @@ -228,7 +227,7 @@ fn main() -> Result<()> { if let Err(e) = status { let msg = app::MsgIn::External(app::ExternalMsg::LogError(e)); - tx_msg_in.send(app::Task::new(1, msg, None))?; + tx_msg_in.send(app::Task::new(msg, None))?; }; tx_event_reader.send(false)?; @@ -254,7 +253,7 @@ fn main() -> Result<()> { if let Err(e) = status { let msg = app::MsgIn::External(app::ExternalMsg::LogError(e)); - tx_msg_in.send(app::Task::new(1, msg, None))?; + tx_msg_in.send(app::Task::new(msg, None))?; }; terminal.hide_cursor()?; diff --git a/src/pipe_reader.rs b/src/pipe_reader.rs index dacd6b2..fbac156 100644 --- a/src/pipe_reader.rs +++ b/src/pipe_reader.rs @@ -15,11 +15,10 @@ pub fn keep_reading(pipe: String, tx: Sender) { msgs.for_each(|msg| match msg { Ok(m) => { - tx.send(Task::new(2, MsgIn::External(m), None)).unwrap(); + tx.send(Task::new(MsgIn::External(m), None)).unwrap(); } Err(e) => { tx.send(Task::new( - 0, MsgIn::External(ExternalMsg::LogError(e.to_string())), None, )) diff --git a/src/pwd_watcher.rs b/src/pwd_watcher.rs index 433a826..9707591 100644 --- a/src/pwd_watcher.rs +++ b/src/pwd_watcher.rs @@ -29,7 +29,7 @@ pub fn keep_watching( if rx.try_recv().is_ok() { let msg = MsgIn::External(ExternalMsg::Explore); - tx_msg_in.send(Task::new(3, msg, None)).unwrap(); + tx_msg_in.send(Task::new(msg, None)).unwrap(); } else { thread::sleep(Duration::from_secs(1)); } diff --git a/tests/test_task.rs b/tests/test_task.rs deleted file mode 100644 index a67f5cc..0000000 --- a/tests/test_task.rs +++ /dev/null @@ -1,29 +0,0 @@ -use std::collections::BinaryHeap; -use xplr::*; - -#[test] -fn test_task_priority() { - let task1 = app::Task::new(2, app::MsgIn::External(app::ExternalMsg::Refresh), None); - let task2 = app::Task::new(2, app::MsgIn::External(app::ExternalMsg::Refresh), None); - let task3 = app::Task::new(1, app::MsgIn::External(app::ExternalMsg::Refresh), None); - let task4 = app::Task::new(1, app::MsgIn::External(app::ExternalMsg::Refresh), None); - let task5 = app::Task::new(3, app::MsgIn::External(app::ExternalMsg::Refresh), None); - let task6 = app::Task::new(3, app::MsgIn::External(app::ExternalMsg::Refresh), None); - - let mut heap = BinaryHeap::new(); - - heap.push(task1.clone()); - heap.push(task2.clone()); - heap.push(task3.clone()); - heap.push(task4.clone()); - heap.push(task5.clone()); - heap.push(task6.clone()); - - assert_eq!(heap.pop(), Some(task3)); - assert_eq!(heap.pop(), Some(task4)); - assert_eq!(heap.pop(), Some(task1)); - assert_eq!(heap.pop(), Some(task2)); - assert_eq!(heap.pop(), Some(task5)); - assert_eq!(heap.pop(), Some(task6)); - assert_eq!(heap.pop(), None); -}