From 70a6f95c8c7f3d617b233e89567812c0372c7976 Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Sun, 24 Apr 2022 20:47:22 -0400 Subject: [PATCH] fix: allow fermi atomref to be cloned --- examples/fermi.rs | 25 +++++++++++++++++++++++++ packages/fermi/src/hooks/atom_ref.rs | 11 +++++++++++ 2 files changed, 36 insertions(+) diff --git a/examples/fermi.rs b/examples/fermi.rs index f4391de5e..9f87961b3 100644 --- a/examples/fermi.rs +++ b/examples/fermi.rs @@ -14,6 +14,7 @@ fn app(cx: Scope) -> Element { cx.render(rsx! { div { "hello {name}!" } Child {} + ChildWithRef{} }) } @@ -27,3 +28,27 @@ fn Child(cx: Scope) -> Element { } }) } + +static NAMES: AtomRef> = |_| vec!["world".to_string()]; + +fn ChildWithRef(cx: Scope) -> Element { + let names = use_atom_ref(&cx, NAMES); + + cx.render(rsx! { + div { + ul { + names.read().iter().map(|f| rsx!{ + li { "hello: {f}" } + }) + } + button { + onclick: move |_| { + let names = names.clone(); + cx.spawn(async move { + names.write().push("asd".to_string()); + }) + } + } + } + }) +} diff --git a/packages/fermi/src/hooks/atom_ref.rs b/packages/fermi/src/hooks/atom_ref.rs index da9ad9367..f9c7f2869 100644 --- a/packages/fermi/src/hooks/atom_ref.rs +++ b/packages/fermi/src/hooks/atom_ref.rs @@ -34,6 +34,17 @@ pub struct UseAtomRef { scope_id: ScopeId, } +impl Clone for UseAtomRef { + fn clone(&self) -> Self { + Self { + ptr: self.ptr, + value: self.value.clone(), + root: self.root.clone(), + scope_id: self.scope_id, + } + } +} + impl UseAtomRef { pub fn read(&self) -> Ref { self.value.borrow()