computed.rs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. use dioxus_core::{ScopeId, ScopeState};
  2. use slab::Slab;
  3. use std::{
  4. cell::{RefCell, RefMut},
  5. collections::HashSet,
  6. ops::{Deref, DerefMut},
  7. rc::Rc,
  8. };
  9. /// Create a new tracked state.
  10. /// Tracked state is state that can drive Selector state
  11. ///
  12. /// It state will efficiently update any Selector state that is reading from it, but it is not readable on it's own.
  13. ///
  14. /// ```rust
  15. /// use dioxus::prelude::*;
  16. ///
  17. /// fn Parent(cx: Scope) -> Element {
  18. /// let count = use_tracked_state(cx, || 0);
  19. ///
  20. /// render! {
  21. /// Child {
  22. /// count: count.clone(),
  23. /// }
  24. /// }
  25. /// }
  26. ///
  27. /// #[inline_props]
  28. /// fn Child(cx: Scope, count: Tracked<usize>) -> Element {
  29. /// let less_than_five = use_selector(cx, count, |count| *count < 5);
  30. ///
  31. /// render! {
  32. /// "{less_than_five}"
  33. /// }
  34. /// }
  35. /// ```
  36. pub fn use_tracked_state<T: 'static>(cx: &ScopeState, init: impl FnOnce() -> T) -> &Tracked<T> {
  37. cx.use_hook(|| {
  38. let init = init();
  39. Tracked::new(cx, init)
  40. })
  41. }
  42. /// Tracked state is state that can drive Selector state
  43. ///
  44. /// Tracked state will efficiently update any Selector state that is reading from it, but it is not readable on it's own.
  45. #[derive(Clone)]
  46. pub struct Tracked<I> {
  47. state: Rc<RefCell<I>>,
  48. update_any: std::sync::Arc<dyn Fn(ScopeId)>,
  49. subscribers: SubscribedCallbacks<I>,
  50. }
  51. impl<I: PartialEq> PartialEq for Tracked<I> {
  52. fn eq(&self, other: &Self) -> bool {
  53. self.state == other.state
  54. }
  55. }
  56. impl<I> Tracked<I> {
  57. /// Create a new tracked state
  58. pub fn new(cx: &ScopeState, state: I) -> Self {
  59. let subscribers = std::rc::Rc::new(std::cell::RefCell::new(Slab::new()));
  60. Self {
  61. state: Rc::new(RefCell::new(state)),
  62. subscribers,
  63. update_any: cx.schedule_update_any(),
  64. }
  65. }
  66. /// Create a new Selector state from this tracked state
  67. pub fn compute<O: PartialEq + 'static>(
  68. &self,
  69. mut compute: impl FnMut(&I) -> O + 'static,
  70. ) -> Selector<O, I> {
  71. let subscribers = Rc::new(RefCell::new(HashSet::new()));
  72. let state = Rc::new(RefCell::new(compute(&self.state.borrow())));
  73. let update_any = self.update_any.clone();
  74. Selector {
  75. value: state.clone(),
  76. subscribers: subscribers.clone(),
  77. _tracker: Rc::new(self.track(move |input_state| {
  78. let new = compute(input_state);
  79. let different = {
  80. let state = state.borrow();
  81. *state != new
  82. };
  83. if different {
  84. let mut state = state.borrow_mut();
  85. *state = new;
  86. for id in subscribers.borrow().iter().copied() {
  87. (update_any)(id);
  88. }
  89. }
  90. })),
  91. }
  92. }
  93. pub(crate) fn track(&self, update: impl FnMut(&I) + 'static) -> Tracker<I> {
  94. let mut subscribers = self.subscribers.borrow_mut();
  95. let id = subscribers.insert(Box::new(update));
  96. Tracker {
  97. subscribers: self.subscribers.clone(),
  98. id,
  99. }
  100. }
  101. /// Write to the tracked state
  102. pub fn write(&self) -> TrackedMut<'_, I> {
  103. TrackedMut {
  104. state: self.state.borrow_mut(),
  105. subscribers: self.subscribers.clone(),
  106. }
  107. }
  108. }
  109. /// A mutable reference to tracked state
  110. pub struct TrackedMut<'a, I> {
  111. state: RefMut<'a, I>,
  112. subscribers: SubscribedCallbacks<I>,
  113. }
  114. impl<'a, I> Deref for TrackedMut<'a, I> {
  115. type Target = I;
  116. fn deref(&self) -> &Self::Target {
  117. &self.state
  118. }
  119. }
  120. impl<'a, I> DerefMut for TrackedMut<'a, I> {
  121. fn deref_mut(&mut self) -> &mut Self::Target {
  122. &mut self.state
  123. }
  124. }
  125. impl<'a, I> Drop for TrackedMut<'a, I> {
  126. fn drop(&mut self) {
  127. let state = self.state.deref();
  128. for (_, sub) in &mut *self.subscribers.borrow_mut() {
  129. sub(state);
  130. }
  131. }
  132. }
  133. type SubscribedCallbacks<I> = std::rc::Rc<std::cell::RefCell<Slab<Box<dyn FnMut(&I) + 'static>>>>;
  134. pub(crate) struct Tracker<I> {
  135. subscribers: SubscribedCallbacks<I>,
  136. id: usize,
  137. }
  138. impl<I> Drop for Tracker<I> {
  139. fn drop(&mut self) {
  140. let _ = self.subscribers.borrow_mut().remove(self.id);
  141. }
  142. }
  143. pub fn use_selector<I: 'static, O: Clone + PartialEq + 'static>(
  144. cx: &ScopeState,
  145. tracked: &Tracked<I>,
  146. init: impl FnMut(&I) -> O + 'static,
  147. ) -> O {
  148. let selector = cx.use_hook(|| tracked.compute(init));
  149. selector.use_state(cx)
  150. }
  151. /// Selector state is state that is derived from tracked state
  152. ///
  153. /// Whenever the tracked state changes, the Selector state will be updated and any components reading from it will be rerun
  154. #[derive(Clone)]
  155. pub struct Selector<T, I> {
  156. _tracker: Rc<Tracker<I>>,
  157. value: Rc<RefCell<T>>,
  158. subscribers: Rc<RefCell<HashSet<ScopeId>>>,
  159. }
  160. impl<T, I> PartialEq for Selector<T, I> {
  161. fn eq(&self, other: &Self) -> bool {
  162. std::rc::Rc::ptr_eq(&self.value, &other.value)
  163. }
  164. }
  165. impl<T: Clone + PartialEq, I> Selector<T, I> {
  166. /// Read the Selector state and subscribe to updates
  167. pub fn use_state(&self, cx: &ScopeState) -> T {
  168. cx.use_hook(|| {
  169. let id = cx.scope_id();
  170. self.subscribers.borrow_mut().insert(id);
  171. ComputedRead {
  172. scope: cx.scope_id(),
  173. subscribers: self.subscribers.clone(),
  174. }
  175. });
  176. self.value.borrow().clone()
  177. }
  178. }
  179. struct ComputedRead {
  180. scope: ScopeId,
  181. subscribers: std::rc::Rc<std::cell::RefCell<std::collections::HashSet<ScopeId>>>,
  182. }
  183. impl Drop for ComputedRead {
  184. fn drop(&mut self) {
  185. self.subscribers.borrow_mut().remove(&self.scope);
  186. }
  187. }