From dba0c643b747bb3edcb76ebd7feaa2467a86040e Mon Sep 17 00:00:00 2001 From: sxyazi Date: Wed, 6 Dec 2023 00:58:05 +0800 Subject: [PATCH] Simplify the code --- yazi-config/preset/yazi.toml | 12 +++++----- yazi-config/src/tasks/tasks.rs | 2 +- yazi-scheduler/src/running.rs | 23 +++++++++++------- yazi-scheduler/src/scheduler.rs | 42 +++++++++------------------------ yazi-scheduler/src/task.rs | 20 +++++++++------- 5 files changed, 43 insertions(+), 56 deletions(-) diff --git a/yazi-config/preset/yazi.toml b/yazi-config/preset/yazi.toml index a53c0227..4240f05b 100644 --- a/yazi-config/preset/yazi.toml +++ b/yazi-config/preset/yazi.toml @@ -66,12 +66,12 @@ rules = [ ] [tasks] -micro_workers = 5 -macro_workers = 10 -bizarre_retry = 5 -image_alloc = 536870912 # 512MB -image_bound = [ 0, 0 ] -ignore_precaching_tasks = true +micro_workers = 5 +macro_workers = 10 +bizarre_retry = 5 +image_alloc = 536870912 # 512MB +image_bound = [ 0, 0 ] +suppress_preload = false [plugins] preload = [] diff --git a/yazi-config/src/tasks/tasks.rs b/yazi-config/src/tasks/tasks.rs index e7ae67f4..21900dfe 100644 --- a/yazi-config/src/tasks/tasks.rs +++ b/yazi-config/src/tasks/tasks.rs @@ -15,7 +15,7 @@ pub struct Tasks { pub image_alloc: u32, pub image_bound: [u16; 2], - pub ignore_precaching_tasks: bool, + pub suppress_preload: bool, } impl Default for Tasks { diff --git a/yazi-scheduler/src/running.rs b/yazi-scheduler/src/running.rs index 54306b4c..e2d67b37 100644 --- a/yazi-scheduler/src/running.rs +++ b/yazi-scheduler/src/running.rs @@ -16,9 +16,9 @@ pub struct Running { } impl Running { - pub(super) fn add(&mut self, name: String) -> usize { + pub(super) fn add(&mut self, kind: TaskKind, name: String) -> usize { self.incr += 1; - self.all.insert(self.incr, Task::new(self.incr, name)); + self.all.insert(self.incr, Task::new(self.incr, kind, name)); self.incr } @@ -32,23 +32,28 @@ impl Running { pub fn get_id(&self, idx: usize) -> Option { self.values().nth(idx).map(|t| t.id) } #[inline] - pub fn len(&self) -> usize { self.all.len() } + pub fn len(&self) -> usize { + if TASKS.suppress_preload { + self.all.values().filter(|t| t.kind != TaskKind::Preload).count() + } else { + self.all.len() + } + } #[inline] pub(super) fn exists(&self, id: usize) -> bool { self.all.contains_key(&id) } #[inline] - pub fn values(&self) -> impl Iterator { - let map = self.all.values(); - if TASKS.ignore_precaching_tasks { - map.into_iter().filter(|t| t.kind == TaskKind::User).collect::>().into_iter() + pub fn values(&self) -> Box + '_> { + if TASKS.suppress_preload { + Box::new(self.all.values().filter(|t| t.kind != TaskKind::Preload)) } else { - map.into_iter().collect::>().into_iter() + Box::new(self.all.values()) } } #[inline] - pub fn is_empty(&self) -> bool { self.all.is_empty() } + pub fn is_empty(&self) -> bool { self.len() == 0 } pub(super) fn try_remove( &mut self, diff --git a/yazi-scheduler/src/scheduler.rs b/yazi-scheduler/src/scheduler.rs index 4b6d20fa..b67f1811 100644 --- a/yazi-scheduler/src/scheduler.rs +++ b/yazi-scheduler/src/scheduler.rs @@ -174,7 +174,7 @@ impl Scheduler { pub fn file_cut(&self, from: Url, mut to: Url, force: bool) { let mut running = self.running.write(); - let id = running.add(format!("Cut {:?} to {:?}", from, to)); + let id = running.add(TaskKind::User, format!("Cut {:?} to {:?}", from, to)); running.hooks.insert(id, { let from = from.clone(); @@ -205,7 +205,7 @@ impl Scheduler { pub fn file_copy(&self, from: Url, mut to: Url, force: bool) { let name = format!("Copy {:?} to {:?}", from, to); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -221,7 +221,7 @@ impl Scheduler { pub fn file_link(&self, from: Url, mut to: Url, relative: bool, force: bool) { let name = format!("Link {from:?} to {to:?}"); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -240,7 +240,7 @@ impl Scheduler { pub fn file_delete(&self, target: Url) { let mut running = self.running.write(); - let id = running.add(format!("Delete {:?}", target)); + let id = running.add(TaskKind::User, format!("Delete {:?}", target)); running.hooks.insert(id, { let target = target.clone(); @@ -268,7 +268,7 @@ impl Scheduler { pub fn file_trash(&self, target: Url) { let name = format!("Trash {:?}", target); - let id = self.running.write().add(name); + let id = self.running.write().add(TaskKind::User, name); _ = self.todo.send_blocking({ let file = self.file.clone(); @@ -287,7 +287,7 @@ impl Scheduler { }; let mut running = self.running.write(); - let id = running.add(name); + let id = running.add(TaskKind::User, name); let (cancel_tx, mut cancel_rx) = oneshot::channel(); running.hooks.insert(id, { @@ -335,11 +335,7 @@ impl Scheduler { continue; } - let id = running.add(format!("Calculate the size of {:?}", target)); - if let Some(task) = self.running.clone().write().get_mut(id) { - task.kind = TaskKind::PreCache; - } - + let id = running.add(TaskKind::Preload, format!("Calculate the size of {:?}", target)); _ = self.todo.send_blocking({ let precache = self.precache.clone(); let target = target.clone(); @@ -354,11 +350,7 @@ impl Scheduler { pub fn precache_mime(&self, targets: Vec) { let name = format!("Preload mimetype for {} files", targets.len()); - let id = self.running.write().add(name); - - if let Some(task) = self.running.clone().write().get_mut(id) { - task.kind = TaskKind::PreCache; - } + let id = self.running.write().add(TaskKind::Preload, name); _ = self.todo.send_blocking({ let precache = self.precache.clone(); @@ -371,33 +363,21 @@ impl Scheduler { pub fn precache_image(&self, targets: Vec) { let name = format!("Precache of {} image files", targets.len()); - let id = self.running.write().add(name); - - if let Some(task) = self.running.clone().write().get_mut(id) { - task.kind = TaskKind::PreCache; - } + let id = self.running.write().add(TaskKind::Preload, name); self.precache.image(id, targets).ok(); } pub fn precache_video(&self, targets: Vec) { let name = format!("Precache of {} video files", targets.len()); - let id = self.running.write().add(name); - - if let Some(task) = self.running.clone().write().get_mut(id) { - task.kind = TaskKind::PreCache; - } + let id = self.running.write().add(TaskKind::Preload, name); self.precache.video(id, targets).ok(); } pub fn precache_pdf(&self, targets: Vec) { let name = format!("Precache of {} PDF files", targets.len()); - let id = self.running.write().add(name); - - if let Some(task) = self.running.clone().write().get_mut(id) { - task.kind = TaskKind::PreCache; - } + let id = self.running.write().add(TaskKind::Preload, name); self.precache.pdf(id, targets).ok(); } diff --git a/yazi-scheduler/src/task.rs b/yazi-scheduler/src/task.rs index d914e084..6c404453 100644 --- a/yazi-scheduler/src/task.rs +++ b/yazi-scheduler/src/task.rs @@ -3,9 +3,9 @@ use tokio::sync::mpsc; #[derive(Debug, Default)] pub struct Task { pub id: usize, + pub kind: TaskKind, pub name: String, pub stage: TaskStage, - pub kind: TaskKind, pub total: u32, pub succ: u32, @@ -19,7 +19,16 @@ pub struct Task { } impl Task { - pub fn new(id: usize, name: String) -> Self { Self { id, name, ..Default::default() } } + pub fn new(id: usize, kind: TaskKind, name: String) -> Self { + Self { id, kind, name, ..Default::default() } + } +} + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub enum TaskKind { + #[default] + User, + Preload, } #[derive(Debug)] @@ -70,10 +79,3 @@ pub enum TaskStage { Dispatched, Hooked, } - -#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)] -pub enum TaskKind { - #[default] - User, - PreCache, -}