Add constructor to Sse (#244)

I think this is more consistent with the rest of the API
This commit is contained in:
David Pedersen
2021-08-23 17:51:30 +02:00
committed by GitHub
parent a753eac23f
commit 6777dc1e5c
3 changed files with 22 additions and 26 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ use axum::{
extract::TypedHeader, extract::TypedHeader,
handler::get, handler::get,
http::StatusCode, http::StatusCode,
response::sse::{sse, Event, Sse}, response::sse::{Event, Sse},
Router, Router,
}; };
use futures::stream::{self, Stream}; use futures::stream::{self, Stream};
@@ -59,5 +59,5 @@ async fn sse_handler(
.map(Ok) .map(Ok)
.throttle(Duration::from_secs(1)); .throttle(Duration::from_secs(1));
sse(stream) Sse::new(stream)
} }
+1 -5
View File
@@ -21,11 +21,7 @@ pub mod sse;
pub use crate::Json; pub use crate::Json;
#[doc(inline)] #[doc(inline)]
pub use self::{ pub use self::{headers::Headers, redirect::Redirect, sse::Sse};
headers::Headers,
redirect::Redirect,
sse::{sse, Sse},
};
/// Trait for generating responses. /// Trait for generating responses.
/// ///
+19 -19
View File
@@ -4,10 +4,10 @@
//! //!
//! ``` //! ```
//! use axum::{ //! use axum::{
//! handler::get,
//! Router, //! Router,
//! handler::get,
//! response::sse::{Event, KeepAlive, Sse},
//! }; //! };
//! use axum::response::sse::{sse, Event, KeepAlive, Sse};
//! use std::{time::Duration, convert::Infallible}; //! use std::{time::Duration, convert::Infallible};
//! use tokio_stream::StreamExt as _ ; //! use tokio_stream::StreamExt as _ ;
//! use futures::stream::{self, Stream}; //! use futures::stream::{self, Stream};
@@ -20,7 +20,7 @@
//! .map(Ok) //! .map(Ok)
//! .throttle(Duration::from_secs(1)); //! .throttle(Duration::from_secs(1));
//! //!
//! sse(stream).keep_alive(KeepAlive::default()) //! Sse::new(stream).keep_alive(KeepAlive::default())
//! } //! }
//! # async { //! # async {
//! # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap(); //! # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
@@ -50,22 +50,7 @@ use std::{
use sync_wrapper::SyncWrapper; use sync_wrapper::SyncWrapper;
use tokio::time::Sleep; use tokio::time::Sleep;
/// Create a new [`Sse`] response that will respond with the given stream of /// An SSE response
/// [`Event`]s.
///
/// See the [module docs](self) for more details.
pub fn sse<S>(stream: S) -> Sse<S>
where
S: TryStream<Ok = Event> + Send + 'static,
S::Error: Into<BoxError>,
{
Sse {
stream,
keep_alive: None,
}
}
/// An SSE response, created by [`sse`].
#[derive(Clone)] #[derive(Clone)]
pub struct Sse<S> { pub struct Sse<S> {
stream: S, stream: S,
@@ -73,6 +58,21 @@ pub struct Sse<S> {
} }
impl<S> Sse<S> { impl<S> Sse<S> {
/// Create a new [`Sse`] response that will respond with the given stream of
/// [`Event`]s.
///
/// See the [module docs](self) for more details.
pub fn new(stream: S) -> Self
where
S: TryStream<Ok = Event> + Send + 'static,
S::Error: Into<BoxError>,
{
Sse {
stream,
keep_alive: None,
}
}
/// Configure the interval between keep-alive messages. /// Configure the interval between keep-alive messages.
/// ///
/// Defaults to no keep-alive messages. /// Defaults to no keep-alive messages.