Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example-projects/ecommerce-site/src/components/home.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
use dioxus::prelude::*;

pub(crate) fn Home() -> Element {
let products = use_server_future(|| fetch_products(10, Sort::Ascending))?;
let products = use_hydration_resource(|| fetch_products(10, Sort::Ascending))?;
let products = products().unwrap()?;

rsx! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn product_page(product_id: ReadSignal<usize>) -> Element {
let mut quantity = use_signal(|| 1);
let mut size = use_signal(Size::default);

let product = use_server_future(move || fetch_product(product_id()))?;
let product = use_hydration_resource(move || fetch_product(product_id()))?;

let Product {
title,
Expand Down
8 changes: 4 additions & 4 deletions example-projects/fullstack-hackernews/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn Homepage(story: ReadSignal<PreviewState>) -> Element {

#[component]
fn Stories() -> Element {
let stories: Resource<dioxus::Result<Vec<i64>>> = use_server_future(move || async move {
let stories: Resource<dioxus::Result<Vec<i64>>> = use_hydration_resource(move || async move {
let url = format!("{}topstories.json", BASE_API_URL);
let mut stories_ids = reqwest::get(&url).await?.json::<Vec<i64>>().await?;
stories_ids.truncate(30);
Expand All @@ -85,7 +85,7 @@ fn Stories() -> Element {

#[component]
fn StoryListing(story: ReadSignal<i64>) -> Element {
let story = use_server_future(move || get_story(story()))?;
let story = use_hydration_resource(move || get_story(story()))?;

let StoryItem {
title,
Expand Down Expand Up @@ -175,7 +175,7 @@ fn Preview(story: ReadSignal<PreviewState>) -> Element {
return rsx! {"Click on a story to preview it here"};
};

let story = use_server_future(use_reactive!(|id| get_story(id)))?;
let story = use_hydration_resource(use_reactive!(|id| get_story(id)))?;

let story = story().unwrap()?;

Expand All @@ -196,7 +196,7 @@ fn Preview(story: ReadSignal<PreviewState>) -> Element {
#[component]
fn Comment(comment: i64) -> Element {
let comment: Resource<dioxus::Result<CommentData>> =
use_server_future(use_reactive!(|comment| async move {
use_hydration_resource(use_reactive!(|comment| async move {
let url = format!("{}{}{}.json", BASE_API_URL, ITEM_API, comment);
let mut comment = reqwest::get(&url).await?.json::<CommentData>().await?;
Ok(comment)
Expand Down
2 changes: 1 addition & 1 deletion examples/fullstack-hello-world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
fn app() -> Element {
let mut count = use_signal(|| 0);
let mut text = use_signal(|| "...".to_string());
let server_future = use_server_future(get_server_data)?;
let server_future = use_hydration_resource(get_server_data)?;

rsx! {
document::Link { href: asset!("/assets/hello.css"), rel: "stylesheet" }
Expand Down
3 changes: 2 additions & 1 deletion packages/dioxus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ pub mod prelude {
#[cfg_attr(docsrs, doc(cfg(feature = "fullstack")))]
#[doc(inline)]
pub use dioxus_fullstack::{
server, server_fn, use_server_cached, use_server_future, ServerFnError, ServerFnResult,
server, server_fn, use_hydration_hook, use_hydration_resource, ServerFnError,
ServerFnResult,
};

#[cfg(feature = "server")]
Expand Down
6 changes: 3 additions & 3 deletions packages/fullstack-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ To start using this crate, you can run the following command:
cargo add dioxus-fullstack-hooks
```

Then you can use hooks like `use_server_future` in your components:
Then you can use hooks like `use_hydration_resource` in your components:

```rust
use dioxus::prelude::*;
Expand All @@ -20,10 +20,10 @@ async fn fetch_article(id: u32) -> String {

fn App() -> Element {
let mut article_id = use_signal(|| 0);
// `use_server_future` will spawn a task that runs on the server and serializes the result to send to the client.
// `use_hydration_resource` will spawn a task that runs on the server and serializes the result to send to the client.
// The future will rerun any time the
// Since we bubble up the suspense with `?`, the server will wait for the future to resolve before rendering
let article = use_server_future(move || fetch_article(article_id()))?;
let article = use_hydration_resource(move || fetch_article(article_id()))?;

rsx! {
"{article().unwrap()}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ use serde::{de::DeserializeOwned, Serialize};
/// use dioxus::prelude::*;
///
/// fn app() -> Element {
/// let state1 = use_server_cached(|| {
/// let state1 = use_hydration_hook(|| {
/// 1234
/// });
///
/// unimplemented!()
/// }
/// ```
#[track_caller]
pub fn use_server_cached<O: 'static + Clone + Serialize + DeserializeOwned>(
pub fn use_hydration_hook<O: 'static + Clone + Serialize + DeserializeOwned>(
server_fn: impl Fn() -> O,
) -> O {
let location = std::panic::Location::caller();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ use std::future::Future;
///
/// ```rust, no_run
/// # use dioxus::prelude::*;
/// // ❌ The future inside of use_server_future is not reactive
/// // ❌ The future inside of use_hydration_resource is not reactive
/// let id = use_signal(|| 0);
/// use_server_future(move || {
/// use_hydration_resource(move || {
/// async move {
/// // But the future is not reactive which means that the future will not subscribe to any reads here
/// println!("{id}");
/// }
/// });
/// // ✅ The closure that creates the future for use_server_future is reactive
/// // ✅ The closure that creates the future for use_hydration_resource is reactive
/// let id = use_signal(|| 0);
/// use_server_future(move || {
/// use_hydration_resource(move || {
/// // The closure itself is reactive which means the future will subscribe to any signals you read here
/// let cloned_id = id();
/// async move {
Expand All @@ -47,10 +47,10 @@ use std::future::Future;
///
/// fn App() -> Element {
/// let mut article_id = use_signal(|| 0);
/// // `use_server_future` will spawn a task that runs on the server and serializes the result to send to the client.
/// // `use_hydration_resource` will spawn a task that runs on the server and serializes the result to send to the client.
/// // The future will rerun any time the
/// // Since we bubble up the suspense with `?`, the server will wait for the future to resolve before rendering
/// let article = use_server_future(move || fetch_article(article_id()))?;
/// let article = use_hydration_resource(move || fetch_article(article_id()))?;
///
/// rsx! {
/// "{article().unwrap()}"
Expand All @@ -59,7 +59,7 @@ use std::future::Future;
/// ```
#[must_use = "Consider using `cx.spawn` to run a future without reading its value"]
#[track_caller]
pub fn use_server_future<T, F>(
pub fn use_hydration_resource<T, F>(
mut future: impl FnMut() -> F + 'static,
) -> Result<Resource<T>, RenderError>
where
Expand Down
8 changes: 4 additions & 4 deletions packages/fullstack-hooks/src/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod server_cached;
pub use server_cached::*;
mod server_future;
pub use server_future::*;
mod hydration_hook;
pub use hydration_hook::*;
mod hydration_resource;
pub use hydration_resource::*;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ fn main() {
}

fn app() -> Element {
let server_features = use_server_future(get_server_features)?.unwrap().unwrap();
let server_features = use_hydration_resource(get_server_features)?
.unwrap()
.unwrap();
let mut client_features = use_signal(Vec::new);

use_effect(move || {
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-tests/fullstack/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn OnMounted() -> Element {

#[component]
fn DefaultServerFnCodec() -> Element {
let resource = use_server_future(|| get_server_data_empty_vec(Vec::new()))?;
let resource = use_hydration_resource(|| get_server_data_empty_vec(Vec::new()))?;
let empty_vec = resource.unwrap().unwrap();
assert!(empty_vec.is_empty());

Expand Down Expand Up @@ -138,7 +138,7 @@ fn Errors() -> Element {

#[component]
pub fn ThrowsError() -> Element {
use_server_future(server_error)?.unwrap()?;
use_hydration_resource(server_error)?.unwrap()?;
rsx! {
"success"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-tests/nested-suspense/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn MessageWithLoader(id: usize) -> Element {

#[component]
fn LoadTitle() -> Element {
let title = use_server_future(move || server_content(0))?()
let title = use_hydration_resource(move || server_content(0))?()
.unwrap()
.unwrap();

Expand All @@ -52,7 +52,7 @@ fn LoadTitle() -> Element {

#[component]
fn Message(id: usize) -> Element {
let message = use_server_future(move || server_content(id))?()
let message = use_hydration_resource(move || server_content(id))?()
.unwrap()
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions packages/playwright-tests/suspense-carousel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl ResolvedOn {

#[component]
fn SuspendedComponent(id: i32) -> Element {
let resolved_on = use_server_future(move || async move {
let resolved_on = use_hydration_resource(move || async move {
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
ResolvedOn::CURRENT
})?()
Expand Down Expand Up @@ -90,7 +90,7 @@ fn SuspendedComponent(id: i32) -> Element {

#[component]
fn NestedSuspendedComponent(id: i32) -> Element {
let resolved_on = use_server_future(move || async move {
let resolved_on = use_hydration_resource(move || async move {
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
ResolvedOn::CURRENT
})?()
Expand Down
6 changes: 3 additions & 3 deletions packages/server/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl SsrRendererPool {
break;
}

// Wait for new async work that runs during suspense (mainly use_server_futures)
// Wait for new async work that runs during suspense (mainly use_hydration_resources)
virtual_dom.wait_for_suspense_work().await;

// Do that async work
Expand Down Expand Up @@ -679,8 +679,8 @@ impl FullstackHTMLTemplate {
) -> Result<(), dioxus_isrg::IncrementalRendererError> {
let ServeConfig { index, .. } = &self.cfg;

// Collect the initial server data from the root node. For most apps, no use_server_futures will be resolved initially, so this will be full on `None`s.
// Sending down those Nones are still important to tell the client not to run the use_server_futures that are already running on the backend
// Collect the initial server data from the root node. For most apps, no use_hydration_resources will be resolved initially, so this will be full on `None`s.
// Sending down those Nones are still important to tell the client not to run the use_hydration_resources that are already running on the backend
let resolved_data = serialize_server_data(virtual_dom, ScopeId::ROOT);
// We always send down the data required to hydrate components on the client
let raw_data = resolved_data.data;
Expand Down
Loading