feat: add recycling of rts in signals
This commit is contained in:
1 parent
c0315e55f4
commit
fd92079eb3
2 files changed
+22
-20
No files matched your search
+1
-5
@@ -1,7 +1,3 @@
|
|||||||
//! Example: README.md showcase
|
|
||||||
//!
|
|
||||||
//! The example from the README.md.
|
|
||||||
|
|
||||||
use dioxus::prelude::*;
|
use dioxus::prelude::*;
|
||||||
use dioxus_signals::{use_init_signal_rt, use_signal};
|
use dioxus_signals::{use_init_signal_rt, use_signal};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -15,7 +11,7 @@ fn app(cx: Scope) -> Element {
|
|||||||
|
|
||||||
let mut count = use_signal(cx, || 0);
|
let mut count = use_signal(cx, || 0);
|
||||||
|
|
||||||
use_coroutine(cx, |_: UnboundedReceiver<()>| async move {
|
use_future!(cx, || async move {
|
||||||
loop {
|
loop {
|
||||||
count += 1;
|
count += 1;
|
||||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
|
|||||||
+21
-15
@@ -1,27 +1,33 @@
|
|||||||
use std::{
|
use std::{any::Any, cell::RefCell, sync::Arc};
|
||||||
any::Any,
|
|
||||||
cell::RefCell,
|
|
||||||
rc::Rc,
|
|
||||||
sync::{Arc, Mutex},
|
|
||||||
};
|
|
||||||
|
|
||||||
use dioxus_core::ScopeId;
|
use dioxus_core::ScopeId;
|
||||||
use slab::Slab;
|
use slab::Slab;
|
||||||
|
|
||||||
|
thread_local! {
|
||||||
|
static RUNTIMES: RefCell<Vec<&'static SignalRt>> = RefCell::new(Vec::new());
|
||||||
|
}
|
||||||
|
|
||||||
/// Provide the runtime for signals
|
/// Provide the runtime for signals
|
||||||
///
|
///
|
||||||
/// This will reuse dead runtimes
|
/// This will reuse dead runtimes
|
||||||
pub fn claim_rt(update_any: Arc<dyn Fn(ScopeId)>) -> &'static SignalRt {
|
pub fn claim_rt(update_any: Arc<dyn Fn(ScopeId)>) -> &'static SignalRt {
|
||||||
Box::leak(Box::new(SignalRt {
|
RUNTIMES.with(|runtimes| {
|
||||||
signals: RefCell::new(Slab::new()),
|
if let Some(rt) = runtimes.borrow_mut().pop() {
|
||||||
update_any,
|
return rt;
|
||||||
}))
|
}
|
||||||
|
|
||||||
|
Box::leak(Box::new(SignalRt {
|
||||||
|
signals: RefCell::new(Slab::new()),
|
||||||
|
update_any,
|
||||||
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reclam_rt(rt: usize) {}
|
/// Push this runtime into the global runtime list
|
||||||
|
pub fn reclam_rt(_rt: &'static SignalRt) {
|
||||||
struct GlobalRt {
|
RUNTIMES.with(|runtimes| {
|
||||||
signals: Slab<Inner>,
|
runtimes.borrow_mut().push(_rt);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SignalRt {
|
pub struct SignalRt {
|
||||||
@@ -78,7 +84,7 @@ impl SignalRt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn write<T: 'static>(&self, id: usize) -> std::cell::RefMut<T> {
|
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| {
|
std::cell::RefMut::map(signals, |signals| {
|
||||||
signals[id].value.downcast_mut::<T>().unwrap()
|
signals[id].value.downcast_mut::<T>().unwrap()
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user