computed.rs 5.9 KB

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