Remove task priority

Since we are now blocking on task inputs, the priority is no longer
required.
pull/46/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 5030749ab1
commit 2596c0c4c3

@ -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,
))

@ -643,37 +643,13 @@ pub enum MsgOut {
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Task {
priority: usize,
msg: MsgIn,
key: Option<Key>,
created_at: DateTime<Utc>,
}
impl Task {
pub fn new(priority: usize, msg: MsgIn, key: Option<Key>) -> 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<Ordering> {
Some(self.cmp(other))
pub fn new(msg: MsgIn, key: Option<Key>) -> 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)

@ -5,7 +5,7 @@ use std::time::Duration;
pub fn start_auto_refreshing(tx: Sender<Task>) {
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));
});

@ -19,18 +19,17 @@ pub fn keep_reading(tx_msg_in: Sender<Task>, rx_event_reader: Receiver<bool>) {
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,
))

@ -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,
))

@ -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()?;

@ -15,11 +15,10 @@ pub fn keep_reading(pipe: String, tx: Sender<Task>) {
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,
))

@ -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));
}

@ -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);
}
Loading…
Cancel
Save