use std::{cell::RefCell, ops::Add, rc::Rc};
use super::{
Context, DropResource, Graph, GraphId, ToComputed, Value,
graph::{CallbackGuard, ErasedNode, GraphInner, Node, NodeId},
};
pub struct Computed<T: Clone + PartialEq + 'static> {
inner: Rc<ComputedInner<T>>,
}
struct ComputedInner<T: Clone + PartialEq + 'static> {
node: Node,
compute: Box<dyn Fn(&Context) -> T>,
value: RefCell<Option<T>>,
}
struct SubscribeInner {
node: Node,
refresh: Box<dyn Fn(&Context)>,
}
impl<T: Clone + PartialEq + 'static> Clone for Computed<T> {
fn clone(&self) -> Self {
Computed {
inner: self.inner.clone(),
}
}
}
impl<T: Clone + PartialEq + 'static> PartialEq for Computed<T> {
fn eq(&self, other: &Self) -> bool {
self.inner.node.id == other.inner.node.id
}
}
impl<T: Clone + PartialEq + 'static> ErasedNode for ComputedInner<T> {
fn refresh(&self) -> bool {
self.recompute()
}
}
impl ErasedNode for SubscribeInner {
fn refresh(&self) -> bool {
let tracking = Tracking::start(&self.node.graph);
(self.refresh)(&tracking.context);
tracking.record(self.node.id);
false
}
}
impl<T: Clone + PartialEq + 'static> ComputedInner<T> {
fn recompute(&self) -> bool {
let tracking = Tracking::start(&self.node.graph);
let new_value = (self.compute)(&tracking.context);
tracking.record(self.node.id);
let mut slot = self.value.borrow_mut();
match slot.as_ref() {
Some(old) if old == &new_value => false,
_ => {
*slot = Some(new_value);
true
}
}
}
fn ensure(&self) -> T {
if self.value.borrow().is_none() {
self.recompute();
}
match self.value.borrow().clone() {
Some(value) => value,
None => no_value(),
}
}
}
struct Tracking<'a> {
graph: &'a GraphInner,
_guard: CallbackGuard<'a>,
context: Context,
}
impl<'a> Tracking<'a> {
#[inline(never)]
fn start(graph: &'a GraphInner) -> Self {
Tracking {
graph,
_guard: graph.enter_callback(),
context: Context::tracking(),
}
}
#[inline(never)]
fn record(&self, id: NodeId) {
self.graph.set_parents(id, self.context.take_parents());
}
}
#[cold]
#[inline(never)]
fn no_value() -> ! {
panic!("vertigo: computed has no value after refresh")
}
impl<T: Clone + PartialEq + 'static> Computed<T> {
pub(crate) fn create(graph: Rc<GraphInner>, compute: Box<dyn Fn(&Context) -> T>) -> Self {
let inner = Rc::new(ComputedInner {
node: Node::new(graph),
compute,
value: RefCell::new(None),
});
inner.node.graph.register(inner.node.id, inner.clone());
Computed { inner }
}
pub fn from(compute: impl Fn(&Context) -> T + 'static) -> Self {
Self::create(super::default_graph().inner, Box::new(compute))
}
pub fn get(&self, ctx: &Context) -> T {
self.inner.node.read(ctx, self.inner.clone());
self.inner.ensure()
}
pub fn id(&self) -> GraphId {
GraphId::from_node(self.inner.node.id)
}
pub fn when_connect<F: Fn() -> DropResource + 'static>(&self, create: F) -> Computed<T> {
let new_computed = Computed::create(self.inner.node.graph.clone(), {
let parent = self.clone();
Box::new(move |context| parent.get(context))
});
new_computed
.inner
.node
.graph
.register_connect(new_computed.inner.node.id, Rc::new(create));
new_computed
}
pub fn subscribe<R: 'static, F: Fn(T) -> R + 'static>(self, callback: F) -> DropResource {
let graph = self.inner.node.graph.clone();
let parent = self;
register_sink(
graph,
Box::new(move |ctx| {
let _ = callback(parent.get(ctx));
}),
)
}
pub fn map<K: Clone + PartialEq + 'static, F: 'static + Fn(T) -> K>(
&self,
fun: F,
) -> Computed<K> {
Computed::create(self.inner.node.graph.clone(), {
let myself = self.clone();
Box::new(move |context| fun(myself.get(context)))
})
}
}
impl<T: Clone + PartialEq + 'static> Computed<T> {
pub(crate) fn graph(&self) -> Graph {
Graph {
inner: self.inner.node.graph.clone(),
}
}
}
impl Graph {
pub(crate) fn subscribe(&self, refresh: Box<dyn Fn(&Context)>) -> DropResource {
register_sink(self.inner.clone(), refresh)
}
}
fn register_sink(graph: Rc<GraphInner>, refresh: Box<dyn Fn(&Context)>) -> DropResource {
let inner = Rc::new(SubscribeInner {
node: Node::new(graph.clone()),
refresh,
});
graph.register(inner.node.id, inner.clone());
Graph {
inner: graph.clone(),
}
.transaction(|_| {
inner.refresh();
});
DropResource::from_struct(inner)
}
impl<T: Clone + PartialEq + 'static> ToComputed<T> for Computed<T> {
fn to_computed(&self) -> Computed<T> {
self.clone()
}
}
impl<T: Clone + PartialEq + 'static> ToComputed<T> for &Computed<T> {
fn to_computed(&self) -> Computed<T> {
(*self).clone()
}
}
impl<T: Clone + PartialEq + 'static> From<Value<T>> for Computed<T> {
fn from(val: Value<T>) -> Self {
val.to_computed()
}
}
impl<T: Clone + PartialEq + 'static> From<T> for Computed<T> {
fn from(value: T) -> Self {
Value::new(value).to_computed()
}
}
impl<T: Clone + PartialEq + 'static> From<&T> for Computed<T> {
fn from(value: &T) -> Self {
Value::new(value.clone()).to_computed()
}
}
impl From<&str> for Computed<String> {
fn from(value: &str) -> Self {
Value::new(value.to_string()).to_computed()
}
}
impl<T> Add for Computed<T>
where
T: Clone + PartialEq + Add<Output = T> + 'static,
{
type Output = Computed<T>;
fn add(self, rhs: Self) -> Self::Output {
Computed::from({
let left = self;
let right = rhs;
move |ctx| left.get(ctx) + right.get(ctx)
})
}
}
impl<T> Add<T> for Computed<T>
where
T: Clone + PartialEq + Add<Output = T> + 'static,
{
type Output = Computed<T>;
fn add(self, rhs: T) -> Self::Output {
self.map(move |left| left + rhs.clone())
}
}
#[cfg(test)]
mod tests {
use super::{super::Graph, *};
use std::cell::Cell;
#[test]
fn subscribe_does_not_notify_dependents() {
let g = Graph::new();
let logs = g.logger().listen();
let a = g.value(0);
let node = Node::new(g.inner.clone());
let id = node.id;
let sink = Rc::new(SubscribeInner {
node,
refresh: Box::new({
let a = a.clone();
move |ctx| {
let _ = a.get(ctx);
}
}),
});
g.inner.register(id, sink.clone());
sink.refresh();
let runs = Rc::new(Cell::new(0));
let child = g.computed({
let sink = sink.clone();
let runs = runs.clone();
move |ctx| {
runs.set(runs.get() + 1);
ctx.track(id, sink.clone());
0
}
});
g.transaction(|ctx| {
let _ = child.get(ctx);
});
runs.set(0);
a.set(1);
assert_eq!(runs.get(), 0);
logs.assert_eq(&[]);
}
}