2024-02-01 09:05:28 +00:00
|
|
|
use dioxus_core::prelude::{
|
2024-02-14 18:13:54 +00:00
|
|
|
current_scope_id, has_context, provide_context, schedule_update_any, ScopeId,
|
2024-02-01 09:05:28 +00:00
|
|
|
};
|
2024-03-06 17:38:28 +00:00
|
|
|
use futures_channel::mpsc::UnboundedReceiver;
|
2024-02-05 14:05:27 +00:00
|
|
|
use generational_box::SyncStorage;
|
2024-03-06 17:38:28 +00:00
|
|
|
use std::{cell::RefCell, hash::Hash};
|
2024-02-01 03:12:34 +00:00
|
|
|
|
2024-03-21 06:05:50 +00:00
|
|
|
use crate::{CopyValue, Writable};
|
2024-02-01 03:12:34 +00:00
|
|
|
|
|
|
|
/// A context for signal reads and writes to be directed to
|
|
|
|
///
|
|
|
|
/// When a signal calls .read(), it will look for the current ReactiveContext to read from.
|
|
|
|
/// If it doesn't find it, then it will try and insert a context into the nearest component scope via context api.
|
|
|
|
///
|
2024-03-11 14:49:26 +00:00
|
|
|
/// When the ReactiveContext drops, it will remove itself from the associated contexts attached to signal
|
2024-02-01 09:05:28 +00:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq)]
|
2024-02-01 03:12:34 +00:00
|
|
|
pub struct ReactiveContext {
|
2024-02-02 21:36:19 +00:00
|
|
|
inner: CopyValue<Inner, SyncStorage>,
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
thread_local! {
|
2024-02-05 13:23:32 +00:00
|
|
|
static CURRENT: RefCell<Vec<ReactiveContext>> = const { RefCell::new(vec![]) };
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 17:46:01 +00:00
|
|
|
impl std::fmt::Display for ReactiveContext {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
2024-03-06 17:38:28 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2024-03-07 17:49:51 +00:00
|
|
|
{
|
2024-03-21 06:05:50 +00:00
|
|
|
use crate::Readable;
|
2024-03-07 17:49:51 +00:00
|
|
|
if let Ok(read) = self.inner.try_read() {
|
|
|
|
return write!(f, "ReactiveContext created at {}", read.origin);
|
|
|
|
}
|
|
|
|
}
|
2024-03-06 17:38:28 +00:00
|
|
|
write!(f, "ReactiveContext")
|
2024-02-05 14:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 03:12:34 +00:00
|
|
|
impl ReactiveContext {
|
2024-02-03 21:10:16 +00:00
|
|
|
/// Create a new reactive context
|
2024-02-26 17:46:01 +00:00
|
|
|
#[track_caller]
|
2024-03-06 17:38:28 +00:00
|
|
|
pub fn new() -> (Self, UnboundedReceiver<()>) {
|
|
|
|
Self::new_with_origin(std::panic::Location::caller())
|
2024-02-05 14:13:52 +00:00
|
|
|
}
|
|
|
|
|
2024-02-26 17:46:01 +00:00
|
|
|
/// Create a new reactive context with a location for debugging purposes
|
|
|
|
/// This is useful for reactive contexts created within closures
|
2024-03-06 17:38:28 +00:00
|
|
|
pub fn new_with_origin(
|
|
|
|
origin: &'static std::panic::Location<'static>,
|
|
|
|
) -> (Self, UnboundedReceiver<()>) {
|
|
|
|
let (tx, rx) = futures_channel::mpsc::unbounded();
|
|
|
|
let callback = move || {
|
|
|
|
let _ = tx.unbounded_send(());
|
|
|
|
};
|
|
|
|
let _self = Self::new_with_callback(callback, current_scope_id().unwrap(), origin);
|
|
|
|
(_self, rx)
|
2024-02-26 17:46:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 17:38:28 +00:00
|
|
|
/// Create a new reactive context that may update a scope. When any signal that this context subscribes to changes, the callback will be run
|
|
|
|
pub fn new_with_callback(
|
|
|
|
callback: impl FnMut() + Send + Sync + 'static,
|
|
|
|
scope: ScopeId,
|
2024-03-21 06:05:50 +00:00
|
|
|
#[allow(unused)] origin: &'static std::panic::Location<'static>,
|
2024-02-26 17:46:01 +00:00
|
|
|
) -> Self {
|
2024-02-01 09:05:28 +00:00
|
|
|
let inner = Inner {
|
2024-02-01 09:26:05 +00:00
|
|
|
self_: None,
|
2024-03-06 17:38:28 +00:00
|
|
|
update: Box::new(callback),
|
2024-02-26 17:46:01 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
origin,
|
2024-02-01 09:05:28 +00:00
|
|
|
};
|
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
let mut self_ = Self {
|
2024-03-06 17:38:28 +00:00
|
|
|
inner: CopyValue::new_maybe_sync_in_scope(inner, scope),
|
2024-02-01 09:05:28 +00:00
|
|
|
};
|
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
self_.inner.write().self_ = Some(self_);
|
2024-02-01 09:05:28 +00:00
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
self_
|
2024-02-01 09:05:28 +00:00
|
|
|
}
|
|
|
|
|
2024-02-01 03:12:34 +00:00
|
|
|
/// Get the current reactive context
|
|
|
|
///
|
|
|
|
/// If this was set manually, then that value will be returned.
|
|
|
|
///
|
2024-02-05 14:13:52 +00:00
|
|
|
/// If there's no current reactive context, then a new one will be created for the current scope and returned.
|
2024-02-14 15:33:22 +00:00
|
|
|
pub fn current() -> Option<Self> {
|
2024-02-01 09:05:28 +00:00
|
|
|
let cur = CURRENT.with(|current| current.borrow().last().cloned());
|
|
|
|
|
|
|
|
// If we're already inside a reactive context, then return that
|
|
|
|
if let Some(cur) = cur {
|
2024-02-14 15:33:22 +00:00
|
|
|
return Some(cur);
|
2024-02-01 09:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we're rendering, then try and use the reactive context attached to this component
|
2024-02-14 18:13:54 +00:00
|
|
|
if !dioxus_core::vdom_is_rendering() {
|
2024-02-14 15:33:22 +00:00
|
|
|
return None;
|
|
|
|
}
|
2024-02-01 09:05:28 +00:00
|
|
|
if let Some(cx) = has_context() {
|
2024-02-14 15:33:22 +00:00
|
|
|
return Some(cx);
|
2024-02-01 09:05:28 +00:00
|
|
|
}
|
2024-03-06 17:38:28 +00:00
|
|
|
let update_any = schedule_update_any();
|
|
|
|
let scope_id = current_scope_id().unwrap();
|
|
|
|
let update_scope = move || {
|
|
|
|
tracing::trace!("Marking scope {:?} as dirty", scope_id);
|
|
|
|
update_any(scope_id)
|
|
|
|
};
|
2024-02-01 09:05:28 +00:00
|
|
|
|
|
|
|
// Otherwise, create a new context at the current scope
|
2024-03-06 17:38:28 +00:00
|
|
|
Some(provide_context(ReactiveContext::new_with_callback(
|
|
|
|
update_scope,
|
|
|
|
scope_id,
|
2024-02-26 17:46:01 +00:00
|
|
|
std::panic::Location::caller(),
|
2024-02-14 18:13:54 +00:00
|
|
|
)))
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Run this function in the context of this reactive context
|
|
|
|
///
|
|
|
|
/// This will set the current reactive context to this context for the duration of the function.
|
|
|
|
/// You can then get information about the current subscriptions.
|
2024-02-01 09:05:28 +00:00
|
|
|
pub fn run_in<O>(&self, f: impl FnOnce() -> O) -> O {
|
|
|
|
CURRENT.with(|current| current.borrow_mut().push(*self));
|
|
|
|
let out = f();
|
|
|
|
CURRENT.with(|current| current.borrow_mut().pop());
|
|
|
|
out
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Marks this reactive context as dirty
|
|
|
|
///
|
|
|
|
/// If there's a scope associated with this context, then it will be marked as dirty too
|
2024-02-05 14:05:27 +00:00
|
|
|
///
|
|
|
|
/// Returns true if the context was marked as dirty, or false if the context has been dropped
|
|
|
|
pub fn mark_dirty(&self) -> bool {
|
2024-03-08 16:32:57 +00:00
|
|
|
if let Ok(mut self_write) = self.inner.try_write_unchecked() {
|
2024-02-26 17:46:01 +00:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
2024-03-06 17:38:28 +00:00
|
|
|
tracing::trace!(
|
|
|
|
"Marking reactive context created at {} as dirty",
|
|
|
|
self_write.origin
|
|
|
|
);
|
2024-02-05 14:05:27 +00:00
|
|
|
}
|
|
|
|
|
2024-03-06 17:38:28 +00:00
|
|
|
(self_write.update)();
|
|
|
|
|
2024-02-05 14:05:27 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2024-02-01 09:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-01 03:12:34 +00:00
|
|
|
|
2024-02-03 21:10:16 +00:00
|
|
|
/// Get the scope that inner CopyValue is associated with
|
|
|
|
pub fn origin_scope(&self) -> ScopeId {
|
|
|
|
self.inner.origin_scope()
|
|
|
|
}
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Hash for ReactiveContext {
|
|
|
|
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
|
|
|
self.inner.id().hash(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
struct Inner {
|
|
|
|
self_: Option<ReactiveContext>,
|
2024-02-01 03:12:34 +00:00
|
|
|
|
|
|
|
// Futures will call .changed().await
|
2024-03-06 17:38:28 +00:00
|
|
|
update: Box<dyn FnMut() + Send + Sync>,
|
2024-02-26 17:46:01 +00:00
|
|
|
|
|
|
|
// Debug information for signal subscriptions
|
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
origin: &'static std::panic::Location<'static>,
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|