From 9463c75f12dcbd47fbb7a4e556229cae1a5b83d6 Mon Sep 17 00:00:00 2001 From: alex-ds13 <145657253+alex-ds13@users.noreply.github.com> Date: Sat, 14 Dec 2024 10:41:52 +0000 Subject: [PATCH] feat(client): create send_batch helper This commit adds a helper function `send_batch` to komorebi-client that allows sending multiple messages in a batch. 3rd party users of this library could already do this themselves but it is nice to have this helper to simplify it. --- komorebi-client/src/lib.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/komorebi-client/src/lib.rs b/komorebi-client/src/lib.rs index 8e6c599b..0480b18d 100644 --- a/komorebi-client/src/lib.rs +++ b/komorebi-client/src/lib.rs @@ -68,6 +68,20 @@ pub fn send_message(message: &SocketMessage) -> std::io::Result<()> { stream.write_all(serde_json::to_string(message)?.as_bytes()) } +pub fn send_batch(messages: impl IntoIterator) -> std::io::Result<()> { + let socket = DATA_DIR.join(KOMOREBI); + let mut stream = UnixStream::connect(socket)?; + stream.set_write_timeout(Some(Duration::from_secs(1)))?; + let msgs = messages.into_iter().fold(String::new(), |mut s, m| { + if let Ok(m_str) = serde_json::to_string(&m) { + s.push_str(&m_str); + s.push('\n'); + } + s + }); + stream.write_all(msgs.as_bytes()) +} + pub fn send_query(message: &SocketMessage) -> std::io::Result { let socket = DATA_DIR.join(KOMOREBI);