dioxus_fullstack_core/server_future.rs
1use crate::Transportable;
2use dioxus_core::{suspend, use_hook, RenderError};
3use dioxus_hooks::*;
4use dioxus_signals::ReadableExt;
5use std::future::Future;
6
7/// Runs a future with a manual list of dependencies and returns a resource with the result if the future is finished or a suspended error if it is still running.
8///
9///
10/// On the server, this will wait until the future is resolved before continuing to render. When the future is resolved, the result will be serialized into the page and hydrated on the client without rerunning the future.
11///
12///
13/// <div class="warning">
14///
15/// Unlike [`use_resource`] dependencies are only tracked inside the function that spawns the async block, not the async block itself.
16///
17/// ```rust, no_run
18/// # use dioxus::prelude::*;
19/// // ❌ The future inside of use_server_future is not reactive
20/// let id = use_signal(|| 0);
21/// use_server_future(move || {
22/// async move {
23/// // But the future is not reactive which means that the future will not subscribe to any reads here
24/// println!("{id}");
25/// }
26/// });
27/// // ✅ The closure that creates the future for use_server_future is reactive
28/// let id = use_signal(|| 0);
29/// use_server_future(move || {
30/// // The closure itself is reactive which means the future will subscribe to any signals you read here
31/// let cloned_id = id();
32/// async move {
33/// // But the future is not reactive which means that the future will not subscribe to any reads here
34/// println!("{cloned_id}");
35/// }
36/// });
37/// ```
38///
39/// </div>
40///
41/// # Example
42///
43/// ```rust, no_run
44/// # use dioxus::prelude::*;
45/// # async fn fetch_article(id: u32) -> String { unimplemented!() }
46/// use dioxus::prelude::*;
47///
48/// fn App() -> Element {
49/// let mut article_id = use_signal(|| 0);
50/// // `use_server_future` will spawn a task that runs on the server and serializes the result to send to the client.
51/// // The future will rerun any time the
52/// // Since we bubble up the suspense with `?`, the server will wait for the future to resolve before rendering
53/// let article = use_server_future(move || fetch_article(article_id()))?;
54///
55/// rsx! {
56/// "{article().unwrap()}"
57/// }
58/// }
59/// ```
60#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
61#[track_caller]
62pub fn use_server_future<T, F, M>(
63 mut future: impl FnMut() -> F + 'static,
64) -> Result<Resource<T>, RenderError>
65where
66 F: Future<Output = T> + 'static,
67 T: Transportable<M>,
68 M: 'static,
69{
70 let serialize_context = use_hook(crate::transport::serialize_context);
71
72 // We always create a storage entry, even if the data isn't ready yet to make it possible to deserialize pending server futures on the client
73 #[allow(unused)]
74 let storage_entry: crate::transport::SerializeContextEntry<T> =
75 use_hook(|| serialize_context.create_entry());
76
77 #[cfg(feature = "server")]
78 let caller = std::panic::Location::caller();
79
80 // If this is the first run and we are on the web client, the data might be cached
81 #[cfg(feature = "web")]
82 let initial_web_result =
83 use_hook(|| std::rc::Rc::new(std::cell::RefCell::new(Some(storage_entry.get()))));
84
85 let resource = use_resource(move || {
86 #[cfg(feature = "server")]
87 let storage_entry = storage_entry.clone();
88
89 let user_fut = future();
90
91 #[cfg(feature = "web")]
92 let initial_web_result = initial_web_result.clone();
93
94 #[allow(clippy::let_and_return)]
95 async move {
96 // If this is the first run and we are on the web client, the data might be cached
97 #[cfg(feature = "web")]
98 match initial_web_result.take() {
99 // The data was deserialized successfully from the server
100 Some(Ok(o)) => return o,
101
102 // The data is still pending from the server. Don't try to resolve it on the client
103 Some(Err(crate::transport::TakeDataError::DataPending)) => {
104 std::future::pending::<()>().await
105 }
106
107 // The data was not available on the server, rerun the future
108 Some(Err(_)) => {}
109
110 // This isn't the first run, so we don't need do anything
111 None => {}
112 }
113
114 // Otherwise just run the future itself
115 let out = user_fut.await;
116
117 // If this is the first run and we are on the server, cache the data in the slot we reserved for it
118 #[cfg(feature = "server")]
119 storage_entry.insert(&out, caller);
120
121 out
122 }
123 });
124
125 // On the first run, force this task to be polled right away in case its value is ready
126 use_hook(|| {
127 let _ = resource.task().poll_now();
128 });
129
130 // Suspend if the value isn't ready
131 if resource.state().cloned() == UseResourceState::Pending {
132 let task = resource.task();
133 if !task.paused() {
134 return Err(suspend(task).unwrap_err());
135 }
136 }
137
138 Ok(resource)
139}