Repository: sxyazi/yazi
File: yazi-dds/src/client.rs (Rust)

Code before:
		while let Ok(Some(line)) = lines.next_line().await {
			match line.split(',').next() {
				Some("hey") => {
					if let Ok(Body::Hey(hey)) = Payload::from_str(&line).map(|p| p.body) {
						(peers, version) = (hey.peers, Some(hey.version));
					}
				}
				Some("bye") => break,
				_ => {}
			}
		}

		if version.as_deref() != Some(BodyHi::version()) {
			bail!(
				"Incompatible version (Ya {}, Yazi {}). Restart all `ya` and `yazi` processes if you upgrade either one.",
				BodyHi::version(),
				version.as_deref().unwrap_or("Unknown")
			);
		}

		match peers.get(&receiver).map(|p| p.able(kind)) {
			Some(true) => {}
			Some(false) => {
				bail!("Receiver `{receiver}` does not have the ability to receive `{kind}` messages.")
			}
			None => bail!("Receiver `{receiver}` not found. Check if the receiver is running."),
		}

		Ok(())
	}

	/// Connect to an existing server and listen in on the messages that are being
	/// sent by other yazi instances:
	///   - If no server is running, fail right away;
	///   - If a server is closed, attempt to reconnect forever.
	pub async fn draw(kinds: HashSet<&str>) -> Result<()> {
		async fn make(kinds: &HashSet<&str>) -> Result<ClientReader> {
			let (lines, mut writer) = Stream::connect().await?;
			let hi = Payload::new(BodyHi::borrowed(kinds.clone()));
			writer.write_all(format!("{hi}\n").as_bytes()).await?;
			writer.flush().await?;
			Ok(lines)
		}

		let mut lines = make(&kinds).await.context("No running Yazi instance found")?;
		loop {
			match lines.next_line().await? {
				Some(s) => {
					let kind = s.split(',').next();
					if matches!(kind, Some(kind) if kinds.contains(kind)) {
						println!("{s}");

Review comment (performance):
[nitpick] Consider caching the result of p.able(kind) for all peers prior to the check to avoid redundant computations, which could improve clarity and maintainability.
```suggestion
		// Cache the results of `p.able(kind)` for all peers
		let peer_abilities: HashMap<_, _> = peers
			.iter()
			.map(|(id, peer)| (id, peer.able(kind)))
			.collect();

		match (receiver, peer_abilities.get(&receiver).copied()) {
			// Send to all receivers
			(Id(0), _) if peers.is_empty() => {
				bail!("No receiver found. Check if any receivers are running.")
			}
			(Id(0), _) if peer_abilities.values().all(|&able| !able) => {
```

Code after:
		while let Ok(Some(line)) = lines.next_line().await {
			match line.split(',').next() {
				Some("hey") => {
					if let Ok(Body::Hey(hey)) = Payload::from_str(&line).map(|p| p.body) {
						(peers, version) = (hey.peers, Some(hey.version));
					}
				}
				Some("bye") => break,
				_ => {}
			}
		}

		if version.as_deref() != Some(BodyHi::version()) {
			bail!(
				"Incompatible version (Ya {}, Yazi {}). Restart all `ya` and `yazi` processes if you upgrade either one.",
				BodyHi::version(),
				version.as_deref().unwrap_or("Unknown")
			);
		}

		match (receiver, peers.get(&receiver).map(|p| p.able(kind))) {
			// Send to all receivers
			(Id(0), _) if peers.is_empty() => {
				bail!("No receiver found. Check if any receivers are running.")
			}
			(Id(0), _) if peers.values().all(|p| !p.able(kind)) => {
				bail!("No receiver has the ability to receive `{kind}` messages.")
			}
			(Id(0), _) => {}

			// Send to a specific receiver
			(_, Some(true)) => {}
			(_, Some(false)) => {
				bail!("Receiver `{receiver}` does not have the ability to receive `{kind}` messages.")
			}
			(_, None) => bail!("Receiver `{receiver}` not found. Check if the receiver is running."),
		}

		Ok(())
	}

	/// Connect to an existing server and listen in on the messages that are being
	/// sent by other yazi instances:
	///   - If no server is running, fail right away;
	///   - If a server is closed, attempt to reconnect forever.
	pub async fn draw(kinds: HashSet<&str>) -> Result<()> {
		async fn make(kinds: &HashSet<&str>) -> Result<ClientReader> {
			let (lines, mut writer) = Stream::connect().await?;
			let hi = Payload::new(BodyHi::borrowed(kinds.clone()));
			writer.write_all(format!("{hi}\n").as_bytes()).await?;
			writer.flush().await?;