handle iterators and nested rsx calls
This commit is contained in:
1 parent
f703347936
commit
3c7db0631b
4 files changed
+40
-18
No files matched your search
@@ -34,6 +34,11 @@ fn app(cx: Scope) -> Element {
|
|||||||
Comp{
|
Comp{
|
||||||
color: "green"
|
color: "green"
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
(0..10).map(|i| {
|
||||||
|
cx.render(rsx!{p{"{i}"}})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,24 +195,26 @@ pub fn rsx(s: TokenStream) -> TokenStream {
|
|||||||
let line_num = get_line_num();
|
let line_num = get_line_num();
|
||||||
let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap();
|
let rsx_text_index: RsxTextIndex = cx.consume_context().unwrap();
|
||||||
// only the insert the rsx text once
|
// only the insert the rsx text once
|
||||||
// todo: rsx could be conditionally rendered which would brake this
|
if !rsx_text_index.read().contains_key(&line_num){
|
||||||
use_state(&cx, || {
|
|
||||||
rsx_text_index.insert(
|
rsx_text_index.insert(
|
||||||
line_num.clone(),
|
line_num.clone(),
|
||||||
#rsx_text.to_string(),
|
#rsx_text.to_string(),
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
LazyNodes::new(move |__cx|{
|
LazyNodes::new(move |__cx|{
|
||||||
let read = rsx_text_index.read();
|
if let Some(text) = {
|
||||||
if let Some(text) = read.get(&line_num){
|
let read = rsx_text_index.read();
|
||||||
|
// clone prevents deadlock on nested rsx calls
|
||||||
|
read.get(&line_num).cloned()
|
||||||
|
} {
|
||||||
interpert_rsx(
|
interpert_rsx(
|
||||||
__cx,
|
__cx,
|
||||||
&text,
|
&text,
|
||||||
#captured
|
#captured
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
panic!("rsx: line number {:?} not found", line_num);
|
panic!("rsx: line number {:?} not found in rsx index", line_num);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ pub struct CapturedContextBuilder {
|
|||||||
pub attributes: HashMap<String, IfmtInput>,
|
pub attributes: HashMap<String, IfmtInput>,
|
||||||
pub text: Vec<IfmtInput>,
|
pub text: Vec<IfmtInput>,
|
||||||
pub components: Vec<Component>,
|
pub components: Vec<Component>,
|
||||||
pub iterators: Vec<Expr>,
|
pub iterators: Vec<BodyNode>,
|
||||||
pub captured_expressions: Vec<Expr>,
|
pub captured_expressions: Vec<Expr>,
|
||||||
pub listeners: Vec<ElementAttrNamed>,
|
pub listeners: Vec<ElementAttrNamed>,
|
||||||
}
|
}
|
||||||
@@ -70,7 +70,7 @@ impl CapturedContextBuilder {
|
|||||||
let formated: IfmtInput = syn::parse2(tokens).unwrap();
|
let formated: IfmtInput = syn::parse2(tokens).unwrap();
|
||||||
captured.text.push(formated);
|
captured.text.push(formated);
|
||||||
}
|
}
|
||||||
BodyNode::RawExpr(expr) => captured.iterators.push(expr),
|
BodyNode::RawExpr(_) => captured.iterators.push(node),
|
||||||
BodyNode::Meta(_) => (),
|
BodyNode::Meta(_) => (),
|
||||||
}
|
}
|
||||||
captured
|
captured
|
||||||
@@ -93,9 +93,10 @@ impl ToTokens for CapturedContextBuilder {
|
|||||||
let compontents_str = components
|
let compontents_str = components
|
||||||
.iter()
|
.iter()
|
||||||
.map(|comp| comp.to_token_stream().to_string());
|
.map(|comp| comp.to_token_stream().to_string());
|
||||||
let iterators_str = iterators
|
let iterators_str = iterators.iter().map(|node| match node {
|
||||||
.iter()
|
BodyNode::RawExpr(expr) => expr.to_token_stream().to_string(),
|
||||||
.map(|expr| expr.to_token_stream().to_string());
|
_ => unreachable!(),
|
||||||
|
});
|
||||||
let captured: Vec<_> = attributes
|
let captured: Vec<_> = attributes
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, fmt)| fmt.named_args.iter())
|
.map(|(_, fmt)| fmt.named_args.iter())
|
||||||
|
|||||||
@@ -90,8 +90,8 @@ pub fn build<'a>(
|
|||||||
factory: &NodeFactory<'a>,
|
factory: &NodeFactory<'a>,
|
||||||
) -> VNode<'a> {
|
) -> VNode<'a> {
|
||||||
let children_built = factory.bump().alloc(Vec::new());
|
let children_built = factory.bump().alloc(Vec::new());
|
||||||
for (i, child) in rsx.roots.into_iter().enumerate() {
|
for child in rsx.roots {
|
||||||
children_built.push(build_node(child, &mut ctx, factory, i.to_string().as_str()));
|
children_built.push(build_node(child, &mut ctx, factory));
|
||||||
}
|
}
|
||||||
factory.fragment_from_iter(children_built.iter())
|
factory.fragment_from_iter(children_built.iter())
|
||||||
}
|
}
|
||||||
@@ -100,7 +100,6 @@ fn build_node<'a>(
|
|||||||
node: BodyNode,
|
node: BodyNode,
|
||||||
ctx: &mut CapturedContext<'a>,
|
ctx: &mut CapturedContext<'a>,
|
||||||
factory: &NodeFactory<'a>,
|
factory: &NodeFactory<'a>,
|
||||||
key: &str,
|
|
||||||
) -> Option<VNode<'a>> {
|
) -> Option<VNode<'a>> {
|
||||||
let bump = factory.bump();
|
let bump = factory.bump();
|
||||||
match node {
|
match node {
|
||||||
@@ -173,8 +172,8 @@ fn build_node<'a>(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
let children = bump.alloc(Vec::new());
|
let children = bump.alloc(Vec::new());
|
||||||
for (i, child) in el.children.into_iter().enumerate() {
|
for child in el.children {
|
||||||
let node = build_node(child, ctx, factory, i.to_string().as_str());
|
let node = build_node(child, ctx, factory);
|
||||||
if let Some(node) = node {
|
if let Some(node) = node {
|
||||||
children.push(node);
|
children.push(node);
|
||||||
}
|
}
|
||||||
@@ -229,7 +228,22 @@ fn build_node<'a>(
|
|||||||
panic!("could not resolve component {:?}", comp.name);
|
panic!("could not resolve component {:?}", comp.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BodyNode::RawExpr(_) => todo!(),
|
BodyNode::RawExpr(iterator) => {
|
||||||
|
if let Some(idx) = ctx
|
||||||
|
.iterators
|
||||||
|
.iter()
|
||||||
|
.position(|(code, _)| parse_str::<Expr>(*code).unwrap() == iterator)
|
||||||
|
{
|
||||||
|
let (_, vnode) = ctx.iterators.remove(idx);
|
||||||
|
Some(vnode)
|
||||||
|
} else {
|
||||||
|
panic!(
|
||||||
|
"could not resolve iterator {} \n fron {:?}",
|
||||||
|
iterator.to_token_stream(),
|
||||||
|
ctx.iterators.iter().map(|(s, _)| s).collect::<Vec<_>>()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
BodyNode::Meta(_) => todo!(),
|
BodyNode::Meta(_) => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user