rt.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. use std::{any::Any, cell::RefCell, sync::Arc};
  2. use dioxus_core::ScopeId;
  3. use slab::Slab;
  4. thread_local! {
  5. // we cannot drop these since any future might be using them
  6. static RUNTIMES: RefCell<Vec<&'static SignalRt>> = RefCell::new(Vec::new());
  7. }
  8. /// Provide the runtime for signals
  9. ///
  10. /// This will reuse dead runtimes
  11. pub fn claim_rt(update_any: Arc<dyn Fn(ScopeId)>) -> &'static SignalRt {
  12. RUNTIMES.with(|runtimes| {
  13. if let Some(rt) = runtimes.borrow_mut().pop() {
  14. return rt;
  15. }
  16. Box::leak(Box::new(SignalRt {
  17. signals: RefCell::new(Slab::new()),
  18. update_any,
  19. }))
  20. })
  21. }
  22. /// Push this runtime into the global runtime list
  23. pub fn reclam_rt(_rt: &'static SignalRt) {
  24. RUNTIMES.with(|runtimes| {
  25. runtimes.borrow_mut().push(_rt);
  26. });
  27. }
  28. pub struct SignalRt {
  29. pub(crate) signals: RefCell<Slab<Inner>>,
  30. pub(crate) update_any: Arc<dyn Fn(ScopeId)>,
  31. }
  32. impl SignalRt {
  33. pub fn init<T: 'static>(&'static self, val: T) -> usize {
  34. self.signals.borrow_mut().insert(Inner {
  35. value: Box::new(val),
  36. subscribers: Vec::new(),
  37. getter: None,
  38. })
  39. }
  40. pub fn subscribe(&self, id: usize, subscriber: ScopeId) {
  41. self.signals.borrow_mut()[id].subscribers.push(subscriber);
  42. }
  43. pub fn get<T: Clone + 'static>(&self, id: usize) -> T {
  44. self.signals.borrow()[id]
  45. .value
  46. .downcast_ref::<T>()
  47. .cloned()
  48. .unwrap()
  49. }
  50. pub fn set<T: 'static>(&self, id: usize, value: T) {
  51. let mut signals = self.signals.borrow_mut();
  52. let inner = &mut signals[id];
  53. inner.value = Box::new(value);
  54. for subscriber in inner.subscribers.iter() {
  55. (self.update_any)(*subscriber);
  56. }
  57. }
  58. pub fn remove(&self, id: usize) {
  59. self.signals.borrow_mut().remove(id);
  60. }
  61. pub fn with<T: 'static, O>(&self, id: usize, f: impl FnOnce(&T) -> O) -> O {
  62. let signals = self.signals.borrow();
  63. let inner = &signals[id];
  64. let inner = inner.value.downcast_ref::<T>().unwrap();
  65. f(inner)
  66. }
  67. pub(crate) fn read<T: 'static>(&self, id: usize) -> std::cell::Ref<T> {
  68. let signals = self.signals.borrow();
  69. std::cell::Ref::map(signals, |signals| {
  70. signals[id].value.downcast_ref::<T>().unwrap()
  71. })
  72. }
  73. pub(crate) fn write<T: 'static>(&self, id: usize) -> std::cell::RefMut<T> {
  74. let signals = self.signals.borrow_mut();
  75. std::cell::RefMut::map(signals, |signals| {
  76. signals[id].value.downcast_mut::<T>().unwrap()
  77. })
  78. }
  79. pub(crate) fn getter<T: 'static + Clone>(&self, id: usize) -> &dyn Fn() -> T {
  80. let mut signals = self.signals.borrow_mut();
  81. let inner = &mut signals[id];
  82. let r = inner.getter.as_mut();
  83. if r.is_none() {
  84. let rt = self;
  85. let r = move || rt.get::<T>(id);
  86. let getter: Box<dyn Fn() -> T> = Box::new(r);
  87. let getter: Box<dyn Fn()> = unsafe { std::mem::transmute(getter) };
  88. inner.getter = Some(getter);
  89. }
  90. let r = inner.getter.as_ref().unwrap();
  91. unsafe { std::mem::transmute::<&dyn Fn(), &dyn Fn() -> T>(r) }
  92. }
  93. }
  94. pub(crate) struct Inner {
  95. pub value: Box<dyn Any>,
  96. pub subscribers: Vec<ScopeId>,
  97. // todo: this has a soundness hole in it that you might not run into
  98. pub getter: Option<Box<dyn Fn()>>,
  99. }