mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-23 04:33:06 +00:00
wip: suspense ergonomics
This commit is contained in:
parent
bf21c82de0
commit
0f0fc3e4b8
1 changed files with 26 additions and 19 deletions
|
@ -8,9 +8,7 @@
|
||||||
//! and all work will stop. In this example, we store the future in a hook so we can always resume it.
|
//! and all work will stop. In this example, we store the future in a hook so we can always resume it.
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(serde::Deserialize)]
|
||||||
struct DogApi {
|
struct DogApi {
|
||||||
message: String,
|
message: String,
|
||||||
|
@ -18,9 +16,11 @@ struct DogApi {
|
||||||
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
|
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
|
||||||
|
|
||||||
pub static App: FC<()> = |cx| {
|
pub static App: FC<()> = |cx| {
|
||||||
let doggo = use_fetch(&cx, ENDPOINT, |cx, res: surf::Result<DogApi>| match res {
|
let doggo = use_future_effect(&cx, move || async move {
|
||||||
Ok(res) => rsx!(in cx, img { src: "{res.message}"}),
|
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
|
||||||
Err(_) => rsx!(in cx, p { "Failed to load doggo :("}),
|
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
|
||||||
|
Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.render(rsx!(
|
cx.render(rsx!(
|
||||||
|
@ -31,22 +31,29 @@ pub static App: FC<()> = |cx| {
|
||||||
))
|
))
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use dioxus_core::virtual_dom::SuspendedContext;
|
||||||
|
use futures::Future;
|
||||||
|
use futures::FutureExt;
|
||||||
|
use std::pin::Pin;
|
||||||
fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
|
fn use_fetch<'a, T: serde::de::DeserializeOwned + 'static>(
|
||||||
cx: &impl Scoped<'a>,
|
cx: &impl Scoped<'a>,
|
||||||
url: &str,
|
url: &str,
|
||||||
g: impl FnOnce(dioxus_core::virtual_dom::SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
|
g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
|
||||||
) -> VNode<'a> {
|
) -> VNode<'a> {
|
||||||
use futures::Future;
|
// essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
|
||||||
use futures::FutureExt;
|
let fetch_promise = cx.use_hook(
|
||||||
use std::pin::Pin;
|
move || surf::get(url).recv_json::<T>().boxed_local(),
|
||||||
|
// just pass the future through
|
||||||
// essentially we're doing a "use_effect" but with no dependent props
|
|p| p,
|
||||||
let doggo_promise: &'a mut Pin<Box<dyn Future<Output = surf::Result<T>> + Send + 'static>> = cx
|
|_| (),
|
||||||
.use_hook(
|
);
|
||||||
move || surf::get(url).recv_json::<T>().boxed(),
|
cx.suspend(fetch_promise, g)
|
||||||
// just pass the future through
|
}
|
||||||
|p| p,
|
|
||||||
|_| (),
|
/// Spawns the future only when the inputs change
|
||||||
);
|
fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
|
||||||
cx.suspend(doggo_promise, g)
|
cx: &impl Scoped<'a>,
|
||||||
|
g: impl FnOnce() -> F + 'a,
|
||||||
|
) -> VNode<'a> {
|
||||||
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue