Skip to main content

ReactiveGraph

Trait ReactiveGraph 

Source
pub trait ReactiveGraph {
    type Computed<T>: GraphNode + Copy;
    type Source<T>: GraphNode + Copy;
    type Effect: GraphNode + Copy;
    type Scope<'a>: Teardown
       where Self: 'a;

    // Required methods
    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;
}
Expand description

The graph operations that need no bounds on node values.

Everything here — disposal, scopes, batching, degree introspection — is about graph structure, so generic code over this trait pays for nothing it does not use. Construction and reads live in SyncReactiveGraph / AsyncReactiveGraph. Both capability traits expose the Cell-kernel vocabulary: source, get, and set.

use lazily::{Context, ReactiveGraph, SyncReactiveGraph};

// Written once, no value bounds, works for every execution model.
fn leaked_edges<G: ReactiveGraph>(graph: &G, node: &G::Source<i64>) -> usize {
    graph.dependent_count(node)
}

let ctx = Context::new();
let topic = SyncReactiveGraph::source(&ctx, 1i64);
let derived = SyncReactiveGraph::computed(&ctx, move |c| c.get(&topic) + 1);
assert_eq!(SyncReactiveGraph::get(&ctx, &derived), 2);
assert_eq!(leaked_edges(&ctx, &topic), 1);

ReactiveGraph::dispose_slot(&ctx, &derived);
assert_eq!(leaked_edges(&ctx, &topic), 0);

Required Associated Types§

Source

type Computed<T>: GraphNode + Copy

This graph’s derived-slot handle.

Bounded by GraphNode and Copy so generic code can take degrees of any handle and pass handles around freely. Every handle in the crate is an id, so both hold everywhere.

Source

type Source<T>: GraphNode + Copy

This graph’s source-cell handle.

Source

type Effect: GraphNode + Copy

This graph’s effect handle.

Source

type Scope<'a>: Teardown where Self: 'a

This graph’s teardown scope.

Required Methods§

Source

fn dispose_slot<T: 'static>(&self, handle: &Self::Computed<T>)

Tear down a derived slot: detach both edge directions, invalidate the surviving readers, and recycle the id.

Source

fn dispose_cell<T: 'static>(&self, handle: &Self::Source<T>)

Tear down a source cell: detach its dependents, invalidate them, and recycle the id.

Source

fn dispose_effect(&self, handle: &Self::Effect)

Tear down an effect, running its cleanup.

Source

fn scope(&self) -> Self::Scope<'_>

Open a teardown scope: nodes created through it are disposed when it ends, in reverse creation order.

Source

fn batch<R>(&self, run: impl FnOnce(&Self) -> R) -> R

Run run with invalidation batched until it returns.

Source

fn dependent_count(&self, node: &impl GraphNode) -> usize

How many nodes currently depend on node — the size of its reverse edge set. Returns 0 for a disposed or unknown node.

Source

fn dependency_count(&self, node: &impl GraphNode) -> usize

How many nodes node currently depends on — the size of its forward edge set. Returns 0 for a disposed or unknown node.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§