Merge branch 'DioxusLabs:master' into master
This commit is contained in:
2 files changed
+64
-13
No files matched your search
@@ -14,6 +14,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
cx.render(rsx! {
|
cx.render(rsx! {
|
||||||
div { "hello {name}!" }
|
div { "hello {name}!" }
|
||||||
Child {}
|
Child {}
|
||||||
|
ChildWithRef{}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,3 +28,28 @@ fn Child(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static NAMES: AtomRef<Vec<String>> = |_| 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());
|
||||||
|
})
|
||||||
|
},
|
||||||
|
"Add name"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -16,15 +16,35 @@ use std::{
|
|||||||
pub fn use_atom_ref<T: 'static>(cx: &ScopeState, atom: AtomRef<T>) -> &UseAtomRef<T> {
|
pub fn use_atom_ref<T: 'static>(cx: &ScopeState, atom: AtomRef<T>) -> &UseAtomRef<T> {
|
||||||
let root = use_atom_root(cx);
|
let root = use_atom_root(cx);
|
||||||
|
|
||||||
cx.use_hook(|_| {
|
&cx.use_hook(|_| {
|
||||||
root.initialize(atom);
|
root.initialize(atom);
|
||||||
UseAtomRef {
|
(
|
||||||
ptr: atom.unique_id(),
|
UseAtomRef {
|
||||||
root: root.clone(),
|
ptr: atom.unique_id(),
|
||||||
scope_id: cx.scope_id(),
|
root: root.clone(),
|
||||||
value: root.register(atom, cx.scope_id()),
|
scope_id: cx.scope_id(),
|
||||||
}
|
value: root.register(atom, cx.scope_id()),
|
||||||
|
},
|
||||||
|
AtomRefSubscription {
|
||||||
|
ptr: atom.unique_id(),
|
||||||
|
root: root.clone(),
|
||||||
|
scope_id: cx.scope_id(),
|
||||||
|
},
|
||||||
|
)
|
||||||
})
|
})
|
||||||
|
.0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AtomRefSubscription {
|
||||||
|
ptr: AtomId,
|
||||||
|
root: Rc<AtomRoot>,
|
||||||
|
scope_id: ScopeId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for AtomRefSubscription {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.root.unsubscribe(self.ptr, self.scope_id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UseAtomRef<T> {
|
pub struct UseAtomRef<T> {
|
||||||
@@ -34,6 +54,17 @@ pub struct UseAtomRef<T> {
|
|||||||
scope_id: ScopeId,
|
scope_id: ScopeId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> Clone for UseAtomRef<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self {
|
||||||
|
ptr: self.ptr,
|
||||||
|
value: self.value.clone(),
|
||||||
|
root: self.root.clone(),
|
||||||
|
scope_id: self.scope_id,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: 'static> UseAtomRef<T> {
|
impl<T: 'static> UseAtomRef<T> {
|
||||||
pub fn read(&self) -> Ref<T> {
|
pub fn read(&self) -> Ref<T> {
|
||||||
self.value.borrow()
|
self.value.borrow()
|
||||||
@@ -54,9 +85,3 @@ impl<T: 'static> UseAtomRef<T> {
|
|||||||
self.root.set(self.ptr, new);
|
self.root.set(self.ptr, new);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Drop for UseAtomRef<T> {
|
|
||||||
fn drop(&mut self) {
|
|
||||||
self.root.unsubscribe(self.ptr, self.scope_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user