use crate::context::{GraphNode, Read, Write};
pub trait Teardown {
fn len(&self) -> usize;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn disarm(self)
where
Self: Sized;
}
pub trait ReactiveGraph {
type Computed<T>: GraphNode + Copy;
type Source<T>: GraphNode + Copy;
type Effect: GraphNode + Copy;
type Scope<'a>: Teardown
where
Self: 'a;
fn dispose_slot<T: 'static>(&self, handle: &Self::Computed<T>);
fn dispose_cell<T: 'static>(&self, handle: &Self::Source<T>);
fn dispose_effect(&self, handle: &Self::Effect);
fn scope(&self) -> Self::Scope<'_>;
fn batch<R>(&self, run: impl FnOnce(&Self) -> R) -> R;
fn dependent_count(&self, node: &impl GraphNode) -> usize;
fn dependency_count(&self, node: &impl GraphNode) -> usize;
}
pub trait ThreadSafeReactiveGraph: ReactiveGraph + Send + Sync + 'static {}
impl<T: ReactiveGraph + Send + Sync + 'static> ThreadSafeReactiveGraph for T {}
pub trait SyncReactiveGraph: ReactiveGraph {
type Compute<'a>
where
Self: 'a;
fn source<T>(&self, value: T) -> Self::Source<T>
where
T: PartialEq + Send + Sync + 'static;
fn get<H>(&self, handle: &H) -> <H as Read<Self>>::Output
where
Self: Sized,
H: Read<Self> + ?Sized,
{
handle.read(self)
}
fn set<H>(&self, handle: &H, value: <H as Write<Self>>::Value)
where
Self: Sized,
H: Write<Self> + ?Sized,
{
handle.write(self, value);
}
#[deprecated(note = "use `SyncReactiveGraph::source`")]
fn cell<T>(&self, value: T) -> Self::Source<T>
where
Self: Sized,
T: PartialEq + Send + Sync + 'static,
{
self.source(value)
}
#[deprecated(note = "use `SyncReactiveGraph::get`")]
fn get_cell<T>(&self, handle: &Self::Source<T>) -> T
where
Self: Sized,
T: Clone + Send + Sync + 'static,
Self::Source<T>: Read<Self, Output = T>,
{
self.get(handle)
}
#[deprecated(note = "use `SyncReactiveGraph::set`")]
fn set_cell<T>(&self, handle: &Self::Source<T>, value: T)
where
Self: Sized,
T: PartialEq + Send + Sync + 'static,
Self::Source<T>: Write<Self, Value = T>,
{
self.set(handle, value);
}
fn computed<T, F>(&self, compute: F) -> Self::Computed<T>
where
T: PartialEq + Send + Sync + 'static,
F: Fn(&Self::Compute<'_>) -> T + Send + Sync + 'static;
fn effect<F, C>(&self, run: F) -> Self::Effect
where
F: Fn(&Self::Compute<'_>) -> C + Send + Sync + 'static,
C: FnOnce() + Send + Sync + 'static;
}
pub trait AsyncReactiveGraph: ReactiveGraph {
fn source<T>(&self, value: T) -> Self::Source<T>
where
T: PartialEq + Clone + Send + Sync + 'static;
fn get<H>(&self, handle: &H) -> <H as Read<Self>>::Output
where
Self: Sized,
H: Read<Self> + ?Sized,
{
handle.read(self)
}
fn set<H>(&self, handle: &H, value: <H as Write<Self>>::Value)
where
Self: Sized,
H: Write<Self> + ?Sized,
{
handle.write(self, value);
}
fn get_async<T>(&self, handle: &Self::Computed<T>) -> impl Future<Output = T> + Send
where
T: Clone + Send + Sync + 'static;
#[deprecated(note = "use `AsyncReactiveGraph::source`")]
fn cell<T>(&self, value: T) -> Self::Source<T>
where
Self: Sized,
T: PartialEq + Clone + Send + Sync + 'static,
{
self.source(value)
}
#[deprecated(note = "use `AsyncReactiveGraph::get`")]
fn get_cell<T>(&self, handle: &Self::Source<T>) -> T
where
Self: Sized,
T: Clone + Send + Sync + 'static,
Self::Source<T>: Read<Self, Output = T>,
{
self.get(handle)
}
#[deprecated(note = "use `AsyncReactiveGraph::set`")]
fn set_cell<T>(&self, handle: &Self::Source<T>, value: T)
where
Self: Sized,
T: PartialEq + Clone + Send + Sync + 'static,
Self::Source<T>: Write<Self, Value = T>,
{
self.set(handle, value);
}
}