feat!: include the sender ID in static messages

This commit is contained in:
sxyazi 2024-06-20 17:38:33 +08:00
parent 0f84717a1b
commit 5816d81f1b
No known key found for this signature in database
6 changed files with 14 additions and 59 deletions

View file

@ -45,20 +45,6 @@ impl Body<'static> {
BodyCustom::from_lua(kind, value) BodyCustom::from_lua(kind, value)
} }
pub fn tab(kind: &str, body: &str) -> usize {
match kind {
"cd" | "hover" | "bulk" | "rename" => {}
_ => return 0,
}
match Self::from_str(kind, body) {
Ok(Self::Cd(b)) => b.tab,
Ok(Self::Hover(b)) => b.tab,
Ok(Self::Rename(b)) => b.tab,
_ => 0,
}
}
pub fn validate(kind: &str) -> Result<()> { pub fn validate(kind: &str) -> Result<()> {
if matches!( if matches!(
kind, kind,
@ -111,11 +97,6 @@ impl<'a> Body<'a> {
#[inline] #[inline]
pub fn with_sender(self, sender: u64) -> Payload<'a> { Payload::new(self).with_sender(sender) } pub fn with_sender(self, sender: u64) -> Payload<'a> { Payload::new(self).with_sender(sender) }
#[inline]
pub fn with_severity(self, severity: u16) -> Payload<'a> {
Payload::new(self).with_severity(severity)
}
} }
impl IntoLua<'_> for Body<'static> { impl IntoLua<'_> for Body<'static> {

View file

@ -41,11 +41,6 @@ impl<'a> Payload<'a> {
self.sender = sender; self.sender = sender;
self self
} }
pub(super) fn with_severity(mut self, severity: u16) -> Self {
self.sender = severity as u64;
self
}
} }
impl Payload<'static> { impl Payload<'static> {

View file

@ -80,12 +80,6 @@ impl Pubsub {
} }
} }
pub fn pub_static(severity: u16, body: Body) {
if Self::own_static_ability(body.kind()) {
Client::push(body.with_severity(severity));
}
}
pub fn pub_from_hi() -> bool { pub fn pub_from_hi() -> bool {
let abilities = REMOTE.read().keys().cloned().collect(); let abilities = REMOTE.read().keys().cloned().collect();
let abilities = BOOT.remote_events.union(&abilities).map(|s| s.as_str()).collect(); let abilities = BOOT.remote_events.union(&abilities).map(|s| s.as_str()).collect();
@ -98,8 +92,8 @@ impl Pubsub {
if LOCAL.read().contains_key("cd") { if LOCAL.read().contains_key("cd") {
Self::pub_(BodyCd::dummy(tab)); Self::pub_(BodyCd::dummy(tab));
} }
if Self::own_static_ability("cd") { if PEERS.read().values().any(|p| p.able("cd")) {
Client::push(BodyCd::borrowed(tab, url).with_severity(100)); Client::push(BodyCd::borrowed(tab, url));
} }
if BOOT.local_events.contains("cd") { if BOOT.local_events.contains("cd") {
BodyCd::borrowed(tab, url).with_receiver(*ID).flush(); BodyCd::borrowed(tab, url).with_receiver(*ID).flush();
@ -110,8 +104,8 @@ impl Pubsub {
if LOCAL.read().contains_key("hover") { if LOCAL.read().contains_key("hover") {
Self::pub_(BodyHover::dummy(tab)); Self::pub_(BodyHover::dummy(tab));
} }
if Self::own_static_ability("hover") { if PEERS.read().values().any(|p| p.able("hover")) {
Client::push(BodyHover::borrowed(tab, url).with_severity(200)); Client::push(BodyHover::borrowed(tab, url));
} }
if BOOT.local_events.contains("hover") { if BOOT.local_events.contains("hover") {
BodyHover::borrowed(tab, url).with_receiver(*ID).flush(); BodyHover::borrowed(tab, url).with_receiver(*ID).flush();
@ -147,7 +141,7 @@ impl Pubsub {
Self::pub_(BodyYank::dummy()); Self::pub_(BodyYank::dummy());
} }
if Self::own_static_ability("yank") { if Self::own_static_ability("yank") {
Client::push(BodyYank::borrowed(cut, urls).with_severity(300)); Client::push(BodyYank::borrowed(cut, urls));
} }
if BOOT.local_events.contains("yank") { if BOOT.local_events.contains("yank") {
BodyYank::borrowed(cut, urls).with_receiver(*ID).flush(); BodyYank::borrowed(cut, urls).with_receiver(*ID).flush();

View file

@ -66,9 +66,9 @@ impl Server {
continue; continue;
} }
if receiver == 0 && sender <= u16::MAX as u64 { if receiver == 0 {
let Some(body) = parts.next() else { continue }; let Some(body) = parts.next() else { continue };
if !STATE.set(kind, sender as u16, body) { continue } if !STATE.set(kind, sender, body) { continue }
} }
line.push('\n'); line.push('\n');
@ -91,10 +91,6 @@ impl Server {
let Ok(payload) = Payload::from_str(&s) else { return }; let Ok(payload) = Payload::from_str(&s) else { return };
let Body::Hi(hi) = payload.body else { return }; let Body::Hi(hi) = payload.body else { return };
if payload.sender <= u16::MAX as u64 {
return; // The kind of static messages cannot be "hi"
}
if id.is_none() { if id.is_none() {
if let Some(ref state) = *STATE.read() { if let Some(ref state) = *STATE.read() {
state.values().for_each(|s| _ = tx.send(s.clone())); state.values().for_each(|s| _ = tx.send(s.clone()));

View file

@ -6,7 +6,7 @@ use tokio::{fs::{self, File, OpenOptions}, io::{AsyncBufReadExt, AsyncWriteExt,
use yazi_boot::BOOT; use yazi_boot::BOOT;
use yazi_shared::{timestamp_us, RoCell}; use yazi_shared::{timestamp_us, RoCell};
use crate::{body::Body, CLIENTS}; use crate::CLIENTS;
pub static STATE: RoCell<State> = RoCell::new(); pub static STATE: RoCell<State> = RoCell::new();
@ -23,23 +23,22 @@ impl Deref for State {
} }
impl State { impl State {
pub fn set(&self, kind: &str, severity: u16, body: &str) -> bool { pub fn set(&self, kind: &str, sender: u64, body: &str) -> bool {
let Some(inner) = &mut *self.inner.write() else { return false }; let Some(inner) = &mut *self.inner.write() else { return false };
let key = format!("{}_{severity}_{kind}", Body::tab(kind, body));
if body == "null" { if body == "null" {
return inner return inner
.remove(&key) .remove(kind)
.map(|_| self.last.store(timestamp_us(), Ordering::Relaxed)) .map(|_| self.last.store(timestamp_us(), Ordering::Relaxed))
.is_some(); .is_some();
} }
let value = format!("{kind},0,{severity},{body}\n"); let value = format!("{kind},0,{sender},{body}\n");
if inner.get(&key).is_some_and(|s| *s == value) { if inner.get(kind).is_some_and(|s| *s == value) {
return false; return false;
} }
inner.insert(key, value); inner.insert(kind.to_owned(), value);
self.last.store(timestamp_us(), Ordering::Relaxed); self.last.store(timestamp_us(), Ordering::Relaxed);
true true
} }
@ -86,9 +85,7 @@ impl State {
let mut parts = line.splitn(4, ','); let mut parts = line.splitn(4, ',');
let Some(kind) = parts.next() else { continue }; let Some(kind) = parts.next() else { continue };
let Some(_) = parts.next() else { continue }; let Some(_) = parts.next() else { continue };
let Some(severity) = parts.next().and_then(|s| s.parse::<u16>().ok()) else { continue }; inner.insert(kind.to_owned(), mem::take(&mut line));
let Some(body) = parts.next() else { continue };
inner.insert(format!("{}_{severity}_{kind}", Body::tab(kind, body)), mem::take(&mut line));
} }
let clients = CLIENTS.read(); let clients = CLIENTS.read();

View file

@ -25,14 +25,6 @@ impl Pubsub {
})?, })?,
)?; )?;
ps.raw_set(
"pub_static",
lua.create_function(|_, (severity, kind, value): (u16, mlua::String, Value)| {
yazi_dds::Pubsub::pub_static(severity, Body::from_lua(kind.to_str()?, value)?);
Ok(())
})?,
)?;
ps.raw_set( ps.raw_set(
"sub", "sub",
lua.create_function(|lua, (kind, f): (mlua::String, Function)| { lua.create_function(|lua, (kind, f): (mlua::String, Function)| {