chore: make for loops not the same

This commit is contained in:
Jonathan Kelley committed 2022-11-17 22:55:46 -08:00
1 parent 20f9957fbe
commit 6c677e64da
4 files changed
+33 -29

No files matched your search

+7 -9
View File
@@ -4,6 +4,7 @@ use std::collections::HashMap;
fn main() { fn main() {
dioxus_desktop::launch(|cx| { dioxus_desktop::launch(|cx| {
cx.render(rsx! { cx.render(rsx! {
h1 {"Loading...."}
app_root {} app_root {}
}) })
}); });
@@ -16,6 +17,7 @@ struct ListBreeds {
async fn app_root(cx: Scope<'_>) -> Element { async fn app_root(cx: Scope<'_>) -> Element {
let breed = use_state(cx, || "deerhound".to_string()); let breed = use_state(cx, || "deerhound".to_string());
let breeds = use_future!(cx, || async move { let breeds = use_future!(cx, || async move {
reqwest::get("https://dog.ceo/api/breeds/list/all") reqwest::get("https://dog.ceo/api/breeds/list/all")
.await .await
@@ -31,19 +33,15 @@ async fn app_root(cx: Scope<'_>) -> Element {
div { display: "flex", div { display: "flex",
ul { flex: "50%", ul { flex: "50%",
for cur_breed in breeds.message.keys().take(10) { for cur_breed in breeds.message.keys().take(10) {
rsx! { li { key: "{cur_breed}",
li { key: "{cur_breed}", button {
button { onclick: move |_| breed.set(cur_breed.clone()),
onclick: move |_| breed.set(cur_breed.clone()), "{cur_breed}"
"{cur_breed}"
}
} }
} }
} }
} }
div { flex: "50%", div { flex: "50%", breed_pic { breed: breed.to_string() } }
breed_pic { breed: breed.to_string() }
}
} }
} }
}), }),
+2 -2
View File
@@ -20,9 +20,9 @@ fn app(cx: Scope) -> Element {
// Use optionals // Use optionals
Some(rsx! { "Some" }), Some(rsx! { "Some" }),
// use a for loop // use a for loop where the body itself is RSX
for name in 0..10 { for name in 0..10 {
rsx! { "{name}" } div {"{name}"}
} }
// Or even use an unterminated conditional // Or even use an unterminated conditional
+10 -11
View File
@@ -44,18 +44,17 @@ impl DesktopController {
.build() .build()
.unwrap(); .unwrap();
let mut dom = VirtualDom::new_with_props(root, props)
.with_root_context(DesktopContext::new(desktop_context_proxy));
{
let edits = dom.rebuild();
let mut queue = edit_queue.lock().unwrap();
queue.push(serde_json::to_string(&edits.template_mutations).unwrap());
queue.push(serde_json::to_string(&edits.edits).unwrap());
proxy.send_event(UserWindowEvent::Update).unwrap();
}
runtime.block_on(async move { runtime.block_on(async move {
let mut dom = VirtualDom::new_with_props(root, props)
.with_root_context(DesktopContext::new(desktop_context_proxy));
{
let edits = dom.rebuild();
let mut queue = edit_queue.lock().unwrap();
queue.push(serde_json::to_string(&edits.template_mutations).unwrap());
queue.push(serde_json::to_string(&edits.edits).unwrap());
proxy.send_event(UserWindowEvent::Update).unwrap();
}
loop { loop {
tokio::select! { tokio::select! {
_ = dom.wait_for_work() => {} _ = dom.wait_for_work() => {}
+14 -7
View File
@@ -3,9 +3,10 @@ use super::*;
use proc_macro2::{Span, TokenStream as TokenStream2}; use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{quote, ToTokens, TokenStreamExt}; use quote::{quote, ToTokens, TokenStreamExt};
use syn::{ use syn::{
braced,
parse::{Parse, ParseStream}, parse::{Parse, ParseStream},
spanned::Spanned, spanned::Spanned,
token, Block, Expr, ExprIf, LitStr, Pat, Result, token, Expr, ExprIf, LitStr, Pat, Result,
}; };
/* /*
@@ -98,14 +99,20 @@ impl Parse for BodyNode {
let pat = stream.parse::<Pat>()?; let pat = stream.parse::<Pat>()?;
let _i = stream.parse::<Token![in]>()?; let _i = stream.parse::<Token![in]>()?;
let expr = stream.parse::<Box<Expr>>()?; let expr = stream.parse::<Box<Expr>>()?;
let body = stream.parse::<Block>()?;
let body;
braced!(body in stream);
let mut children = vec![];
while !body.is_empty() {
children.push(body.parse()?);
}
return Ok(BodyNode::ForLoop(ForLoop { return Ok(BodyNode::ForLoop(ForLoop {
for_token: _f, for_token: _f,
pat, pat,
in_token: _i, in_token: _i,
expr, expr,
body, body: children,
})); }));
} }
@@ -134,11 +141,11 @@ impl ToTokens for BodyNode {
pat, expr, body, .. pat, expr, body, ..
} = exp; } = exp;
let renderer = TemplateRenderer { roots: &body };
tokens.append_all(quote! { tokens.append_all(quote! {
__cx.fragment_from_iter( __cx.fragment_from_iter(
(#expr).into_iter().map(|#pat| { (#expr).into_iter().map(|#pat| { #renderer })
#body
})
) )
}) })
} }
@@ -213,7 +220,7 @@ pub struct ForLoop {
pub pat: Pat, pub pat: Pat,
pub in_token: Token![in], pub in_token: Token![in],
pub expr: Box<Expr>, pub expr: Box<Expr>,
pub body: Block, pub body: Vec<BodyNode>,
} }
fn is_if_chain_terminated(chain: &ExprIf) -> bool { fn is_if_chain_terminated(chain: &ExprIf) -> bool {