Add into_vnode temporary logic for more things

This commit adjusts how rsx! works, making it more forgiving with signals.

Notably, we add the temporaries to if chains too.
This commit is contained in:
Jonathan Kelley committed 2023-10-17 15:42:21 -07:00
1 parent 55ad41b392
commit e819702fc5
3 files changed
+23 -10

No files matched your search

+11 -2
View File
@@ -8,8 +8,10 @@ fn main() {
fn app(cx: Scope) -> Element { fn app(cx: Scope) -> Element {
let running = dioxus_signals::use_signal(cx, || true); let running = dioxus_signals::use_signal(cx, || true);
let mut count = dioxus_signals::use_signal(cx, || 0); let mut count = dioxus_signals::use_signal(cx, || 0);
let saved_values = dioxus_signals::use_signal(cx, || vec![0]); let saved_values = dioxus_signals::use_signal(cx, || vec![0.to_string()]);
// Signals can be used in async functions without an explicit clone since they're 'static and Copy
// Signals are backed by a runtime that is designed to deeply integrate with Dioxus apps
use_future!(cx, || async move { use_future!(cx, || async move {
loop { loop {
if running.value() { if running.value() {
@@ -24,7 +26,7 @@ fn app(cx: Scope) -> Element {
button { onclick: move |_| count += 1, "Up high!" } button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" } button { onclick: move |_| count -= 1, "Down low!" }
button { onclick: move |_| running.toggle(), "Toggle counter" } button { onclick: move |_| running.toggle(), "Toggle counter" }
button { onclick: move |_| saved_values.push(count.value()), "Save this value" } button { onclick: move |_| saved_values.push(count.value().to_string()), "Save this value" }
// We can do boolean operations on the current signal value // We can do boolean operations on the current signal value
if count.value() > 5 { if count.value() > 5 {
@@ -35,5 +37,12 @@ fn app(cx: Scope) -> Element {
for value in saved_values.read().iter() { for value in saved_values.read().iter() {
h3 { "Saved value: {value}" } h3 { "Saved value: {value}" }
} }
// We can also use the signal value as a slice
if let &[ref first, .., ref last] = saved_values.read().as_slice() {
rsx! { li { "single element: {first}, {last}" } }
} else {
rsx! { "No saved values" }
}
}) })
} }
+3 -3
View File
@@ -91,9 +91,9 @@ pub mod prelude {
consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, has_context, consume_context, consume_context_from_scope, current_scope_id, fc_to_builder, has_context,
provide_context, provide_context_to_scope, provide_root_context, push_future, provide_context, provide_context_to_scope, provide_root_context, push_future,
remove_future, schedule_update_any, spawn, spawn_forever, suspend, throw, AnyValue, remove_future, schedule_update_any, spawn, spawn_forever, suspend, throw, AnyValue,
Component, Element, Event, EventHandler, Fragment, IntoAttributeValue, LazyNodes, Component, Element, Event, EventHandler, Fragment, IntoAttributeValue, IntoDynNode,
Properties, Runtime, RuntimeGuard, Scope, ScopeId, ScopeState, Scoped, TaskId, Template, LazyNodes, Properties, Runtime, RuntimeGuard, Scope, ScopeId, ScopeState, Scoped, TaskId,
TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom, Template, TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom,
}; };
} }
+9 -5
View File
@@ -126,8 +126,7 @@ impl ToTokens for BodyNode {
}), }),
BodyNode::RawExpr(exp) => tokens.append_all(quote! { BodyNode::RawExpr(exp) => tokens.append_all(quote! {
{ {
use ::dioxus::core::IntoDynNode; let ___nodes = (#exp).into_vnode(__cx);
let ___nodes =(#exp).into_vnode(__cx);
___nodes ___nodes
} }
}), }),
@@ -146,7 +145,6 @@ impl ToTokens for BodyNode {
// And then we can return them into the dyn loop // And then we can return them into the dyn loop
tokens.append_all(quote! { tokens.append_all(quote! {
{ {
use ::dioxus::core::IntoDynNode;
let ___nodes =(#expr).into_iter().map(|#pat| { #renderer }).into_vnode(__cx); let ___nodes =(#expr).into_iter().map(|#pat| { #renderer }).into_vnode(__cx);
___nodes ___nodes
} }
@@ -155,7 +153,10 @@ impl ToTokens for BodyNode {
BodyNode::IfChain(chain) => { BodyNode::IfChain(chain) => {
if is_if_chain_terminated(chain) { if is_if_chain_terminated(chain) {
tokens.append_all(quote! { tokens.append_all(quote! {
__cx.make_node(#chain) {
let ___nodes = (#chain).into_vnode(__cx);
___nodes
}
}); });
} else { } else {
let ExprIf { let ExprIf {
@@ -209,7 +210,10 @@ impl ToTokens for BodyNode {
}); });
tokens.append_all(quote! { tokens.append_all(quote! {
__cx.make_node(#body) {
let ___nodes = (#body).into_vnode(__cx);
___nodes
}
}); });
} }
} }