* Build sorted children for a node, deduplicating edges to the same child.
(
nodeId: string,
childrenMap: Map<string, { id: string; variables: string[] }[]>,
nodeMap: Map<string, DagNode>
)
| 259 | * Build sorted children for a node, deduplicating edges to the same child. |
| 260 | */ |
| 261 | function buildSortedChildren( |
| 262 | nodeId: string, |
| 263 | childrenMap: Map<string, { id: string; variables: string[] }[]>, |
| 264 | nodeMap: Map<string, DagNode> |
| 265 | ): { id: string; variables: string[] }[] { |
| 266 | // Get children and deduplicate by target ID, merging variables |
| 267 | const childrenRaw = childrenMap.get(nodeId) ?? [] |
| 268 | const childrenById = new Map<string, string[]>() |
| 269 | for (const child of childrenRaw) { |
| 270 | const existing = childrenById.get(child.id) ?? [] |
| 271 | existing.push(...child.variables) |
| 272 | childrenById.set(child.id, existing) |
| 273 | } |
| 274 | |
| 275 | const children = Array.from(childrenById.entries()).map(([id, vars]) => ({ |
| 276 | id, |
| 277 | variables: [...new Set(vars)], // deduplicate variables |
| 278 | })) |
| 279 | |
| 280 | // Sort by DAG order |
| 281 | children.sort((a, b) => { |
| 282 | const orderA = nodeMap.get(a.id)?.order ?? 0 |
| 283 | const orderB = nodeMap.get(b.id)?.order ?? 0 |
| 284 | return orderA - orderB |
| 285 | }) |
| 286 | |
| 287 | return children |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Render a node and its subtree in tree format. |