reactive_context.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. use crate::{
  2. prelude::{current_scope_id, ScopeId},
  3. scope_context::Scope,
  4. tasks::SchedulerMsg,
  5. Runtime,
  6. };
  7. use futures_channel::mpsc::UnboundedReceiver;
  8. use generational_box::{GenerationalBox, SyncStorage};
  9. use std::{
  10. cell::RefCell,
  11. collections::HashSet,
  12. hash::Hash,
  13. sync::{Arc, Mutex},
  14. };
  15. #[doc = include_str!("../docs/reactivity.md")]
  16. #[derive(Clone, Copy)]
  17. pub struct ReactiveContext {
  18. scope: ScopeId,
  19. inner: GenerationalBox<Inner, SyncStorage>,
  20. }
  21. impl PartialEq for ReactiveContext {
  22. fn eq(&self, other: &Self) -> bool {
  23. self.inner.ptr_eq(&other.inner)
  24. }
  25. }
  26. impl Eq for ReactiveContext {}
  27. thread_local! {
  28. static CURRENT: RefCell<Vec<ReactiveContext>> = const { RefCell::new(vec![]) };
  29. }
  30. impl std::fmt::Display for ReactiveContext {
  31. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  32. #[cfg(debug_assertions)]
  33. {
  34. if let Ok(read) = self.inner.try_read() {
  35. if let Some(scope) = read.scope {
  36. return write!(f, "ReactiveContext(for scope: {:?})", scope);
  37. }
  38. return write!(f, "ReactiveContext created at {}", read.origin);
  39. }
  40. }
  41. write!(f, "ReactiveContext")
  42. }
  43. }
  44. impl ReactiveContext {
  45. /// Create a new reactive context
  46. #[track_caller]
  47. pub fn new() -> (Self, UnboundedReceiver<()>) {
  48. Self::new_with_origin(std::panic::Location::caller())
  49. }
  50. /// Create a new reactive context with a location for debugging purposes
  51. /// This is useful for reactive contexts created within closures
  52. pub fn new_with_origin(
  53. origin: &'static std::panic::Location<'static>,
  54. ) -> (Self, UnboundedReceiver<()>) {
  55. let (tx, rx) = futures_channel::mpsc::unbounded();
  56. let callback = move || {
  57. // If there is already an update queued, we don't need to queue another
  58. if !tx.is_empty() {
  59. return;
  60. }
  61. let _ = tx.unbounded_send(());
  62. };
  63. let _self = Self::new_with_callback(callback, current_scope_id().unwrap(), origin);
  64. (_self, rx)
  65. }
  66. /// Create a new reactive context that may update a scope. When any signal that this context subscribes to changes, the callback will be run
  67. pub fn new_with_callback(
  68. callback: impl FnMut() + Send + Sync + 'static,
  69. scope: ScopeId,
  70. #[allow(unused)] origin: &'static std::panic::Location<'static>,
  71. ) -> Self {
  72. let inner = Inner {
  73. self_: None,
  74. update: Box::new(callback),
  75. subscribers: Default::default(),
  76. #[cfg(debug_assertions)]
  77. origin,
  78. #[cfg(debug_assertions)]
  79. scope: None,
  80. };
  81. let owner = scope.owner();
  82. let self_ = Self {
  83. scope,
  84. inner: owner.insert(inner),
  85. };
  86. self_.inner.write().self_ = Some(self_);
  87. self_
  88. }
  89. /// Get the current reactive context from the nearest reactive hook or scope
  90. pub fn current() -> Option<Self> {
  91. CURRENT.with(|current| current.borrow().last().cloned())
  92. }
  93. /// Create a reactive context for a scope id
  94. pub(crate) fn new_for_scope(scope: &Scope, runtime: &Runtime) -> Self {
  95. let id = scope.id;
  96. let sender = runtime.sender.clone();
  97. let update_scope = move || {
  98. tracing::trace!("Marking scope {:?} as dirty", id);
  99. sender.unbounded_send(SchedulerMsg::Immediate(id)).unwrap();
  100. };
  101. // Otherwise, create a new context at the current scope
  102. let inner = Inner {
  103. self_: None,
  104. update: Box::new(update_scope),
  105. subscribers: Default::default(),
  106. #[cfg(debug_assertions)]
  107. origin: std::panic::Location::caller(),
  108. #[cfg(debug_assertions)]
  109. scope: Some(id),
  110. };
  111. let owner = scope.owner();
  112. let self_ = Self {
  113. scope: id,
  114. inner: owner.insert(inner),
  115. };
  116. self_.inner.write().self_ = Some(self_);
  117. self_
  118. }
  119. /// Clear all subscribers to this context
  120. pub fn clear_subscribers(&self) {
  121. let old_subscribers = std::mem::take(&mut self.inner.write().subscribers);
  122. for subscriber in old_subscribers {
  123. subscriber.0.lock().unwrap().remove(self);
  124. }
  125. }
  126. /// Update the subscribers
  127. pub(crate) fn update_subscribers(&self) {
  128. let subscribers = &self.inner.read().subscribers;
  129. for subscriber in subscribers.iter() {
  130. subscriber.0.lock().unwrap().insert(*self);
  131. }
  132. }
  133. /// Reset the reactive context and then run the callback in the context. This can be used to create custom reactive hooks like `use_memo`.
  134. ///
  135. /// ```rust, no_run
  136. /// # use dioxus::prelude::*;
  137. /// # use futures_util::StreamExt;
  138. /// fn use_simplified_memo(mut closure: impl FnMut() -> i32 + 'static) -> Signal<i32> {
  139. /// use_hook(|| {
  140. /// // Create a new reactive context and channel that will recieve a value every time a value the reactive context subscribes to changes
  141. /// let (reactive_context, mut changed) = ReactiveContext::new();
  142. /// // Compute the value of the memo inside the reactive context. This will subscribe the reactive context to any values you read inside the closure
  143. /// let value = reactive_context.reset_and_run_in(&mut closure);
  144. /// // Create a new signal with the value of the memo
  145. /// let mut signal = Signal::new(value);
  146. /// // Create a task that reruns the closure when the reactive context changes
  147. /// spawn(async move {
  148. /// while changed.next().await.is_some() {
  149. /// // Since we reset the reactive context as we run the closure, our memo will only subscribe to the new values that are read in the closure
  150. /// let new_value = reactive_context.run_in(&mut closure);
  151. /// if new_value != value {
  152. /// signal.set(new_value);
  153. /// }
  154. /// }
  155. /// });
  156. /// signal
  157. /// })
  158. /// }
  159. ///
  160. /// let mut boolean = use_signal(|| false);
  161. /// let mut count = use_signal(|| 0);
  162. /// // Because we use `reset_and_run_in` instead of just `run_in`, our memo will only subscribe to the signals that are read this run of the closure (initially just the boolean)
  163. /// let memo = use_simplified_memo(move || if boolean() { count() } else { 0 });
  164. /// println!("{memo}");
  165. /// // Because the count signal is not read in this run of the closure, the memo will not rerun
  166. /// count += 1;
  167. /// println!("{memo}");
  168. /// // Because the boolean signal is read in this run of the closure, the memo will rerun
  169. /// boolean.toggle();
  170. /// println!("{memo}");
  171. /// // If we toggle the boolean again, and the memo unsubscribes from the count signal
  172. /// boolean.toggle();
  173. /// println!("{memo}");
  174. /// ```
  175. pub fn reset_and_run_in<O>(&self, f: impl FnOnce() -> O) -> O {
  176. self.clear_subscribers();
  177. self.run_in(f)
  178. }
  179. /// Run this function in the context of this reactive context
  180. ///
  181. /// This will set the current reactive context to this context for the duration of the function.
  182. /// You can then get information about the current subscriptions.
  183. pub fn run_in<O>(&self, f: impl FnOnce() -> O) -> O {
  184. CURRENT.with(|current| current.borrow_mut().push(*self));
  185. let out = f();
  186. CURRENT.with(|current| current.borrow_mut().pop());
  187. self.update_subscribers();
  188. out
  189. }
  190. /// Marks this reactive context as dirty
  191. ///
  192. /// If there's a scope associated with this context, then it will be marked as dirty too
  193. ///
  194. /// Returns true if the context was marked as dirty, or false if the context has been dropped
  195. pub fn mark_dirty(&self) -> bool {
  196. if let Ok(mut self_write) = self.inner.try_write() {
  197. #[cfg(debug_assertions)]
  198. {
  199. tracing::trace!(
  200. "Marking reactive context created at {} as dirty",
  201. self_write.origin
  202. );
  203. }
  204. (self_write.update)();
  205. true
  206. } else {
  207. false
  208. }
  209. }
  210. /// Subscribe to this context. The reactive context will automatically remove itself from the subscriptions when it is reset.
  211. pub fn subscribe(&self, subscriptions: Arc<Mutex<HashSet<ReactiveContext>>>) {
  212. subscriptions.lock().unwrap().insert(*self);
  213. self.inner
  214. .write()
  215. .subscribers
  216. .insert(PointerHash(subscriptions));
  217. }
  218. /// Get the scope that inner CopyValue is associated with
  219. pub fn origin_scope(&self) -> ScopeId {
  220. self.scope
  221. }
  222. }
  223. impl Hash for ReactiveContext {
  224. fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
  225. self.inner.id().hash(state);
  226. }
  227. }
  228. struct PointerHash<T>(Arc<T>);
  229. impl<T> Hash for PointerHash<T> {
  230. fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
  231. std::sync::Arc::<T>::as_ptr(&self.0).hash(state);
  232. }
  233. }
  234. impl<T> PartialEq for PointerHash<T> {
  235. fn eq(&self, other: &Self) -> bool {
  236. std::sync::Arc::<T>::as_ptr(&self.0) == std::sync::Arc::<T>::as_ptr(&other.0)
  237. }
  238. }
  239. impl<T> Eq for PointerHash<T> {}
  240. impl<T> Clone for PointerHash<T> {
  241. fn clone(&self) -> Self {
  242. Self(self.0.clone())
  243. }
  244. }
  245. type SubscriberMap = Mutex<HashSet<ReactiveContext>>;
  246. struct Inner {
  247. self_: Option<ReactiveContext>,
  248. // Futures will call .changed().await
  249. update: Box<dyn FnMut() + Send + Sync>,
  250. // Subscribers to this context
  251. subscribers: HashSet<PointerHash<SubscriberMap>>,
  252. // Debug information for signal subscriptions
  253. #[cfg(debug_assertions)]
  254. origin: &'static std::panic::Location<'static>,
  255. #[cfg(debug_assertions)]
  256. // The scope that this reactive context is associated with
  257. scope: Option<ScopeId>,
  258. }