feat: add recycling of rts in signals

This commit is contained in:
Jonathan Kelley 2023-01-01 22:09:08 -05:00
parent c0315e55f4
commit fd92079eb3
2 changed files with 22 additions and 20 deletions

View file

@ -1,7 +1,3 @@
//! 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;
@ -15,7 +11,7 @@ fn app(cx: Scope) -> Element {
let mut count = use_signal(cx, || 0);
use_coroutine(cx, |_: UnboundedReceiver<()>| async move {
use_future!(cx, || async move {
loop {
count += 1;
tokio::time::sleep(Duration::from_millis(100)).await;

View file

@ -1,27 +1,33 @@
use std::{
any::Any,
cell::RefCell,
rc::Rc,
sync::{Arc, Mutex},
};
use std::{any::Any, cell::RefCell, sync::Arc};
use dioxus_core::ScopeId;
use slab::Slab;
thread_local! {
static RUNTIMES: RefCell<Vec<&'static SignalRt>> = RefCell::new(Vec::new());
}
/// 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,
}))
RUNTIMES.with(|runtimes| {
if let Some(rt) = runtimes.borrow_mut().pop() {
return rt;
}
Box::leak(Box::new(SignalRt {
signals: RefCell::new(Slab::new()),
update_any,
}))
})
}
pub fn reclam_rt(rt: usize) {}
struct GlobalRt {
signals: Slab<Inner>,
/// Push this runtime into the global runtime list
pub fn reclam_rt(_rt: &'static SignalRt) {
RUNTIMES.with(|runtimes| {
runtimes.borrow_mut().push(_rt);
});
}
pub struct SignalRt {
@ -78,7 +84,7 @@ impl SignalRt {
}
pub(crate) fn write<T: 'static>(&self, id: usize) -> std::cell::RefMut<T> {
let mut signals = self.signals.borrow_mut();
let signals = self.signals.borrow_mut();
std::cell::RefMut::map(signals, |signals| {
signals[id].value.downcast_mut::<T>().unwrap()
})