Lavash.Graph (Lavash v0.3.0-rc.0)

Copy Markdown View Source

Pure graph algorithms for dependency graphs.

All functions operate on a deps map: %{name => [deps]} where each key is a derived field and the value is the list of fields it depends on.

Used by:

  • Rx.Graph — runtime reactive graph
  • ColocatedTransformer — compile-time JS metadata generation
  • GraphMacro — defgraph compile-time JS generation
  • JS hook (lavash_optimistic.js) — consumes the serialized output client-side

Summary

Functions

Reverse dependency index.

Topological sort via Kahn's algorithm.

BFS to find all fields transitively affected by changed_fields.

Functions

build_dependents(deps_map)

Reverse dependency index.

Returns %{field => [derived fields that depend on it]}.

iex> Lavash.Graph.build_dependents(%{doubled: [:count], quad: [:doubled]})
%{count: [:doubled], doubled: [:quad]}

topo_sort(deps_map)

Topological sort via Kahn's algorithm.

Returns derived field names in dependency order (leaves first). State fields (names not in the deps map keys) are ignored.

iex> Lavash.Graph.topo_sort(%{doubled: [:count], quad: [:doubled]})
[:doubled, :quad]

transitive_dependents(dependents, changed_fields)

BFS to find all fields transitively affected by changed_fields.

Uses the dependents map (output of build_dependents/1).

iex> dependents = %{count: [:doubled], doubled: [:quad]}
iex> Lavash.Graph.transitive_dependents(dependents, [:count])
MapSet.new([:doubled, :quad])