import { Atom, Registry } from '@effect-atom/atom'; import * as Option from 'effect/Option'; import * as Pipeable from 'effect/Pipeable'; import { Event } from '@dxos/async'; import { type MakeOptional } from '@dxos/util'; import * as Node from './node'; /** * Get the Graph a Node is currently associated with. */ export declare const getGraph: (node: Node.Node) => Graph; export type GraphTraversalOptions = { /** * A callback which is called for each node visited during traversal. * * If the callback returns `false`, traversal is stops recursing. */ visitor: (node: Node.Node, path: string[]) => boolean | void; /** * The node to start traversing from. * * @default ROOT_ID */ source?: string; /** The relation(s) to traverse graph edges. */ relation: Node.RelationInput | Node.RelationInput[]; }; export type GraphProps = { registry?: Registry.Registry; nodes?: MakeOptional[]; edges?: Record; onExpand?: (id: string, relation: Node.Relation) => void; onInitialize?: (id: string) => Promise; onRemoveNode?: (id: string) => void; }; export type Edge = { source: string; target: string; relation: Node.RelationInput; }; export type Edges = Record; /** * Identifier denoting a Graph. */ export declare const GraphTypeId: unique symbol; export type GraphTypeId = typeof GraphTypeId; /** * Identifier for the graph kind discriminator. */ export declare const GraphKind: unique symbol; export type GraphKind = typeof GraphKind; export type GraphKindType = 'readable' | 'expandable' | 'writable'; export interface BaseGraph extends Pipeable.Pipeable { readonly [GraphTypeId]: GraphTypeId; readonly [GraphKind]: GraphKindType; /** * Event emitted when a node is changed. */ readonly onNodeChanged: Event<{ id: string; node: Option.Option; }>; /** * Get the atom key for the JSON representation of the graph. */ json(id?: string): Atom.Atom; /** * Get the atom key for the node with the given id. */ node(id: string): Atom.Atom>; /** * Get the atom key for the node with the given id. */ nodeOrThrow(id: string): Atom.Atom; /** * Get the atom key for the connections of the node with the given id. */ connections(id: string, relation: Node.RelationInput): Atom.Atom; /** * Get the atom key for the actions of the node with the given id. */ actions(id: string): Atom.Atom<(Node.Action | Node.ActionGroup)[]>; /** * Get the atom key for the edges of the node with the given id. */ edges(id: string): Atom.Atom; } export type ReadableGraph = BaseGraph & { readonly [GraphKind]: 'readable' | 'expandable' | 'writable'; }; export type ExpandableGraph = BaseGraph & { readonly [GraphKind]: 'expandable' | 'writable'; }; export type WritableGraph = BaseGraph & { readonly [GraphKind]: 'writable'; }; /** * Graph interface. */ export type Graph = WritableGraph; /** * Convert the graph to a JSON object. */ export declare const toJSON: (graph: BaseGraph, id?: string) => object; /** * Get the node with the given id from the graph's registry. */ export declare function getNode(graph: BaseGraph, id: string): Option.Option; export declare function getNode(id: string): (graph: BaseGraph) => Option.Option; /** * Get the node with the given id from the graph's registry. * * @throws If the node is Option.none(). */ export declare function getNodeOrThrow(graph: BaseGraph, id: string): Node.Node; export declare function getNodeOrThrow(id: string): (graph: BaseGraph) => Node.Node; /** * Get the root node of the graph. * This is an alias for `getNodeOrThrow(graph, ROOT_ID)`. */ export declare function getRoot(graph: BaseGraph): Node.Node; /** * Get all nodes connected to the node with the given id by the given relation from the graph's registry. */ export declare function getConnections(graph: BaseGraph, id: string, relation: Node.RelationInput): Node.Node[]; export declare function getConnections(id: string, relation: Node.RelationInput): (graph: BaseGraph) => Node.Node[]; /** * Get all actions connected to the node with the given id from the graph's registry. */ export declare function getActions(graph: BaseGraph, id: string): Node.Node[]; export declare function getActions(id: string): (graph: BaseGraph) => Node.Node[]; /** * Get the edges from the node with the given id from the graph's registry. */ export declare function getEdges(graph: BaseGraph, id: string): Edges; export declare function getEdges(id: string): (graph: BaseGraph) => Edges; /** * Traverse the graph with the given options. */ export declare function traverse(graph: BaseGraph, options: GraphTraversalOptions, path?: string[]): void; export declare function traverse(options: GraphTraversalOptions, path?: string[]): (graph: BaseGraph) => void; /** * Get the path between two nodes in the graph. */ export declare function getPath(graph: BaseGraph, params: { source?: string; target: string; }): Option.Option; export declare function getPath(params: { source?: string; target: string; }): (graph: BaseGraph) => Option.Option; /** * Wait for the path between two nodes in the graph to be established. */ export declare function waitForPath(graph: BaseGraph, params: { source?: string; target: string; }, options?: { timeout?: number; interval?: number; }): Promise; export declare function waitForPath(params: { source?: string; target: string; }, options?: { timeout?: number; interval?: number; }): (graph: BaseGraph) => Promise; /** * Initialize a node in the graph. * * Fires the `onInitialize` callback to provide initial data for a node. * * TODO(wittjosiah): Remove? No graph-builder extension declares a `resolver`, so `onInitialize` has * nothing to run; callers expand the nodes they need explicitly. */ export declare function initialize(graph: T, id: string): Promise; export declare function initialize(id: string): (graph: T) => Promise; /** * Expand a node in the graph. * * Fires the `onExpand` callback to add connections to the node. */ export declare function expand(graph: T, id: string, relation: Node.RelationInput): T; export declare function expand(id: string, relation: Node.RelationInput): (graph: T) => T; /** * Sort the edges of the node with the given id. */ export declare function sortEdges(graph: T, id: string, relation: Node.RelationInput, order: string[]): T; export declare function sortEdges(id: string, relation: Node.RelationInput, order: string[]): (graph: T) => T; /** * Add nodes to the graph. */ export declare function addNodes(graph: T, nodes: Node.NodeArg>[]): T; export declare function addNodes(nodes: Node.NodeArg>[]): (graph: T) => T; /** * Add a node to the graph. */ export declare function addNode(graph: T, nodeArg: Node.NodeArg>): T; export declare function addNode(nodeArg: Node.NodeArg>): (graph: T) => T; /** * Remove nodes from the graph. */ export declare function removeNodes(graph: T, ids: string[], edges?: boolean): T; export declare function removeNodes(ids: string[], edges?: boolean): (graph: T) => T; /** * Remove a node from the graph. */ export declare function removeNode(graph: T, id: string, edges?: boolean): T; export declare function removeNode(id: string, edges?: boolean): (graph: T) => T; /** * Add edges to the graph. */ export declare function addEdges(graph: T, edges: Edge[]): T; export declare function addEdges(edges: Edge[]): (graph: T) => T; /** * Add an edge to the graph. */ export declare function addEdge(graph: T, edgeArg: Edge): T; export declare function addEdge(edgeArg: Edge): (graph: T) => T; /** * Remove edges from the graph. */ export declare function removeEdges(graph: T, edges: Edge[], removeOrphans?: boolean): T; export declare function removeEdges(edges: Edge[], removeOrphans?: boolean): (graph: T) => T; /** * Remove an edge from the graph. */ export declare function removeEdge(graph: T, edgeArg: Edge, removeOrphans?: boolean): T; export declare function removeEdge(edgeArg: Edge, removeOrphans?: boolean): (graph: T) => T; /** * Creates a new Graph instance. */ export declare const make: (params?: GraphProps) => Graph; export declare const relationKey: (relation: Node.RelationInput) => string; export declare const relationFromKey: (encoded: string) => Node.Relation; //# sourceMappingURL=graph.d.ts.map