From 75fa7b4aa672a8a10afcd11016a1b80e0e6f0f02 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Mon, 3 Jan 2022 01:12:39 -0500 Subject: [PATCH] fix: make tests pass --- examples/async.rs | 92 ++++++++++++++++++++++++++ packages/core/README.md | 2 +- packages/core/src/lib.rs | 6 +- packages/core/src/virtual_dom.rs | 18 ++--- packages/core/tests/miri_stress.rs | 7 +- packages/hooks/src/use_shared_state.rs | 4 +- packages/html/src/events.rs | 6 +- packages/router/src/link.rs | 2 +- packages/web/src/lib.rs | 4 +- 9 files changed, 116 insertions(+), 25 deletions(-) create mode 100644 examples/async.rs diff --git a/examples/async.rs b/examples/async.rs new file mode 100644 index 000000000..3b35ae174 --- /dev/null +++ b/examples/async.rs @@ -0,0 +1,92 @@ +//! Render a bunch of doggos! +//! + +use std::collections::HashMap; + +use dioxus::prelude::*; + +fn main() { + dioxus::desktop::launch(app); +} + +#[derive(Debug, Clone, PartialEq, serde::Deserialize)] +struct ListBreeds { + message: HashMap>, +} + +fn app(cx: Scope) -> Element { + let fut = use_future(&cx, || async move { + reqwest::get("https://dog.ceo/api/breeds/list/all") + .await + .unwrap() + .json::() + .await + }); + + let selected_breed = use_state(&cx, || None); + + match fut.value() { + Some(Ok(breeds)) => cx.render(rsx! { + div { + h1 {"Select a dog breed!"} + + div { display: "flex", + ul { flex: "50%", + breeds.message.keys().map(|breed| rsx!( + li { + button { + onclick: move |_| selected_breed.set(Some(breed.clone())), + "{breed}" + } + } + )) + } + div { flex: "50%", + match &*selected_breed { + Some(breed) => rsx!( Breed { breed: breed.clone() } ), + None => rsx!("No Breed selected"), + } + } + } + } + }), + Some(Err(e)) => cx.render(rsx! { + div { "Error fetching breeds" } + }), + None => cx.render(rsx! { + div { "Loading dogs..." } + }), + } +} + +#[inline_props] +fn Breed(cx: Scope, breed: String) -> Element { + #[derive(serde::Deserialize)] + struct DogApi { + message: String, + } + + let endpoint = format!("https://dog.ceo/api/breed/{}/images/random", breed); + + let fut = use_future(&cx, || async move { + reqwest::get(endpoint).await.unwrap().json::().await + }); + + cx.render(match fut.value() { + Some(Ok(resp)) => rsx! { + button { + onclick: move |_| fut.restart(), + "Click to fetch another doggo" + } + div { + img { + max_width: "500px", + max_height: "500px", + src: "{resp.message}", + } + } + }, + Some(Err(_)) => rsx! { div { "loading dogs failed" } }, + None => rsx! { div { "loading dogs..." } }, + }) +} diff --git a/packages/core/README.md b/packages/core/README.md index 87d724415..6313b1415 100644 --- a/packages/core/README.md +++ b/packages/core/README.md @@ -5,7 +5,7 @@ This is the core crate for the Dioxus Virtual DOM. This README will focus on the To build new apps with Dioxus or to extend the ecosystem with new hooks or components, use the higher-level `dioxus` crate with the appropriate feature flags. -```rust +```rust, ignore fn app(cx: Scope) -> Element { rsx!(cx, div { "hello world" }) } diff --git a/packages/core/src/lib.rs b/packages/core/src/lib.rs index 61fb2c264..494c772a1 100644 --- a/packages/core/src/lib.rs +++ b/packages/core/src/lib.rs @@ -29,7 +29,7 @@ pub(crate) mod innerlude { /// /// ## Rust-Style /// - /// ```rust + /// ```rust, ignore /// fn example(cx: Scope) -> Element { /// // ... /// } @@ -39,7 +39,7 @@ pub(crate) mod innerlude { /// ) /// ``` /// ## React-Style - /// ```rust + /// ```rust, ignore /// fn Example(cx: Scope) -> Element { /// // ... /// } @@ -52,7 +52,7 @@ pub(crate) mod innerlude { /// ## As a closure /// This particular type alias lets you even use static closures for pure/static components: /// - /// ```rust + /// ```rust, ignore /// static Example: Component = |cx| { /// // ... /// }; diff --git a/packages/core/src/virtual_dom.rs b/packages/core/src/virtual_dom.rs index 2d16bbdc2..6e6aea94a 100644 --- a/packages/core/src/virtual_dom.rs +++ b/packages/core/src/virtual_dom.rs @@ -185,7 +185,7 @@ impl VirtualDom { /// This is useful when the VirtualDom must be driven from outside a thread and it doesn't make sense to wait for the /// VirtualDom to be created just to retrieve its channel receiver. /// - /// ```rust + /// ```rust, ignore /// let channel = futures_channel::mpsc::unbounded(); /// let dom = VirtualDom::new_with_scheduler(Example, (), channel); /// ``` @@ -228,7 +228,7 @@ impl VirtualDom { /// /// # Example /// - /// ```rust + /// ```rust, ignore /// let mut dom = VirtualDom::new(example); /// dom.rebuild(); /// @@ -547,7 +547,7 @@ impl VirtualDom { /// /// Useful when needing to render nodes from outside the VirtualDom, such as in a test. /// - /// ```rust + /// ```rust, ignore /// fn Base(cx: Scope) -> Element { /// rsx!(cx, div {}) /// } @@ -567,7 +567,7 @@ impl VirtualDom { /// /// Useful when needing to render nodes from outside the VirtualDom, such as in a test. /// - /// ```rust + /// ```rust, ignore /// fn Base(cx: Scope) -> Element { /// rsx!(cx, div {}) /// } @@ -589,7 +589,7 @@ impl VirtualDom { /// Useful when needing to render nodes from outside the VirtualDom, such as in a test. /// /// - /// ```rust + /// ```rust, ignore /// fn Base(cx: Scope) -> Element { /// rsx!(cx, div {}) /// } @@ -612,7 +612,7 @@ impl VirtualDom { /// Useful when needing to diff two rsx! calls from outside the VirtualDom, such as in a test. /// /// - /// ```rust + /// ```rust, ignore /// fn Base(cx: Scope) -> Element { /// rsx!(cx, div {}) /// } @@ -756,7 +756,7 @@ pub enum SchedulerMsg { /// `Send` will not be lifted. The entire `UserEvent` must also be `Send + Sync` due to its use in the scheduler channel. /// /// # Example -/// ```rust +/// ```rust, ignore /// fn App(cx: Scope) -> Element { /// rsx!(cx, div { /// onclick: move |_| println!("Clicked!") @@ -957,7 +957,7 @@ pub fn Fragment<'a>(cx: Scope<'a, FragmentProps<'a>>) -> Element { /// ## Example /// /// For props that are 'static: -/// ```rust, ignore ignore +/// ```rust, ignore /// #[derive(Props, PartialEq)] /// struct MyProps { /// data: String @@ -966,7 +966,7 @@ pub fn Fragment<'a>(cx: Scope<'a, FragmentProps<'a>>) -> Element { /// /// For props that borrow: /// -/// ```rust, ignore ignore +/// ```rust, ignore /// #[derive(Props)] /// struct MyProps<'a >{ /// data: &'a str diff --git a/packages/core/tests/miri_stress.rs b/packages/core/tests/miri_stress.rs index 4541d593a..2d381fddb 100644 --- a/packages/core/tests/miri_stress.rs +++ b/packages/core/tests/miri_stress.rs @@ -9,13 +9,12 @@ Specifically: - [ ] Async isn't busted */ -use dioxus::{prelude::*, DomEdit, SchedulerMsg, ScopeId}; +use dioxus::{prelude::*, SchedulerMsg, ScopeId}; use dioxus_core as dioxus; use dioxus_core_macro::*; use dioxus_html as dioxus_elements; mod test_logging; -use DomEdit::*; const IS_LOGGING_ENABLED: bool = false; @@ -97,7 +96,7 @@ fn memo_works_properly() { let name = cx.use_hook(|_| String::from("asd")); cx.render(rsx!( - div { "Hello, world!" } + div { "Hello, world! {name}" } child(na: "asdfg".to_string()) )) } @@ -149,7 +148,7 @@ fn free_works_on_root_props() { impl Drop for Custom { fn drop(&mut self) { - dbg!("dropped!"); + dbg!("dropped! {}", &self.val); } } diff --git a/packages/hooks/src/use_shared_state.rs b/packages/hooks/src/use_shared_state.rs index ac02c0e72..3d25a5ac3 100644 --- a/packages/hooks/src/use_shared_state.rs +++ b/packages/hooks/src/use_shared_state.rs @@ -39,14 +39,14 @@ impl ProvidedStateInner { /// /// ## Provider /// -/// ```rust +/// ```rust, ignore /// /// /// ``` /// /// ## Consumer /// -/// ```rust +/// ```rust, ignore /// /// /// ``` diff --git a/packages/html/src/events.rs b/packages/html/src/events.rs index c0b432cbe..52c5b906e 100644 --- a/packages/html/src/events.rs +++ b/packages/html/src/events.rs @@ -122,7 +122,7 @@ pub mod on { /// The MouseEvent interface represents events that occur due to the user interacting with a pointing device (such as a mouse). /// /// ## Trait implementation: - /// ```rust + /// ```rust, ignore /// fn alt_key(&self) -> bool; /// fn button(&self) -> i16; /// fn buttons(&self) -> u16; @@ -416,7 +416,7 @@ pub mod on { /// /// # Example /// - /// ```rust + /// ```rust, ignore /// match event.key().as_str() { /// "Esc" | "Escape" => {} /// "ArrowDown" => {} @@ -435,7 +435,7 @@ pub mod on { /// /// ## Example /// - /// ```rust + /// ```rust, ignore /// use dioxus::KeyCode; /// match event.key_code() { /// KeyCode::Escape => {} diff --git a/packages/router/src/link.rs b/packages/router/src/link.rs index 16fc707fe..4ad7a45d0 100644 --- a/packages/router/src/link.rs +++ b/packages/router/src/link.rs @@ -13,7 +13,7 @@ pub struct LinkProps<'a, R: Routable> { /// /// You can either put it your own inline method or just autoderive the route using `derive(Routable)` /// - /// ```rust + /// ```rust, ignore /// /// Link { to: Route::Home, href: |_| "home".to_string() } /// diff --git a/packages/web/src/lib.rs b/packages/web/src/lib.rs index 251597d49..7a72e5641 100644 --- a/packages/web/src/lib.rs +++ b/packages/web/src/lib.rs @@ -79,7 +79,7 @@ mod ric_raf; /// /// # Example /// -/// ```rust +/// ```rust, ignore /// fn main() { /// dioxus_web::launch(App); /// } @@ -98,7 +98,7 @@ pub fn launch(root_component: Component) { /// /// # Example /// -/// ```rust +/// ```rust, ignore /// fn main() { /// dioxus_web::launch_with_props(App, RootProps { name: String::from("joe") }); /// }