44//!
55//! Example:
66//! ```
7+ //! use std::time::Duration;
8+ //!
79//! let your_chunk_handler = std::sync::Arc::new(|chunk: Vec<u8>| {
810//! if chunk.is_empty() {
9- //! return std::ops::ControlFlow ::Break(()) ;
11+ //! return ehttp::streaming::Flow ::Break;
1012//! }
1113//!
1214//! println!("received chunk: {} bytes", chunk.len());
13- //! std::ops::ControlFlow::Continue(())
15+ //!
16+ //! // Example of back-pressure: wait if chunk is large
17+ //! if chunk.len() > 1024 * 1024 {
18+ //! ehttp::streaming::Flow::Wait(Duration::from_millis(100))
19+ //! } else {
20+ //! ehttp::streaming::Flow::Continue
21+ //! }
1422//! });
1523//!
1624//! let url = "https://www.example.com";
2028//! Ok(part) => part,
2129//! Err(err) => {
2230//! eprintln!("an error occurred while streaming `{url}`: {err}");
23- //! return std::ops::ControlFlow ::Break(()) ;
31+ //! return ehttp::streaming::Flow ::Break;
2432//! }
2533//! };
2634//!
2735//! match part {
2836//! ehttp::streaming::Part::Response(response) => {
2937//! println!("Status code: {:?}", response.status);
3038//! if response.ok {
31- //! std::ops::ControlFlow ::Continue(())
39+ //! ehttp::streaming::Flow ::Continue
3240//! } else {
33- //! std::ops::ControlFlow ::Break(())
41+ //! ehttp::streaming::Flow ::Break
3442//! }
3543//! }
3644//! ehttp::streaming::Part::Chunk(chunk) => {
4250//!
4351//! The streaming fetch works like the non-streaming fetch, but instead
4452//! of receiving the response in full, you receive parts of the response
45- //! as they are streamed in.
46-
47- use std:: ops:: ControlFlow ;
53+ //! as they are streamed in. The callback can return [`Flow::Wait`] to implement
54+ //! back-pressure by pausing the stream for a specified duration.
4855
4956use crate :: Request ;
5057
5158/// Performs a HTTP requests and calls the given callback once for the initial response,
5259/// and then once for each chunk in the response body.
5360///
54- /// You can abort the fetch by returning [`ControlFlow::Break`] from the callback.
61+ /// You can abort the fetch by returning [`types::Flow::Break`] from the callback,
62+ /// or implement back-pressure by returning [`types::Flow::Wait`] with a duration.
5563pub fn fetch (
5664 request : Request ,
57- on_data : impl ' static + Send + Fn ( crate :: Result < types:: Part > ) -> ControlFlow < ( ) > ,
65+ on_data : impl ' static + Send + Fn ( crate :: Result < types:: Part > ) -> types :: Flow ,
5866) {
5967 #[ cfg( not( target_arch = "wasm32" ) ) ]
6068 native:: fetch_streaming ( request, Box :: new ( on_data) ) ;
@@ -75,4 +83,4 @@ pub use web::fetch_async_streaming;
7583
7684mod types;
7785
78- pub use self :: types:: Part ;
86+ pub use self :: types:: { Flow , Part } ;
0 commit comments