feat!: include the sender ID in static messages (#1172)

This commit is contained in:
三咲雅 · Misaki Masa 2024-06-23 01:33:59 +08:00 committed by GitHub
parent 505de05d66
commit 0c5d621348
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 29 additions and 109 deletions

View file

@ -18,8 +18,6 @@ pub(super) struct Args {
pub(super) enum Command { pub(super) enum Command {
/// Publish a message to remote instance(s). /// Publish a message to remote instance(s).
Pub(CommandPub), Pub(CommandPub),
/// Publish a static message to all remote instances.
PubStatic(CommandPubStatic),
/// Manage packages. /// Manage packages.
Pack(CommandPack), Pack(CommandPack),
/// Subscribe to messages from all remote instances. /// Subscribe to messages from all remote instances.
@ -66,35 +64,6 @@ impl CommandPub {
} }
} }
#[derive(clap::Args)]
pub(super) struct CommandPubStatic {
/// The kind of message.
#[arg(index = 1)]
pub(super) kind: String,
/// The severity of the message.
#[arg(index = 2)]
pub(super) severity: u16,
/// Send the message with a string body.
#[arg(long)]
pub(super) str: Option<String>,
/// Send the message with a JSON body.
#[arg(long)]
pub(super) json: Option<String>,
}
impl CommandPubStatic {
#[allow(dead_code)]
pub(super) fn body(&self) -> Result<Cow<str>> {
if let Some(json) = &self.json {
Ok(json.into())
} else if let Some(str) = &self.str {
Ok(serde_json::to_string(str)?.into())
} else {
Ok("".into())
}
}
}
#[derive(clap::Args)] #[derive(clap::Args)]
#[command(arg_required_else_help = true)] #[command(arg_required_else_help = true)]
pub(super) struct CommandPack { pub(super) struct CommandPack {

View file

@ -19,14 +19,7 @@ async fn main() -> anyhow::Result<()> {
match Args::parse().command { match Args::parse().command {
Command::Pub(cmd) => { Command::Pub(cmd) => {
yazi_dds::init(); yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver()?, None, &cmd.body()?).await { if let Err(e) = yazi_dds::Client::shot(&cmd.kind, cmd.receiver()?, &cmd.body()?).await {
eprintln!("Cannot send message: {e}");
std::process::exit(1);
}
}
Command::PubStatic(cmd) => {
yazi_dds::init();
if let Err(e) = yazi_dds::Client::shot(&cmd.kind, 0, Some(cmd.severity), &cmd.body()?).await {
eprintln!("Cannot send message: {e}"); eprintln!("Cannot send message: {e}");
std::process::exit(1); std::process::exit(1);
} }

View file

@ -32,7 +32,7 @@ impl Body<'static> {
"hover" => Self::Hover(serde_json::from_str(body)?), "hover" => Self::Hover(serde_json::from_str(body)?),
"rename" => Self::Rename(serde_json::from_str(body)?), "rename" => Self::Rename(serde_json::from_str(body)?),
"bulk" => Self::Bulk(serde_json::from_str(body)?), "bulk" => Self::Bulk(serde_json::from_str(body)?),
"yank" => Self::Yank(serde_json::from_str(body)?), "@yank" => Self::Yank(serde_json::from_str(body)?),
"move" => Self::Move(serde_json::from_str(body)?), "move" => Self::Move(serde_json::from_str(body)?),
"trash" => Self::Trash(serde_json::from_str(body)?), "trash" => Self::Trash(serde_json::from_str(body)?),
"delete" => Self::Delete(serde_json::from_str(body)?), "delete" => Self::Delete(serde_json::from_str(body)?),
@ -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,
@ -69,7 +55,7 @@ impl Body<'static> {
| "hover" | "hover"
| "rename" | "rename"
| "bulk" | "bulk"
| "yank" | "@yank"
| "move" | "move"
| "trash" | "trash"
| "delete" | "delete"
@ -77,7 +63,11 @@ impl Body<'static> {
bail!("Cannot construct system event"); bail!("Cannot construct system event");
} }
if !kind.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-') { let mut it = kind.bytes().peekable();
if it.peek() == Some(&b'@') {
it.next(); // Skip `@` as it's a prefix for static messages
}
if !it.all(|b| b.is_ascii_alphanumeric() || b == b'-') {
bail!("Kind must be alphanumeric with dashes"); bail!("Kind must be alphanumeric with dashes");
} }
@ -96,7 +86,7 @@ impl<'a> Body<'a> {
Self::Hover(_) => "hover", Self::Hover(_) => "hover",
Self::Rename(_) => "rename", Self::Rename(_) => "rename",
Self::Bulk(_) => "bulk", Self::Bulk(_) => "bulk",
Self::Yank(_) => "yank", Self::Yank(_) => "@yank",
Self::Move(_) => "move", Self::Move(_) => "move",
Self::Trash(_) => "trash", Self::Trash(_) => "trash",
Self::Delete(_) => "delete", Self::Delete(_) => "delete",
@ -111,11 +101,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

@ -64,12 +64,11 @@ impl Client {
} }
/// Connect to an existing server to send a single message. /// Connect to an existing server to send a single message.
pub async fn shot(kind: &str, receiver: u64, severity: Option<u16>, body: &str) -> Result<()> { pub async fn shot(kind: &str, receiver: u64, body: &str) -> Result<()> {
Body::validate(kind)?; Body::validate(kind)?;
let sender = severity.map(Into::into).unwrap_or(*ID);
let payload = format!( let payload = format!(
"{}\n{kind},{receiver},{sender},{body}\n{}\n", "{}\n{kind},{receiver},{ID},{body}\n{}\n",
Payload::new(BodyHi::borrowed(Default::default())), Payload::new(BodyHi::borrowed(Default::default())),
Payload::new(BodyBye::owned()) Payload::new(BodyBye::owned())
); );

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();
@ -143,13 +137,13 @@ impl Pubsub {
} }
pub fn pub_from_yank(cut: bool, urls: &HashSet<Url>) { pub fn pub_from_yank(cut: bool, urls: &HashSet<Url>) {
if LOCAL.read().contains_key("yank") { if LOCAL.read().contains_key("@yank") {
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 && kind.starts_with('@') {
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

@ -1,6 +1,6 @@
local function setup(_, opts) local function setup(_, opts)
if opts.sync_yanked then if opts.sync_yanked then
ps.sub_remote("yank", function(body) ya.manager_emit("update_yanked", { cut = body.cut, urls = body }) end) ps.sub_remote("@yank", function(body) ya.manager_emit("update_yanked", { cut = body.cut, urls = body }) end)
end end
end end

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)| {