Dependency graph for topological sorting of plugins.

Ensures plugins are initialized in the correct order based on their dependencies.

const graph = new DependencyGraph();
graph.addNode('logger', []);
graph.addNode('database', ['logger']);
graph.addNode('api', ['database', 'logger']);

const sorted = graph.topologicalSort();
// ['logger', 'database', 'api']

Constructors

Methods

  • Adds a node to the graph.

    Parameters

    • name: string

      The node name

    • dependencies: string[]

      Names of nodes this node depends on

    Returns void

    graph.addNode('database', ['logger']);
    
  • Performs topological sort on the graph.

    Returns nodes in dependency order (dependencies first). Throws if circular dependencies are detected.

    Returns string[]

    Array of node names in topological order

    If circular dependencies exist

    If a dependency is not found

    const sorted = graph.topologicalSort();
    // Plugins can be initialized in this order
  • Gets the reverse topological order.

    Useful for shutdown (shutdown in reverse initialization order).

    Returns string[]

    Array of node names in reverse topological order