From 568631e3318607aff2b8e860389403f19770b7a9 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Mon, 4 Mar 2024 16:02:19 -0600 Subject: [PATCH] test signal drops --- packages/signals/README.md | 19 +++++++++---------- packages/signals/src/signal.rs | 10 ++++------ packages/signals/tests/create.rs | 21 ++++++++++++++++----- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/packages/signals/README.md b/packages/signals/README.md index 56dd8d47d7..7a1f39f0d1 100644 --- a/packages/signals/README.md +++ b/packages/signals/README.md @@ -36,18 +36,18 @@ use dioxus_signals::*; #[component] fn App() -> Element { // Because signal is never read in this component, this component will not rerun when the signal changes - let signal = use_signal(|| 0); + let mut signal = use_signal(|| 0); rsx! { button { onclick: move |_| { - *signal.write() += 1; + signal += 1; }, "Increase" } for id in 0..10 { Child { - signal: signal, + signal, } } } @@ -58,11 +58,10 @@ struct ChildProps { signal: Signal, } -#[component] -fn Child(cx: Scope) -> Element { +fn Child(props: ChildProps) -> Element { // This component does read from the signal, so when the signal changes it will rerun rsx! { - "{cx.props.signal}" + "{props.signal}" } } ``` @@ -85,7 +84,7 @@ fn App() -> Element { #[component] fn Child() -> Element { - let signal: Signal = *use_context(cx).unwrap(); + let signal: Signal = use_context(); // This component does read from the signal, so when the signal changes it will rerun rsx! { "{signal}" @@ -105,12 +104,12 @@ use dioxus_signals::*; #[component] fn App() -> Element { - let signal = use_signal(|| 0); - let doubled = use_memo(|| signal * 2); + let mut signal = use_signal(|| 0); + let doubled = use_memo(move || signal * 2); rsx! { button { - onclick: move |_| *signal.write() += 1, + onclick: move |_| signal += 1, "Increase" } Child { diff --git a/packages/signals/src/signal.rs b/packages/signals/src/signal.rs index faa39009bc..b0a70d210c 100644 --- a/packages/signals/src/signal.rs +++ b/packages/signals/src/signal.rs @@ -30,17 +30,15 @@ use std::{ /// } /// /// #[component] -/// fn Child(state: Signal) -> Element { -/// let state = *state; -/// -/// use_future( |()| async move { +/// fn Child(mut state: Signal) -> Element { +/// use_future(move || async move { /// // Because the signal is a Copy type, we can use it in an async block without cloning it. -/// *state.write() += 1; +/// state += 1; /// }); /// /// rsx! { /// button { -/// onclick: move |_| *state.write() += 1, +/// onclick: move |_| state += 1, /// "{state}" /// } /// } diff --git a/packages/signals/tests/create.rs b/packages/signals/tests/create.rs index 7b17310b66..b32da529c2 100644 --- a/packages/signals/tests/create.rs +++ b/packages/signals/tests/create.rs @@ -56,6 +56,11 @@ fn deref_signal() { #[test] fn drop_signals() { + use std::sync::atomic::AtomicUsize; + use std::sync::atomic::Ordering; + + static SIGNAL_DROP_COUNT: AtomicUsize = AtomicUsize::new(0); + let mut dom = VirtualDom::new(|| { let generation = generation(); @@ -68,10 +73,18 @@ fn drop_signals() { }); fn Child() -> Element { - let signal = create_without_cx(); + struct TracksDrops; + + impl Drop for TracksDrops { + fn drop(&mut self) { + SIGNAL_DROP_COUNT.fetch_add(1, Ordering::Relaxed); + } + } + + use_signal(|| TracksDrops); rsx! { - "{signal}" + "" } } @@ -79,7 +92,5 @@ fn drop_signals() { dom.mark_dirty(ScopeId::ROOT); dom.render_immediate(&mut NoOpMutations); - fn create_without_cx() -> Signal { - Signal::new("hello world".to_string()) - } + assert_eq!(SIGNAL_DROP_COUNT.load(Ordering::Relaxed), 10); }