Skip to content

Commit

Permalink
Support sending events without an event: field for the event name
Browse files Browse the repository at this point in the history
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<Option<&str>>`, 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.
  • Loading branch information
joshtriplett authored and jbr committed Dec 5, 2020
1 parent 8e5add4 commit f52512c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
12 changes: 9 additions & 3 deletions src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Option<&str>>,
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 {
Expand Down
33 changes: 33 additions & 0 deletions tests/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit f52512c

Please sign in to comment.