2024-02-01 09:05:28 +00:00
|
|
|
use dioxus_core::prelude::{
|
2024-02-03 00:13:06 +00:00
|
|
|
current_scope_id, has_context, provide_context, schedule_update_any, ScopeId,
|
2024-02-01 09:05:28 +00:00
|
|
|
};
|
2024-02-01 03:12:34 +00:00
|
|
|
use generational_box::{GenerationalBoxId, SyncStorage};
|
2024-02-01 09:05:28 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2024-02-03 00:13:06 +00:00
|
|
|
use std::{cell::RefCell, hash::Hash, sync::Arc};
|
2024-02-01 03:12:34 +00:00
|
|
|
|
2024-02-01 09:05:28 +00:00
|
|
|
use crate::{CopyValue, RcList, Readable, 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.
|
|
|
|
///
|
|
|
|
/// When the ReactiveContext drops, it will remove itself from the 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
|
|
|
}
|
|
|
|
|
|
|
|
impl ReactiveContext {
|
2024-02-03 21:10:16 +00:00
|
|
|
/// Create a new reactive context
|
2024-02-01 09:05:28 +00:00
|
|
|
pub fn new(scope: Option<ScopeId>) -> Self {
|
|
|
|
let (tx, rx) = flume::unbounded();
|
|
|
|
|
|
|
|
let mut scope_subscribers = FxHashSet::default();
|
|
|
|
if let Some(scope) = scope {
|
|
|
|
scope_subscribers.insert(scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
let inner = Inner {
|
|
|
|
signal_subscribers: FxHashMap::default(),
|
|
|
|
scope_subscribers,
|
|
|
|
sender: tx,
|
2024-02-01 09:26:05 +00:00
|
|
|
self_: None,
|
2024-02-03 00:13:06 +00:00
|
|
|
update_any: schedule_update_any(),
|
2024-02-01 09:05:28 +00:00
|
|
|
receiver: rx,
|
|
|
|
};
|
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
let mut self_ = Self {
|
2024-02-05 13:23:32 +00:00
|
|
|
inner: CopyValue::new_maybe_sync_in_scope(
|
|
|
|
inner,
|
|
|
|
scope.or_else(current_scope_id).unwrap(),
|
|
|
|
),
|
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.
|
|
|
|
///
|
|
|
|
/// If there's no current reactive context, then a new one will be created at the current scope and returned.
|
2024-02-01 09:05:28 +00:00
|
|
|
pub fn current() -> Option<Self> {
|
|
|
|
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 {
|
|
|
|
return Some(cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're rendering, then try and use the reactive context attached to this component
|
|
|
|
if let Some(cx) = has_context() {
|
|
|
|
return Some(cx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create a new context at the current scope
|
2024-02-01 09:26:05 +00:00
|
|
|
Some(provide_context(ReactiveContext::new(current_scope_id())))
|
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-01 09:05:28 +00:00
|
|
|
pub fn mark_dirty(&self) {
|
|
|
|
for scope in self.inner.read().scope_subscribers.iter() {
|
2024-02-03 00:13:06 +00:00
|
|
|
(self.inner.read().update_any)(*scope);
|
2024-02-01 09:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark the listeners as dirty
|
|
|
|
// If the channel is full it means that the receivers have already been marked as dirty
|
|
|
|
_ = self.inner.read().sender.try_send(());
|
|
|
|
}
|
|
|
|
|
2024-02-01 09:26:05 +00:00
|
|
|
/// Create a two-way binding between this reactive context and a signal
|
|
|
|
pub fn link(&mut self, signal: GenerationalBoxId, rc_list: RcList) {
|
2024-02-01 09:05:28 +00:00
|
|
|
rc_list.write().insert(*self);
|
|
|
|
self.inner
|
|
|
|
.write()
|
|
|
|
.signal_subscribers
|
|
|
|
.insert(signal, rc_list);
|
|
|
|
}
|
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
|
|
|
/// Wait for this reactive context to change
|
|
|
|
pub async fn changed(&self) {
|
2024-02-01 09:05:28 +00:00
|
|
|
let rx = self.inner.read().receiver.clone();
|
|
|
|
_ = rx.recv_async().await;
|
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 {
|
2024-02-01 03:12:34 +00:00
|
|
|
// Set of signals bound to this context
|
2024-02-01 09:26:05 +00:00
|
|
|
signal_subscribers: FxHashMap<GenerationalBoxId, RcList>,
|
2024-02-01 09:05:28 +00:00
|
|
|
scope_subscribers: FxHashSet<ScopeId>,
|
2024-02-01 09:26:05 +00:00
|
|
|
self_: Option<ReactiveContext>,
|
2024-02-03 00:13:06 +00:00
|
|
|
update_any: Arc<dyn Fn(ScopeId) + Send + Sync>,
|
2024-02-01 03:12:34 +00:00
|
|
|
|
|
|
|
// Futures will call .changed().await
|
2024-02-01 09:05:28 +00:00
|
|
|
sender: flume::Sender<()>,
|
|
|
|
receiver: flume::Receiver<()>,
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Inner {
|
2024-02-01 09:26:05 +00:00
|
|
|
// Remove this context from all the subscribers
|
2024-02-01 03:12:34 +00:00
|
|
|
fn drop(&mut self) {
|
2024-02-01 09:05:28 +00:00
|
|
|
self.signal_subscribers.values().for_each(|sub_list| {
|
2024-02-01 09:26:05 +00:00
|
|
|
sub_list.write().remove(&self.self_.unwrap());
|
2024-02-01 09:05:28 +00:00
|
|
|
});
|
2024-02-01 03:12:34 +00:00
|
|
|
}
|
|
|
|
}
|