wip: move examples around

This commit is contained in:
Jonathan Kelley committed 2021-07-11 18:39:45 -04:00
1 parent 269e81b0fd
commit 304259d818
24 files changed
+81 -71

No files matched your search

+23 -19
View File
@@ -16,7 +16,7 @@ struct DogApi {
const ENDPOINT: &str = "https://dog.ceo/api/breeds/image/random";
pub static App: FC<()> = |cx| {
let doggo = use_future_effect(&cx, move || async move {
let doggo = use_future_effect(cx, move || async move {
match surf::get(ENDPOINT).recv_json::<DogApi>().await {
Ok(res) => rsx!(in cx, img { src: "{res.message}" }),
Err(_) => rsx!(in cx, div { "No doggos for you :(" }),
@@ -31,29 +31,33 @@ pub static App: FC<()> = |cx| {
))
};
use dioxus_core::virtual_dom::SuspendedContext;
// 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>(
cx: &impl Scoped<'a>,
url: &str,
g: impl FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
) -> VNode<'a> {
// essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
let fetch_promise = cx.use_hook(
move || surf::get(url).recv_json::<T>().boxed_local(),
// just pass the future through
|p| p,
|_| (),
);
cx.suspend(fetch_promise, g)
fn use_fetch<'a, T, P, F>(cx: Context<P>, url: &str, g: F) -> VNode<'a>
where
T: serde::de::DeserializeOwned + 'static,
// F: FnOnce(SuspendedContext, surf::Result<T>) -> VNode<'a> + 'a,
F: FnOnce((), surf::Result<T>) -> VNode<'a> + 'a,
{
todo!()
// // essentially we're doing a "use_effect" but with no dependent props or post-render shenanigans
// let fetch_promise = cx.use_hook(
// move || surf::get(url).recv_json::<T>().boxed_local(),
// // just pass the future through
// |p| p,
// |_| (),
// );
// cx.suspend(fetch_promise, g)
}
/// Spawns the future only when the inputs change
fn use_future_effect<'a, 'b, F: Future<Output = VNode<'b>>>(
cx: &impl Scoped<'a>,
g: impl FnOnce() -> F + 'a,
) -> VNode<'a> {
fn use_future_effect<'a, 'b, F, P, F2>(cx: Context<P>, g: F2) -> VNode<'a>
where
F: Future<Output = VNode<'b>>,
F2: FnOnce() -> F + 'a,
{
todo!()
}