dioxus_fullstack_hooks/streaming.rs
1use dioxus_core::try_consume_context;
2use dioxus_signals::{ReadableExt, Signal, WritableExt};
3
4/// The status of the streaming response
5#[derive(Clone, Copy, Debug, PartialEq)]
6pub enum StreamingStatus {
7 /// The initial chunk is still being rendered. The http response parts can still be modified with
8 /// [DioxusServerContext::response_parts_mut](https://docs.rs/dioxus-fullstack/0.6.3/dioxus_fullstack/prelude/struct.DioxusServerContext.html#method.response_parts_mut).
9 RenderingInitialChunk,
10 /// The initial chunk has been committed and the response is now streaming. The http response parts
11 /// have already been sent to the client and can no longer be modified.
12 InitialChunkCommitted,
13}
14
15/// The context dioxus fullstack provides for the status of streaming responses on the server
16#[derive(Clone, Copy, Debug, PartialEq)]
17pub struct StreamingContext {
18 current_status: Signal<StreamingStatus>,
19}
20
21impl Default for StreamingContext {
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl StreamingContext {
28 /// Create a new streaming context. You should not need to call this directly. Dioxus fullstack will
29 /// provide this context for you.
30 pub fn new() -> Self {
31 Self {
32 current_status: Signal::new(StreamingStatus::RenderingInitialChunk),
33 }
34 }
35
36 /// Commit the initial chunk of the response. This will be called automatically if you are using the
37 /// dioxus router when the suspense boundary above the router is resolved. Otherwise, you will need
38 /// to call this manually to start the streaming part of the response.
39 ///
40 /// Once this method has been called, the http response parts can no longer be modified.
41 pub fn commit_initial_chunk(&mut self) {
42 self.current_status
43 .set(StreamingStatus::InitialChunkCommitted);
44 }
45
46 /// Get the current status of the streaming response. This method is reactive and will cause
47 /// the current reactive context to rerun when the status changes.
48 pub fn current_status(&self) -> StreamingStatus {
49 *self.current_status.read()
50 }
51}
52
53/// Commit the initial chunk of the response. This will be called automatically if you are using the
54/// dioxus router when the suspense boundary above the router is resolved. Otherwise, you will need
55/// to call this manually to start the streaming part of the response.
56///
57/// On the client, this will do nothing.
58///
59/// # Example
60/// ```rust, no_run
61/// # use dioxus::prelude::*;
62/// # use dioxus_fullstack_hooks::*;
63/// # fn Children() -> Element { unimplemented!() }
64/// fn App() -> Element {
65/// // This will start streaming immediately after the current render is complete.
66/// use_hook(commit_initial_chunk);
67///
68/// rsx! { Children {} }
69/// }
70/// ```
71pub fn commit_initial_chunk() {
72 if let Some(mut streaming) = try_consume_context::<StreamingContext>() {
73 streaming.commit_initial_chunk();
74 }
75}
76
77/// Get the current status of the streaming response. This method is reactive and will cause
78/// the current reactive context to rerun when the status changes.
79///
80/// On the client, this will always return `StreamingStatus::InitialChunkCommitted`.
81///
82/// # Example
83/// ```rust, no_run
84/// # use dioxus::prelude::*;
85/// # use dioxus_fullstack_hooks::*;
86/// #[component]
87/// fn MetaTitle(title: String) -> Element {
88/// // If streaming has already started, warn the user that the meta tag will not show
89/// // up in the initial chunk.
90/// use_hook(|| {
91/// if current_status() == StreamingStatus::InitialChunkCommitted {
92/// dioxus::logger::tracing::warn!("Since `MetaTitle` was rendered after the initial chunk was committed, the meta tag will not show up in the head without javascript enabled.");
93/// }
94/// });
95///
96/// rsx! { meta { property: "og:title", content: title } }
97/// }
98/// ```
99pub fn current_status() -> StreamingStatus {
100 if let Some(streaming) = try_consume_context::<StreamingContext>() {
101 streaming.current_status()
102 } else {
103 StreamingStatus::InitialChunkCommitted
104 }
105}