chore: more signals methods

This commit is contained in:
Jonathan Kelley committed 2023-01-01 22:02:49 -05:00
1 parent 4f9d67fb45
commit c0315e55f4
3 files changed
+48 -23

No files matched your search

+1 -12
View File
@@ -14,28 +14,17 @@ fn app(cx: Scope) -> Element {
use_init_signal_rt(cx); use_init_signal_rt(cx);
let mut count = use_signal(cx, || 0); let mut count = use_signal(cx, || 0);
let mut running = use_signal(cx, || false);
use_coroutine(cx, |_: UnboundedReceiver<()>| async move { use_coroutine(cx, |_: UnboundedReceiver<()>| async move {
loop { loop {
if running.get() { count += 1;
count += 1;
}
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
} }
}); });
cx.render(rsx! { cx.render(rsx! {
h1 { "High-Five counter: {count}" } h1 { "High-Five counter: {count}" }
button { onclick: move |_| count += 1, "Up high!" } button { onclick: move |_| count += 1, "Up high!" }
button { onclick: move |_| count -= 1, "Down low!" } button { onclick: move |_| count -= 1, "Down low!" }
button { onclick: move |_| running.set(!running.get()), "Start counting" }
if count.get() > 3 {
rsx! ( h2 { "Nice!" } )
}
}) })
} }
+29 -11
View File
@@ -1,4 +1,5 @@
use std::{ use std::{
cell::{Ref, RefMut},
fmt::Display, fmt::Display,
marker::PhantomData, marker::PhantomData,
ops::{Add, Div, Mul, Sub}, ops::{Add, Div, Mul, Sub},
@@ -17,17 +18,29 @@ pub fn use_init_signal_rt(cx: &ScopeState) {
} }
pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> { pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> {
*cx.use_hook(|| { cx.use_hook(|| {
let rt: &'static SignalRt = cx.consume_context().unwrap(); let rt: &'static SignalRt = cx.consume_context().unwrap();
let id = rt.init(f()); let id = rt.init(f());
rt.subscribe(id, cx.scope_id()); rt.subscribe(id, cx.scope_id());
Signal { struct SignalHook<T> {
rt, signal: Signal<T>,
id, }
t: PhantomData, impl<T> Drop for SignalHook<T> {
fn drop(&mut self) {
self.signal.rt.remove(self.signal.id);
}
}
SignalHook {
signal: Signal {
id,
rt,
t: PhantomData,
},
} }
}) })
.signal
} }
pub struct Signal<T> { pub struct Signal<T> {
@@ -37,16 +50,21 @@ pub struct Signal<T> {
} }
impl<T: 'static> Signal<T> { impl<T: 'static> Signal<T> {
pub fn read(&self) -> Ref<T> {
self.rt.read(self.id)
}
pub fn write(&self) -> RefMut<T> {
self.rt.write(self.id)
}
pub fn set(&mut self, value: T) { pub fn set(&mut self, value: T) {
self.rt.set(self.id, value); self.rt.set(self.id, value);
} }
pub fn map<U>(&self, _f: impl FnOnce(T) -> U) -> Signal<U> { pub fn update<O>(&self, _f: impl FnOnce(&mut T) -> O) -> O {
todo!() let mut write = self.write();
} _f(&mut *write)
pub fn update<O>(&self, _f: impl FnOnce(&mut T) -> O) {
todo!()
} }
} }
+18
View File
@@ -59,12 +59,30 @@ impl SignalRt {
} }
} }
pub fn remove(&self, id: usize) {
self.signals.borrow_mut().remove(id);
}
pub fn with<T: 'static, O>(&self, id: usize, f: impl FnOnce(&T) -> O) -> O { pub fn with<T: 'static, O>(&self, id: usize, f: impl FnOnce(&T) -> O) -> O {
let signals = self.signals.borrow(); let signals = self.signals.borrow();
let inner = &signals[id]; let inner = &signals[id];
let inner = inner.value.downcast_ref::<T>().unwrap(); 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> {
let signals = self.signals.borrow();
std::cell::Ref::map(signals, |signals| {
signals[id].value.downcast_ref::<T>().unwrap()
})
}
pub(crate) fn write<T: 'static>(&self, id: usize) -> std::cell::RefMut<T> {
let mut signals = self.signals.borrow_mut();
std::cell::RefMut::map(signals, |signals| {
signals[id].value.downcast_mut::<T>().unwrap()
})
}
} }
struct Inner { struct Inner {