reactive_context.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. use dioxus_core::prelude::{
  2. current_scope_id, has_context, provide_context, schedule_update_any, ScopeId,
  3. };
  4. use futures_channel::mpsc::UnboundedReceiver;
  5. use generational_box::SyncStorage;
  6. use std::{cell::RefCell, hash::Hash};
  7. use crate::{CopyValue, Readable, Writable};
  8. /// A context for signal reads and writes to be directed to
  9. ///
  10. /// When a signal calls .read(), it will look for the current ReactiveContext to read from.
  11. /// If it doesn't find it, then it will try and insert a context into the nearest component scope via context api.
  12. ///
  13. /// When the ReactiveContext drops, it will remove itself from the the associated contexts attached to signal
  14. #[derive(Clone, Copy, PartialEq, Eq)]
  15. pub struct ReactiveContext {
  16. inner: CopyValue<Inner, SyncStorage>,
  17. }
  18. thread_local! {
  19. static CURRENT: RefCell<Vec<ReactiveContext>> = const { RefCell::new(vec![]) };
  20. }
  21. impl std::fmt::Display for ReactiveContext {
  22. fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
  23. #[cfg(debug_assertions)]
  24. {
  25. if let Ok(read) = self.inner.try_read() {
  26. return write!(f, "ReactiveContext created at {}", read.origin);
  27. }
  28. }
  29. write!(f, "ReactiveContext")
  30. }
  31. }
  32. impl ReactiveContext {
  33. /// Create a new reactive context
  34. #[track_caller]
  35. pub fn new() -> (Self, UnboundedReceiver<()>) {
  36. Self::new_with_origin(std::panic::Location::caller())
  37. }
  38. /// Create a new reactive context with a location for debugging purposes
  39. /// This is useful for reactive contexts created within closures
  40. pub fn new_with_origin(
  41. origin: &'static std::panic::Location<'static>,
  42. ) -> (Self, UnboundedReceiver<()>) {
  43. let (tx, rx) = futures_channel::mpsc::unbounded();
  44. let callback = move || {
  45. let _ = tx.unbounded_send(());
  46. };
  47. let _self = Self::new_with_callback(callback, current_scope_id().unwrap(), origin);
  48. (_self, rx)
  49. }
  50. /// Create a new reactive context that may update a scope. When any signal that this context subscribes to changes, the callback will be run
  51. pub fn new_with_callback(
  52. callback: impl FnMut() + Send + Sync + 'static,
  53. scope: ScopeId,
  54. origin: &'static std::panic::Location<'static>,
  55. ) -> Self {
  56. let inner = Inner {
  57. self_: None,
  58. update: Box::new(callback),
  59. #[cfg(debug_assertions)]
  60. origin,
  61. };
  62. let mut self_ = Self {
  63. inner: CopyValue::new_maybe_sync_in_scope(inner, scope),
  64. };
  65. self_.inner.write().self_ = Some(self_);
  66. self_
  67. }
  68. /// Get the current reactive context
  69. ///
  70. /// If this was set manually, then that value will be returned.
  71. ///
  72. /// If there's no current reactive context, then a new one will be created for the current scope and returned.
  73. pub fn current() -> Option<Self> {
  74. let cur = CURRENT.with(|current| current.borrow().last().cloned());
  75. // If we're already inside a reactive context, then return that
  76. if let Some(cur) = cur {
  77. return Some(cur);
  78. }
  79. // If we're rendering, then try and use the reactive context attached to this component
  80. if !dioxus_core::vdom_is_rendering() {
  81. return None;
  82. }
  83. if let Some(cx) = has_context() {
  84. return Some(cx);
  85. }
  86. let update_any = schedule_update_any();
  87. let scope_id = current_scope_id().unwrap();
  88. let update_scope = move || {
  89. tracing::trace!("Marking scope {:?} as dirty", scope_id);
  90. update_any(scope_id)
  91. };
  92. // Otherwise, create a new context at the current scope
  93. Some(provide_context(ReactiveContext::new_with_callback(
  94. update_scope,
  95. scope_id,
  96. std::panic::Location::caller(),
  97. )))
  98. }
  99. /// Run this function in the context of this reactive context
  100. ///
  101. /// This will set the current reactive context to this context for the duration of the function.
  102. /// You can then get information about the current subscriptions.
  103. pub fn run_in<O>(&self, f: impl FnOnce() -> O) -> O {
  104. CURRENT.with(|current| current.borrow_mut().push(*self));
  105. let out = f();
  106. CURRENT.with(|current| current.borrow_mut().pop());
  107. out
  108. }
  109. /// Marks this reactive context as dirty
  110. ///
  111. /// If there's a scope associated with this context, then it will be marked as dirty too
  112. ///
  113. /// Returns true if the context was marked as dirty, or false if the context has been dropped
  114. pub fn mark_dirty(&self) -> bool {
  115. let mut copy = self.inner;
  116. if let Ok(mut self_write) = copy.try_write() {
  117. #[cfg(debug_assertions)]
  118. {
  119. tracing::trace!(
  120. "Marking reactive context created at {} as dirty",
  121. self_write.origin
  122. );
  123. }
  124. (self_write.update)();
  125. true
  126. } else {
  127. false
  128. }
  129. }
  130. /// Get the scope that inner CopyValue is associated with
  131. pub fn origin_scope(&self) -> ScopeId {
  132. self.inner.origin_scope()
  133. }
  134. }
  135. impl Hash for ReactiveContext {
  136. fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
  137. self.inner.id().hash(state);
  138. }
  139. }
  140. struct Inner {
  141. self_: Option<ReactiveContext>,
  142. // Futures will call .changed().await
  143. update: Box<dyn FnMut() + Send + Sync>,
  144. // Debug information for signal subscriptions
  145. #[cfg(debug_assertions)]
  146. origin: &'static std::panic::Location<'static>,
  147. }