feat: simple signals implementation

This commit is contained in:
Jonathan Kelley 2023-01-01 19:57:33 -05:00
parent c8f88b6428
commit 4f9d67fb45
5 changed files with 257 additions and 0 deletions

View file

@ -19,6 +19,7 @@ members = [
"packages/native-core",
"packages/native-core-macro",
"packages/rsx-rosetta",
"packages/signals",
"docs/guide",
]
@ -43,6 +44,7 @@ dioxus = { path = "./packages/dioxus" }
dioxus-desktop = { path = "./packages/desktop", features = ["transparent"] }
dioxus-ssr = { path = "./packages/ssr" }
dioxus-router = { path = "./packages/router" }
dioxus-signals = { path = "./packages/signals" }
fermi = { path = "./packages/fermi" }
futures-util = "0.3.21"
log = "0.4.14"

41
examples/signals.rs Normal file
View file

@ -0,0 +1,41 @@
//! Example: README.md showcase
//!
//! The example from the README.md.
use dioxus::prelude::*;
use dioxus_signals::{use_init_signal_rt, use_signal};
use std::time::Duration;
fn main() {
dioxus_desktop::launch(app);
}
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;
}
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!" } )
}
})
}

View file

@ -0,0 +1,10 @@
[package]
name = "dioxus-signals"
version = "0.0.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
dioxus-core = { path = "../core" }
slab = "0.4.7"

131
packages/signals/src/lib.rs Normal file
View file

@ -0,0 +1,131 @@
use std::{
fmt::Display,
marker::PhantomData,
ops::{Add, Div, Mul, Sub},
};
mod rt;
use dioxus_core::ScopeState;
pub use rt::*;
pub fn use_init_signal_rt(cx: &ScopeState) {
cx.use_hook(|| {
let rt = crate::rt::claim_rt(cx.schedule_update_any());
cx.provide_context(rt);
});
}
pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> {
*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,
}
})
}
pub struct Signal<T> {
id: usize,
rt: &'static SignalRt,
t: PhantomData<T>,
}
impl<T: 'static> Signal<T> {
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!()
}
}
impl<T: Clone + 'static> Signal<T> {
pub fn get(&self) -> T {
self.rt.get(self.id)
}
}
// impl<T> std::ops::Deref for Signal<T> {
// type Target = dyn Fn() -> T;
// fn deref(&self) -> &Self::Target {
// todo!()
// }
// }
impl<T> std::clone::Clone for Signal<T> {
fn clone(&self) -> Self {
Self {
t: PhantomData,
id: self.id,
rt: self.rt,
}
}
}
impl<T> Copy for Signal<T> {}
impl<T: Display + 'static> Display for Signal<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.rt.with::<T, _>(self.id, |v| T::fmt(v, f))
}
}
impl<T: Add<Output = T> + Copy + 'static> std::ops::AddAssign<T> for Signal<T> {
fn add_assign(&mut self, rhs: T) {
self.set(self.get() + rhs);
}
}
impl<T: Sub<Output = T> + Copy + 'static> std::ops::SubAssign<T> for Signal<T> {
fn sub_assign(&mut self, rhs: T) {
self.set(self.get() - rhs);
}
}
impl<T: Mul<Output = T> + Copy + 'static> std::ops::MulAssign<T> for Signal<T> {
fn mul_assign(&mut self, rhs: T) {
self.set(self.get() * rhs);
}
}
impl<T: Div<Output = T> + Copy + 'static> std::ops::DivAssign<T> for Signal<T> {
fn div_assign(&mut self, rhs: 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

@ -0,0 +1,73 @@
use std::{
any::Any,
cell::RefCell,
rc::Rc,
sync::{Arc, Mutex},
};
use dioxus_core::ScopeId;
use slab::Slab;
/// Provide the runtime for signals
///
/// This will reuse dead runtimes
pub fn claim_rt(update_any: Arc<dyn Fn(ScopeId)>) -> &'static SignalRt {
Box::leak(Box::new(SignalRt {
signals: RefCell::new(Slab::new()),
update_any,
}))
}
pub fn reclam_rt(rt: usize) {}
struct GlobalRt {
signals: Slab<Inner>,
}
pub struct SignalRt {
signals: RefCell<Slab<Inner>>,
update_any: Arc<dyn Fn(ScopeId)>,
}
impl SignalRt {
pub fn init<T: 'static>(&self, val: T) -> usize {
self.signals.borrow_mut().insert(Inner {
value: Box::new(val),
subscribers: Vec::new(),
})
}
pub fn subscribe(&self, id: usize, subscriber: ScopeId) {
self.signals.borrow_mut()[id].subscribers.push(subscriber);
}
pub fn get<T: Clone + 'static>(&self, id: usize) -> T {
self.signals.borrow()[id]
.value
.downcast_ref::<T>()
.cloned()
.unwrap()
}
pub fn set<T: 'static>(&self, id: usize, value: T) {
let mut signals = self.signals.borrow_mut();
let inner = &mut signals[id];
inner.value = Box::new(value);
for subscriber in inner.subscribers.iter() {
(self.update_any)(*subscriber);
}
}
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)
}
}
struct Inner {
value: Box<dyn Any>,
subscribers: Vec<ScopeId>,
}