chore: tweak attributes to only set ID once

This commit is contained in:
Jonathan Kelley 2022-11-08 20:06:39 -08:00
parent fc9fe6e560
commit c096057dd3
6 changed files with 109 additions and 90 deletions

View file

@ -55,44 +55,43 @@ impl VirtualDom {
TemplateNode::DynamicText { .. } => 1, TemplateNode::DynamicText { .. } => 1,
}; };
let mut cur_route = None;
// we're on top of a node that has a dynamic attribute for a descendant // we're on top of a node that has a dynamic attribute for a descendant
// Set that attribute now before the stack gets in a weird state // Set that attribute now before the stack gets in a weird state
while let Some((idx, path)) = dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8) {
let attr = &template.dynamic_attrs[idx];
if cur_route.is_none() { while let Some((mut attr_id, path)) =
cur_route = Some((self.next_element(template), &path[1..])); dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8)
} {
let id = self.next_element(template);
mutations.push(AssignId {
path: &path[1..],
id,
});
// Attach all the elementIDs to the nodes with dynamic content // set any future attrs with the same path (ie same element)
let (id, path) = cur_route.unwrap(); loop {
let attr = &template.dynamic_attrs[attr_id];
mutations.push(AssignId { path, id });
attr.mounted_element.set(id); attr.mounted_element.set(id);
match attr.value { match attr.value {
AttributeValue::Text(value) => { AttributeValue::Text(value) => mutations.push(SetAttribute {
mutations.push(SetAttribute {
name: attr.name, name: attr.name,
value, value,
id, id,
}); }),
} AttributeValue::Listener(_) => {}
AttributeValue::Listener(_) => {
//
}
AttributeValue::Float(_) => todo!(), AttributeValue::Float(_) => todo!(),
AttributeValue::Int(_) => todo!(), AttributeValue::Int(_) => todo!(),
AttributeValue::Bool(_) => todo!(), AttributeValue::Bool(_) => todo!(),
AttributeValue::Any(_) => todo!(), AttributeValue::Any(_) => todo!(),
// Optional attributes
AttributeValue::None => todo!(), AttributeValue::None => todo!(),
} }
if let Some((next_attr_id, _)) = dynamic_attrs.next_if(|(_, p)| *p == path) {
attr_id = next_attr_id
} else {
break;
}
}
} }
// We're on top of a node that has a dynamic child for a descendant // We're on top of a node that has a dynamic child for a descendant

View file

@ -3,9 +3,8 @@ use crate::arena::ElementId;
#[derive(Debug)] #[derive(Debug)]
pub struct Renderer<'a> { pub struct Renderer<'a> {
pub subtree: usize, pub subtree: usize,
pub mutations: Vec<Mutation<'a>>,
pub template_mutations: Vec<Mutation<'a>>, pub template_mutations: Vec<Mutation<'a>>,
// mutations: Vec<Mutations<'a>>, pub mutations: Vec<Mutation<'a>>,
} }
impl<'a> Renderer<'a> { impl<'a> Renderer<'a> {

View file

@ -76,7 +76,7 @@ impl VirtualDom {
RenderReturn::Sync(None) => { RenderReturn::Sync(None) => {
// //
} }
RenderReturn::Async(_) => unreachable!(), RenderReturn::Async(_) => unreachable!("Root scope cannot be an async component"),
} }
mutations.push(Mutation::AppendChildren { m: created }); mutations.push(Mutation::AppendChildren { m: created });

View file

@ -1,5 +1,3 @@
use std::future::IntoFuture;
use dioxus::prelude::*; use dioxus::prelude::*;
fn basic_syntax_is_a_template(cx: Scope) -> Element { fn basic_syntax_is_a_template(cx: Scope) -> Element {
@ -25,63 +23,8 @@ fn basic_syntax_is_a_template(cx: Scope) -> Element {
}) })
} }
#[inline_props] #[test]
fn suspense_boundary<'a>(cx: Scope<'a>, children: Element<'a>) -> Element { fn dual_stream() {
cx.use_hook(|| cx.provide_context(SuspenseBoundary::new(cx.scope_id()))); let mut dom = VirtualDom::new(basic_syntax_is_a_template);
cx.render(rsx! { children })
}
fn basic_child(cx: Scope) -> Element {
cx.render(rsx! {
div { "basic child 1" }
})
}
async fn async_child(cx: Scope<'_>) -> Element {
let username = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"async child 1"
});
let age = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
println!("long future completed");
1234
});
let (_user, _age) = use_future!(cx, || async {
tokio::join!(
tokio::time::sleep(std::time::Duration::from_secs(1)),
tokio::time::sleep(std::time::Duration::from_secs(2))
);
("async child 1", 1234)
})
.await;
let (username, age) = tokio::join!(username.into_future(), age.into_future());
cx.render(rsx!(
div { "Hello! {username}, you are {age}, {_user} {_age}" }
))
}
#[tokio::test]
async fn basic_prints() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {
h1 { "var" }
suspense_boundary {
basic_child { }
async_child { }
}
}
})
});
dbg!(dom.rebuild());
dom.wait_for_work().await;
dbg!(dom.rebuild()); dbg!(dom.rebuild());
} }

View file

@ -0,0 +1,64 @@
use std::future::IntoFuture;
use dioxus::prelude::*;
#[inline_props]
fn suspense_boundary<'a>(cx: Scope<'a>, children: Element<'a>) -> Element {
cx.use_hook(|| cx.provide_context(SuspenseBoundary::new(cx.scope_id())));
cx.render(rsx! { children })
}
fn basic_child(cx: Scope) -> Element {
cx.render(rsx! {
div { "basic child 1" }
})
}
async fn async_child(cx: Scope<'_>) -> Element {
let username = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
"async child 1"
});
let age = use_future!(cx, || async {
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
println!("long future completed");
1234
});
let (_user, _age) = use_future!(cx, || async {
tokio::join!(
tokio::time::sleep(std::time::Duration::from_secs(1)),
tokio::time::sleep(std::time::Duration::from_secs(2))
);
("async child 1", 1234)
})
.await;
let (username, age) = tokio::join!(username.into_future(), age.into_future());
cx.render(rsx!(
div { "Hello! {username}, you are {age}, {_user} {_age}" }
))
}
#[tokio::test]
async fn basic_prints() {
let mut dom = VirtualDom::new(|cx| {
cx.render(rsx! {
div {
h1 { "var" }
suspense_boundary {
basic_child { }
async_child { }
}
}
})
});
dbg!(dom.rebuild());
dom.wait_for_work().await;
dbg!(dom.rebuild());
}

View file

@ -15,6 +15,7 @@ pub mod on {
}; };
use euclid::UnknownUnit; use euclid::UnknownUnit;
use keyboard_types::{Code, Key, Location, Modifiers}; use keyboard_types::{Code, Key, Location, Modifiers};
use std::cell::Cell;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::TryInto;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
@ -62,7 +63,20 @@ pub mod on {
// let handler = bump.alloc(std::cell::RefCell::new(Some(callback))); // let handler = bump.alloc(std::cell::RefCell::new(Some(callback)));
// factory.listener(shortname, handler) // factory.listener(shortname, handler)
todo!() // pub struct Attribute<'a> {
// pub name: &'a str,
// pub value: AttributeValue<'a>,
// pub namespace: Option<&'static str>,
// pub mounted_element: Cell<ElementId>,
// pub volatile: bool,
// }
Attribute {
name: stringify!($name),
value: AttributeValue::Text("asd"),
namespace: None,
mounted_element: Cell::new(ElementId(0)),
volatile: false,
}
} }
)* )*
)* )*