feat: implement function accessor

This commit is contained in:
Jonathan Kelley 2023-01-01 22:57:16 -05:00
parent fd92079eb3
commit 14a1e4145e
3 changed files with 48 additions and 38 deletions

View file

@ -14,7 +14,7 @@ fn app(cx: Scope) -> Element {
use_future!(cx, || async move {
loop {
count += 1;
tokio::time::sleep(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(400)).await;
}
});
@ -22,5 +22,9 @@ fn app(cx: Scope) -> Element {
h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" }
if count() > 5 {
rsx!{ h2 { "High five!" } }
}
})
}

View file

@ -26,6 +26,7 @@ pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<
struct SignalHook<T> {
signal: Signal<T>,
}
impl<T> Drop for SignalHook<T> {
fn drop(&mut self) {
self.signal.rt.remove(self.signal.id);
@ -62,6 +63,11 @@ impl<T: 'static> Signal<T> {
self.rt.set(self.id, value);
}
pub fn with<O>(&self, f: impl FnOnce(&T) -> O) -> O {
let write = self.read();
f(&*write)
}
pub fn update<O>(&self, _f: impl FnOnce(&mut T) -> O) -> O {
let mut write = self.write();
_f(&mut *write)
@ -74,13 +80,13 @@ impl<T: Clone + 'static> Signal<T> {
}
}
// impl<T> std::ops::Deref for Signal<T> {
// type Target = dyn Fn() -> T;
impl<T: Clone + 'static> std::ops::Deref for Signal<T> {
type Target = dyn Fn() -> T;
// fn deref(&self) -> &Self::Target {
// todo!()
// }
// }
fn deref(&self) -> &Self::Target {
self.rt.getter(self.id)
}
}
impl<T> std::clone::Clone for Signal<T> {
fn clone(&self) -> Self {
@ -123,27 +129,3 @@ impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for Signal<T> {
self.set(self.get() / rhs);
}
}
// impl<T: Add<Output = T> + Copy> std::ops::AddAssign<T> for Signal<T> {
// fn add_assign(&mut self, rhs: T) {
// self.set((*self.current()) + rhs);
// }
// }
// impl<T: Sub<Output = T> + Copy> std::ops::SubAssign<T> for Signal<T> {
// fn sub_assign(&mut self, rhs: T) {
// self.set((*self.current()) - rhs);
// }
// }
// impl<T: Mul<Output = T> + Copy> std::ops::MulAssign<T> for Signal<T> {
// fn mul_assign(&mut self, rhs: T) {
// self.set((*self.current()) * rhs);
// }
// }
// impl<T: Div<Output = T> + Copy> std::ops::DivAssign<T> for Signal<T> {
// fn div_assign(&mut self, rhs: T) {
// self.set((*self.current()) / rhs);
// }
// }

View file

@ -4,6 +4,7 @@ use dioxus_core::ScopeId;
use slab::Slab;
thread_local! {
// we cannot drop these since any future might be using them
static RUNTIMES: RefCell<Vec<&'static SignalRt>> = RefCell::new(Vec::new());
}
@ -31,15 +32,16 @@ pub fn reclam_rt(_rt: &'static SignalRt) {
}
pub struct SignalRt {
signals: RefCell<Slab<Inner>>,
update_any: Arc<dyn Fn(ScopeId)>,
pub(crate) signals: RefCell<Slab<Inner>>,
pub(crate) update_any: Arc<dyn Fn(ScopeId)>,
}
impl SignalRt {
pub fn init<T: 'static>(&self, val: T) -> usize {
pub fn init<T: 'static>(&'static self, val: T) -> usize {
self.signals.borrow_mut().insert(Inner {
value: Box::new(val),
subscribers: Vec::new(),
getter: None,
})
}
@ -73,7 +75,7 @@ impl SignalRt {
let signals = self.signals.borrow();
let inner = &signals[id];
let inner = inner.value.downcast_ref::<T>().unwrap();
f(&*inner)
f(inner)
}
pub(crate) fn read<T: 'static>(&self, id: usize) -> std::cell::Ref<T> {
@ -89,9 +91,31 @@ impl SignalRt {
signals[id].value.downcast_mut::<T>().unwrap()
})
}
pub(crate) fn getter<T: 'static + Clone>(&self, id: usize) -> &dyn Fn() -> T {
let mut signals = self.signals.borrow_mut();
let inner = &mut signals[id];
let r = inner.getter.as_mut();
if r.is_none() {
let rt = self;
let r = move || rt.get::<T>(id);
let getter: Box<dyn Fn() -> T> = Box::new(r);
let getter: Box<dyn Fn()> = unsafe { std::mem::transmute(getter) };
inner.getter = Some(getter);
}
let r = inner.getter.as_ref().unwrap();
unsafe { std::mem::transmute::<&dyn Fn(), &dyn Fn() -> T>(r) }
}
}
struct Inner {
value: Box<dyn Any>,
subscribers: Vec<ScopeId>,
pub(crate) struct Inner {
pub value: Box<dyn Any>,
pub subscribers: Vec<ScopeId>,
// todo: this has a soundness hole in it that you might not run into
pub getter: Option<Box<dyn Fn()>>,
}