refactor: bulk renaming sends "bulk" events

This commit is contained in:
Mika Vilpas 2024-04-24 17:09:23 +03:00
parent baf050464f
commit 65003337f9
3 changed files with 19 additions and 4 deletions

View file

@ -94,15 +94,18 @@ impl Manager {
} else if let Ok(f) = File::from(new_url).await {
let old_url = Url::from(old);
succeeded.insert(old_url.clone(), f.clone());
Pubsub::pub_from_rename(tab, &old_url, &f.url);
} else {
failed.push((o, n, anyhow!("Failed to retrieve file info")));
}
}
if !succeeded.is_empty() {
let changes: HashMap<_, _> =
succeeded.clone().into_iter().map(|(old, new)| (old, new.url)).collect();
FilesOp::Upserting(cwd, succeeded).emit();
Pubsub::pub_from_bulk(tab, &changes);
}
drop(permit);

View file

@ -21,7 +21,7 @@ impl<'a> BodyBulk<'a> {
impl BodyBulk<'static> {
#[inline]
pub fn owned(tab: usize, changes: &HashMap<Url, Url>) -> Body<'static> {
pub fn dummy(tab: usize, changes: &HashMap<Url, Url>) -> Body<'static> {
Self { tab, changes: Cow::Owned(changes.clone()) }.into()
}
}

View file

@ -5,7 +5,7 @@ use parking_lot::RwLock;
use yazi_boot::BOOT;
use yazi_shared::{fs::Url, RoCell};
use crate::{body::{Body, BodyCd, BodyDelete, BodyHi, BodyHover, BodyMove, BodyMoveItem, BodyRename, BodyTrash, BodyYank}, Client, ID, PEERS};
use crate::{body::{Body, BodyBulk, BodyCd, BodyDelete, BodyHi, BodyHover, BodyMove, BodyMoveItem, BodyRename, BodyTrash, BodyYank}, Client, ID, PEERS};
pub static LOCAL: RoCell<RwLock<HashMap<String, HashMap<String, Function<'static>>>>> =
RoCell::new();
@ -118,6 +118,18 @@ impl Pubsub {
}
}
pub fn pub_from_bulk(tab: usize, changes: &HashMap<Url, Url>) {
if LOCAL.read().contains_key("bulk") {
Self::pub_(BodyBulk::dummy(tab, changes));
}
if PEERS.read().values().any(|p| p.able("bulk")) {
Client::push(BodyBulk::borrowed(tab, changes));
}
if BOOT.local_events.contains("bulk") {
BodyBulk::borrowed(tab, changes).with_receiver(*ID).flush();
}
}
pub fn pub_from_rename(tab: usize, from: &Url, to: &Url) {
if LOCAL.read().contains_key("rename") {
Self::pub_(BodyRename::dummy(tab, from, to));