Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the semantics of exprs/for loops allocations strategy #1546

Merged
merged 7 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion examples/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ fn main() {
}

fn app(cx: Scope) -> Element {
let running = dioxus_signals::use_signal(cx, || true);
let mut count = dioxus_signals::use_signal(cx, || 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 {
loop {
count += 1;
if running.value() {
count += 1;
}
tokio::time::sleep(Duration::from_millis(400)).await;
}
});
Expand All @@ -19,9 +25,25 @@ fn app(cx: Scope) -> Element {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
button { onclick: move |_| running.toggle(), "Toggle counter" }
button { onclick: move |_| saved_values.push(count.value().to_string()), "Save this value" }
button { onclick: move |_| saved_values.write().clear(), "Clear saved values" }

// We can do boolean operations on the current signal value
if count.value() > 5 {
rsx!{ h2 { "High five!" } }
}

// We can cleanly map signals with iterators
for value in saved_values.read().iter() {
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 { "First and last: {first}, {last}" } }
} else {
rsx! { "No saved values" }
}
})
}
6 changes: 3 additions & 3 deletions packages/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ pub mod prelude {
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,
remove_future, schedule_update_any, spawn, spawn_forever, suspend, throw, AnyValue,
Component, Element, Event, EventHandler, Fragment, IntoAttributeValue, LazyNodes,
Properties, Runtime, RuntimeGuard, Scope, ScopeId, ScopeState, Scoped, TaskId, Template,
TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom,
Component, Element, Event, EventHandler, Fragment, IntoAttributeValue, IntoDynNode,
LazyNodes, Properties, Runtime, RuntimeGuard, Scope, ScopeId, ScopeState, Scoped, TaskId,
Template, TemplateAttribute, TemplateNode, Throw, VNode, VirtualDom,
};
}

Expand Down
25 changes: 19 additions & 6 deletions packages/rsx/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,10 @@ impl ToTokens for BodyNode {
__cx.text_node(#txt)
}),
BodyNode::RawExpr(exp) => tokens.append_all(quote! {
__cx.make_node(#exp)
{
let ___nodes = (#exp).into_vnode(__cx);
___nodes
}
}),
BodyNode::ForLoop(exp) => {
let ForLoop {
Expand All @@ -137,16 +140,23 @@ impl ToTokens for BodyNode {
location: None,
};

// Signals expose an issue with temporary lifetimes
// We need to directly render out the nodes first to collapse their lifetime to <'a>
// And then we can return them into the dyn loop
tokens.append_all(quote! {
__cx.make_node(
(#expr).into_iter().map(|#pat| { #renderer })
)
{
let ___nodes =(#expr).into_iter().map(|#pat| { #renderer }).into_vnode(__cx);
___nodes
}
})
}
BodyNode::IfChain(chain) => {
if is_if_chain_terminated(chain) {
tokens.append_all(quote! {
__cx.make_node(#chain)
{
let ___nodes = (#chain).into_vnode(__cx);
___nodes
}
});
} else {
let ExprIf {
Expand Down Expand Up @@ -200,7 +210,10 @@ impl ToTokens for BodyNode {
});

tokens.append_all(quote! {
__cx.make_node(#body)
{
let ___nodes = (#body).into_vnode(__cx);
___nodes
}
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/signals/src/signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,13 @@ impl<T: Clone + 'static> Signal<T> {
}
}

impl Signal<bool> {
/// Invert the boolean value of the signal. This will trigger an update on all subscribers.
pub fn toggle(&self) {
self.set(!self.value());
}
}

impl<T: 'static> PartialEq for Signal<T> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
Expand Down