mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
feat: avoid duplicating the same task of precaching the size (#11)
This commit is contained in:
parent
f01e1a272f
commit
d8725b3d7c
2 changed files with 20 additions and 4 deletions
|
|
@ -1,7 +1,8 @@
|
||||||
use std::{collections::BTreeMap, path::{Path, PathBuf}, sync::Arc};
|
use std::{collections::{BTreeMap, BTreeSet}, path::{Path, PathBuf}, sync::Arc};
|
||||||
|
|
||||||
use anyhow::{Error, Result};
|
use anyhow::{Error, Result};
|
||||||
use image::{imageops::FilterType, ImageFormat};
|
use image::{imageops::FilterType, ImageFormat};
|
||||||
|
use parking_lot::Mutex;
|
||||||
use tokio::{fs, sync::mpsc};
|
use tokio::{fs, sync::mpsc};
|
||||||
|
|
||||||
use super::TaskOp;
|
use super::TaskOp;
|
||||||
|
|
@ -12,6 +13,8 @@ pub struct Precache {
|
||||||
tx: async_channel::Sender<PrecacheOp>,
|
tx: async_channel::Sender<PrecacheOp>,
|
||||||
|
|
||||||
sch: mpsc::UnboundedSender<TaskOp>,
|
sch: mpsc::UnboundedSender<TaskOp>,
|
||||||
|
|
||||||
|
pub(super) size_handing: Mutex<BTreeSet<PathBuf>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -48,7 +51,7 @@ pub(super) struct PrecacheOpVideo {
|
||||||
impl Precache {
|
impl Precache {
|
||||||
pub(super) fn new(sch: mpsc::UnboundedSender<TaskOp>) -> Self {
|
pub(super) fn new(sch: mpsc::UnboundedSender<TaskOp>) -> Self {
|
||||||
let (tx, rx) = async_channel::unbounded();
|
let (tx, rx) = async_channel::unbounded();
|
||||||
Self { tx, rx, sch }
|
Self { tx, rx, sch, size_handing: Default::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
|
@ -115,9 +118,16 @@ impl Precache {
|
||||||
if let Ok(mut file) = File::from(&task.target).await {
|
if let Ok(mut file) = File::from(&task.target).await {
|
||||||
file.length = length;
|
file.length = length;
|
||||||
task.throttle.done((task.target, file), |buf| {
|
task.throttle.done((task.target, file), |buf| {
|
||||||
|
let mut handing = self.size_handing.lock();
|
||||||
|
for (path, _) in &buf {
|
||||||
|
handing.remove(path);
|
||||||
|
}
|
||||||
|
|
||||||
let parent = buf[0].0.parent().unwrap().to_path_buf();
|
let parent = buf[0].0.parent().unwrap().to_path_buf();
|
||||||
emit!(Files(FilesOp::Sort(parent, BTreeMap::from_iter(buf))));
|
emit!(Files(FilesOp::Sort(parent, BTreeMap::from_iter(buf))));
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
self.size_handing.lock().remove(&task.target);
|
||||||
};
|
};
|
||||||
|
|
||||||
self.sch.send(TaskOp::Adv(task.id, 1, 0))?;
|
self.sch.send(TaskOp::Adv(task.id, 1, 0))?;
|
||||||
|
|
|
||||||
|
|
@ -383,11 +383,17 @@ impl Scheduler {
|
||||||
|
|
||||||
pub(super) fn precache_size(&self, targets: Vec<PathBuf>) {
|
pub(super) fn precache_size(&self, targets: Vec<PathBuf>) {
|
||||||
let throttle = Arc::new(Throttle::new(targets.len(), Duration::from_millis(300)));
|
let throttle = Arc::new(Throttle::new(targets.len(), Duration::from_millis(300)));
|
||||||
|
let mut handing = self.precache.size_handing.lock();
|
||||||
|
let mut running = self.running.write();
|
||||||
|
|
||||||
for target in targets {
|
for target in targets {
|
||||||
let name = format!("Calculate the size of {:?}", target);
|
if !handing.contains(&target) {
|
||||||
let id = self.running.write().add(name);
|
handing.insert(target.clone());
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = running.add(format!("Calculate the size of {:?}", target));
|
||||||
let _ = self.todo.send_blocking({
|
let _ = self.todo.send_blocking({
|
||||||
let precache = self.precache.clone();
|
let precache = self.precache.clone();
|
||||||
let throttle = throttle.clone();
|
let throttle = throttle.clone();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue