From f52512c506e636e1f5e4ee1319d593709dac398b Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Sat, 5 Dec 2020 14:05:57 -0800 Subject: [PATCH] Support sending events without an `event:` field for the event name The event field is optional in the specification, and adds overhead, which some applications do not want. Allow omitting it. To simplify use, accept the name as `impl Into>`, which allows existing code that passes `"name"` to continue working. Since this can potentially cause an inference failure in code that previously compiled, bump the major version, but *most* code should continue working with just a recompile. --- Cargo.toml | 2 +- src/encoder.rs | 12 +++++++++--- tests/encode.rs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8537af7..46273dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "async-sse" -version = "4.1.0" +version = "5.0.0" license = "MIT OR Apache-2.0" repository = "https://github.com/http-rs/async-sse" documentation = "https://docs.rs/async-sse" diff --git a/src/encoder.rs b/src/encoder.rs index 2192e82..e2c282d 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -100,10 +100,16 @@ impl Sender { } /// Send a new message over SSE. - pub async fn send(&self, name: &str, data: &str, id: Option<&str>) -> io::Result<()> { + pub async fn send( + &self, + name: impl Into>, + data: &str, + id: Option<&str>, + ) -> io::Result<()> { // Write the event name - let msg = format!("event:{}\n", name); - self.inner_send(msg).await?; + if let Some(name) = name.into() { + self.inner_send(format!("event:{}\n", name)).await?; + } // Write the id if let Some(id) = id { diff --git a/tests/encode.rs b/tests/encode.rs index a2b927b..3b895d7 100644 --- a/tests/encode.rs +++ b/tests/encode.rs @@ -37,6 +37,28 @@ async fn encode_message() -> http_types::Result<()> { Ok(()) } +#[async_std::test] +async fn encode_message_some() -> http_types::Result<()> { + let (sender, encoder) = encode(); + task::spawn(async move { sender.send(Some("cat"), "chashu", None).await }); + + let mut reader = decode(BufReader::new(encoder)); + let event = reader.next().await.unwrap()?; + assert_message(&event, "cat", "chashu", None); + Ok(()) +} + +#[async_std::test] +async fn encode_message_data_only() -> http_types::Result<()> { + let (sender, encoder) = encode(); + task::spawn(async move { sender.send(None, "chashu", None).await }); + + let mut reader = decode(BufReader::new(encoder)); + let event = reader.next().await.unwrap()?; + assert_message(&event, "message", "chashu", None); + Ok(()) +} + #[async_std::test] async fn encode_message_with_id() -> http_types::Result<()> { let (sender, encoder) = encode(); @@ -48,6 +70,17 @@ async fn encode_message_with_id() -> http_types::Result<()> { Ok(()) } +#[async_std::test] +async fn encode_message_data_only_with_id() -> http_types::Result<()> { + let (sender, encoder) = encode(); + task::spawn(async move { sender.send(None, "chashu", Some("0")).await }); + + let mut reader = decode(BufReader::new(encoder)); + let event = reader.next().await.unwrap()?; + assert_message(&event, "message", "chashu", Some("0")); + Ok(()) +} + #[async_std::test] async fn encode_retry() -> http_types::Result<()> { let (sender, encoder) = encode();