feat: integrate fermi
This commit is contained in:
1 parent
3873cd1a60
commit
d461ffc011
23 files changed
+630
-15
No files matched your search
@@ -0,0 +1,62 @@
|
||||
use crate::{use_atom_root, AtomId, AtomRef, AtomRoot, Readable};
|
||||
use dioxus_core::{ScopeId, ScopeState};
|
||||
use std::{
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
rc::Rc,
|
||||
};
|
||||
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
///
|
||||
pub fn use_atom_ref<T: 'static>(cx: &ScopeState, atom: AtomRef<T>) -> &UseAtomRef<T> {
|
||||
let root = use_atom_root(cx);
|
||||
|
||||
cx.use_hook(|_| {
|
||||
root.initialize(atom);
|
||||
UseAtomRef {
|
||||
ptr: atom.unique_id(),
|
||||
root: root.clone(),
|
||||
scope_id: cx.scope_id(),
|
||||
value: root.register(atom, cx.scope_id()),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub struct UseAtomRef<T> {
|
||||
ptr: AtomId,
|
||||
value: Rc<RefCell<T>>,
|
||||
root: Rc<AtomRoot>,
|
||||
scope_id: ScopeId,
|
||||
}
|
||||
|
||||
impl<T: 'static> UseAtomRef<T> {
|
||||
pub fn read(&self) -> Ref<T> {
|
||||
self.value.borrow()
|
||||
}
|
||||
|
||||
pub fn write(&self) -> RefMut<T> {
|
||||
self.root.force_update(self.ptr);
|
||||
self.value.borrow_mut()
|
||||
}
|
||||
|
||||
pub fn write_silent(&self) -> RefMut<T> {
|
||||
self.root.force_update(self.ptr);
|
||||
self.value.borrow_mut()
|
||||
}
|
||||
|
||||
pub fn set(&self, new: T) {
|
||||
self.root.force_update(self.ptr);
|
||||
self.root.set(self.ptr, new);
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for UseAtomRef<T> {
|
||||
fn drop(&mut self) {
|
||||
self.root.unsubscribe(self.ptr, self.scope_id)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
use crate::AtomRoot;
|
||||
use dioxus_core::ScopeState;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Returns the atom root, initiaizing it at the root of the app if it does not exist.
|
||||
pub fn use_atom_root(cx: &ScopeState) -> &Rc<AtomRoot> {
|
||||
cx.use_hook(|_| match cx.consume_context::<AtomRoot>() {
|
||||
Some(root) => root,
|
||||
None => cx.provide_root_context(AtomRoot::new(cx.schedule_update_any())),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
use crate::AtomRoot;
|
||||
use dioxus_core::ScopeState;
|
||||
use std::rc::Rc;
|
||||
|
||||
// Initializes the atom root and retuns it;
|
||||
pub fn use_init_atom_root(cx: &ScopeState) -> &Rc<AtomRoot> {
|
||||
cx.use_hook(|_| match cx.consume_context::<AtomRoot>() {
|
||||
Some(ctx) => ctx,
|
||||
None => cx.provide_context(AtomRoot::new(cx.schedule_update_any())),
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use crate::{use_atom_root, AtomId, AtomRoot, Readable};
|
||||
use dioxus_core::{ScopeId, ScopeState};
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn use_read<'a, V: 'static>(cx: &'a ScopeState, f: impl Readable<V>) -> &'a V {
|
||||
use_read_rc(cx, f).as_ref()
|
||||
}
|
||||
|
||||
pub fn use_read_rc<'a, V: 'static>(cx: &'a ScopeState, f: impl Readable<V>) -> &'a Rc<V> {
|
||||
let root = use_atom_root(cx);
|
||||
|
||||
struct UseReadInner<V> {
|
||||
root: Rc<AtomRoot>,
|
||||
id: AtomId,
|
||||
scope_id: ScopeId,
|
||||
value: Option<Rc<V>>,
|
||||
}
|
||||
|
||||
impl<V> Drop for UseReadInner<V> {
|
||||
fn drop(&mut self) {
|
||||
self.root.unsubscribe(self.id, self.scope_id)
|
||||
}
|
||||
}
|
||||
|
||||
let inner = cx.use_hook(|_| UseReadInner {
|
||||
value: None,
|
||||
root: root.clone(),
|
||||
scope_id: cx.scope_id(),
|
||||
id: f.unique_id(),
|
||||
});
|
||||
|
||||
let value = inner.root.register(f, cx.scope_id());
|
||||
|
||||
inner.value = Some(value);
|
||||
inner.value.as_ref().unwrap()
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
use crate::{use_atom_root, Writable};
|
||||
use dioxus_core::ScopeState;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub fn use_set<'a, T: 'static>(cx: &'a ScopeState, f: impl Writable<T>) -> &'a Rc<dyn Fn(T)> {
|
||||
let root = use_atom_root(cx);
|
||||
cx.use_hook(|_| {
|
||||
let id = f.unique_id();
|
||||
let root = root.clone();
|
||||
root.initialize(f);
|
||||
Rc::new(move |new| root.set(id, new)) as Rc<dyn Fn(T)>
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user