mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-11-26 22:20:19 +00:00
chore: more signals methods
This commit is contained in:
parent
4f9d67fb45
commit
c0315e55f4
3 changed files with 48 additions and 23 deletions
|
@ -14,28 +14,17 @@ fn app(cx: Scope) -> Element {
|
|||
use_init_signal_rt(cx);
|
||||
|
||||
let mut count = use_signal(cx, || 0);
|
||||
let mut running = use_signal(cx, || false);
|
||||
|
||||
use_coroutine(cx, |_: UnboundedReceiver<()>| async move {
|
||||
loop {
|
||||
if running.get() {
|
||||
count += 1;
|
||||
}
|
||||
count += 1;
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
});
|
||||
|
||||
cx.render(rsx! {
|
||||
h1 { "High-Five counter: {count}" }
|
||||
|
||||
button { onclick: move |_| count += 1, "Up high!" }
|
||||
button { onclick: move |_| count -= 1, "Down low!" }
|
||||
|
||||
button { onclick: move |_| running.set(!running.get()), "Start counting" }
|
||||
|
||||
|
||||
if count.get() > 3 {
|
||||
rsx! ( h2 { "Nice!" } )
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::{
|
||||
cell::{Ref, RefMut},
|
||||
fmt::Display,
|
||||
marker::PhantomData,
|
||||
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> {
|
||||
*cx.use_hook(|| {
|
||||
cx.use_hook(|| {
|
||||
let rt: &'static SignalRt = cx.consume_context().unwrap();
|
||||
let id = rt.init(f());
|
||||
rt.subscribe(id, cx.scope_id());
|
||||
|
||||
Signal {
|
||||
rt,
|
||||
id,
|
||||
t: PhantomData,
|
||||
struct SignalHook<T> {
|
||||
signal: Signal<T>,
|
||||
}
|
||||
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> {
|
||||
|
@ -37,16 +50,21 @@ pub struct 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) {
|
||||
self.rt.set(self.id, value);
|
||||
}
|
||||
|
||||
pub fn map<U>(&self, _f: impl FnOnce(T) -> U) -> Signal<U> {
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub fn update<O>(&self, _f: impl FnOnce(&mut T) -> O) {
|
||||
todo!()
|
||||
pub fn update<O>(&self, _f: impl FnOnce(&mut T) -> O) -> O {
|
||||
let mut write = self.write();
|
||||
_f(&mut *write)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 {
|
||||
let signals = self.signals.borrow();
|
||||
let inner = &signals[id];
|
||||
let inner = inner.value.downcast_ref::<T>().unwrap();
|
||||
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 {
|
||||
|
|
Loading…
Reference in a new issue