@zedux/atoms
Version:
A Molecular State Engine for React
65 lines (64 loc) • 2.97 kB
TypeScript
import { DependentCallback, DependentEdge, EcosystemGraphNode, EvaluationReason, EvaluationType, GraphEdgeSignal } from '../types/index.js';
import { Ecosystem } from './Ecosystem.js';
export declare class Graph {
readonly ecosystem: Ecosystem;
nodes: Record<string, EcosystemGraphNode>;
private updateStack;
constructor(ecosystem: Ecosystem);
/**
* Draw a new edge between two nodes in the graph. This is how dependencies
* are created between atoms, selectors, and external nodes like React
* components.
*/
addEdge(dependentKey: string, dependencyKey: string, operation: string, flags: number, callback?: DependentCallback): DependentEdge | undefined;
addNode(nodeId: string, isSelector?: boolean): void;
/**
* Prevent new graph edges from being added immediately. Instead, buffer them
* so we can prevent duplicates or unnecessary edges. Call `.flushUpdates()`
* to finish buffering.
*
* This is used during atom and AtomSelector evaluation to make the graph as
* efficient as possible.
*/
bufferUpdates(key: string): void;
/**
* If an atom instance or AtomSelector errors during evaluation, we need to
* destroy any instances or AtomSelectors created during that evaluation that
* now have no dependents.
*/
destroyBuffer(): void;
/**
* Stop buffering updates for the node passed to `.bufferUpdates()` and add
* the buffered edges to the graph.
*/
flushUpdates(): void;
removeDependencies(dependentKey: string): void;
/**
* Should only be used internally. Remove the graph edge between two nodes.
* The dependent may not exist as a node in the graph if it's external, e.g. a
* React component
*
* For some reason in React 18+, React destroys parents before children. This
* means a parent EcosystemProvider may have already unmounted and wiped the
* whole graph; this edge may already be destroyed.
*/
removeEdge(dependentKey: string, dependencyKey: string): void;
removeNode(nodeId: string): void;
/**
* Schedules a job to update all dependents of a node. This is called e.g.
* when an atom instance or AtomSelector updates, when an atom instance is
* force-destroyed, or when an atom instance's promise changes.
*/
scheduleDependents(nodeId: string, reasons: EvaluationReason[], newState: any, oldState: any, shouldSetTimeout?: boolean, type?: EvaluationType, signal?: GraphEdgeSignal, scheduleStaticDeps?: boolean): void;
/**
* Actually add an edge to the graph. When we buffer graph updates, we're
* really just deferring the calling of this method.
*/
private finishAddingEdge;
/**
* When a non-static edge is added or removed, every node below that edge (the
* dependent, its dependents, etc) in the graph needs to have its weight
* recalculated.
*/
private recalculateNodeWeight;
}