Skip to content

DependencyGraph

Defined in: packages/engine/src/vm/DependencyGraph.ts:20

Dependency graph for variable and data-source tracking across document lines.

Tracks which lines read/write which variables, and propagates changes through the graph when a variable is modified. Supports:

  • Variable dependency tracking (registerLine, getAffectedLines)
  • Data-source dependency tracking (registerLineDataSourceDependency)
  • Topological ordering of affected lines (getAffectedLinesInOrder)
  • Efficient removal of deleted lines (removeLine)
new DependencyGraph(): DependencyGraph;

DependencyGraph

clear(): void;

Defined in: packages/engine/src/vm/DependencyGraph.ts:332

Clear all dependency graph state. Called on document switch or engine reset.

void


getAffectedLines(changedVariable): Set<number>;

Defined in: packages/engine/src/vm/DependencyGraph.ts:105

Find all lines affected by a changed variable via BFS through the consumer graph.

When a variable is modified (e.g., :x = 5 changes to :x = 10), this returns all lines that transitively depend on it, lines that read x, lines that read variables written by those lines, and so on.

ParameterTypeDescription
changedVariablestringThe variable name that changed

Set<number>

Set of line numbers that need re-evaluation


getAffectedLinesByDataSource(dataSourceId, queryKey): Set<number>;

Defined in: packages/engine/src/vm/DependencyGraph.ts:226

Find all lines affected by a data source update.

When an async data source resolves (e.g., currency rate fetch completes), this returns all lines that depend on that specific data query.

ParameterTypeDescription
dataSourceIdstringThe data source identifier
queryKeystring[]The query key that was updated

Set<number>

Set of line numbers that need re-evaluation


getAffectedLinesInOrder(startVariable): number[];

Defined in: packages/engine/src/vm/DependencyGraph.ts:136

Phase 1.4 DAG-walk optimization: return affected lines in dependency-safe topological order. Uses Kahn’s algorithm (BFS-based) to ensure every line is evaluated AFTER all lines it depends on have been processed.

This is more correct than ascending line-number sort, which fails when variable definitions and their consumers are not in document order.

ParameterType
startVariablestring

number[]

Line numbers in topological order (producers before consumers).


getConsumers(variable): Set<number>;

Defined in: packages/engine/src/vm/DependencyGraph.ts:272

Get all line numbers that consume (read) a given variable.

ParameterTypeDescription
variablestringThe variable name

Set<number>

Set of line numbers that read this variable, or empty set if none


getDependencies(lineNumber): Set<string>;

Defined in: packages/engine/src/vm/DependencyGraph.ts:282

Get all variables that a line depends on (reads).

ParameterTypeDescription
lineNumbernumberThe line number to query

Set<string>

Set of variable names this line reads, or empty set if none


getSnapshot(): DagSnapshot;

Defined in: packages/engine/src/vm/DependencyGraph.ts:302

Get a serializable snapshot of the entire dependency graph for diagnostics.

Returns plain objects (not Maps/Sets) so consumers don’t need to reach into private fields. Used by playground diagnostic tabs for DAG visualization.

DagSnapshot


getWrites(lineNumber): Set<string>;

Defined in: packages/engine/src/vm/DependencyGraph.ts:292

Get all variables that a line writes (assigns to).

ParameterTypeDescription
lineNumbernumberThe line number to query

Set<string>

Set of variable names this line writes, or empty set if none


registerLine(
lineNumber,
reads,
writes): void;

Defined in: packages/engine/src/vm/DependencyGraph.ts:42

Register a line’s variable reads and writes in the dependency graph.

If re-registering the same line (e.g., after editing), old consumer references are cleaned up first. Write-variables are removed from the consumer set so that redefinition breaks the old dependency chain.

ParameterTypeDescription
lineNumbernumber1-based line number in the document
readsstring[]Variable names this line reads
writesstring[]Variable names this line writes (assigns to)

void


registerLineDataSourceDependency(
lineNumber,
dataSourceId,
queryKey): void;

Defined in: packages/engine/src/vm/DependencyGraph.ts:80

Register a line’s dependency on an external data source (e.g., currency rate, OSRS GE price).

When the data source updates, getAffectedLinesByDataSource returns all lines that depend on this data, enabling targeted re-evaluation.

ParameterTypeDescription
lineNumbernumber1-based line number in the document
dataSourceIdstringUnique identifier for the data source (e.g., “currency”, “osrs-ge”)
queryKeystring[]Query key array identifying the specific data (e.g., [“USD”, “EUR”])

void


removeLine(lineNumber): void;

Defined in: packages/engine/src/vm/DependencyGraph.ts:240

Remove a line from the dependency graph (e.g., when a line is deleted from the document).

Cleans up all consumer references, write registrations, and data source dependencies for the removed line. O(k) where k is the number of variables the line reads.

ParameterTypeDescription
lineNumbernumberThe line number being removed

void