(flows: readonly WireFlow[], activeId: string | null)
| 341 | * picture underneath the reader. |
| 342 | */ |
| 343 | export function buildFlowLayout(flows: readonly WireFlow[], activeId: string | null): FlowLayout { |
| 344 | const active = flows.find((f) => f.id === activeId) ?? flows[0] ?? null; |
| 345 | const activeSteps = new Map<string, number>(); |
| 346 | active?.hops.forEach((hop, index) => activeSteps.set(hop.node.id, index)); |
| 347 | |
| 348 | // ---- collect nodes and edges over every flow on screen ------------------- |
| 349 | const cards = new Map<string, { hop: WireFlowHop; flows: string[]; order: number }>(); |
| 350 | const links = new Map< |
| 351 | string, |
| 352 | { source: string; target: string; edge: WireFlowEdge; flows: string[] } |
| 353 | >(); |
| 354 | const successors = new Map<string, Set<string>>(); |
| 355 | const indegree = new Map<string, number>(); |
| 356 | |
| 357 | let order = 0; |
| 358 | for (const flow of flows) { |
| 359 | for (let i = 0; i < flow.hops.length; i++) { |
| 360 | const hop = flow.hops[i] as WireFlowHop; |
| 361 | const id = hop.node.id; |
| 362 | const existing = cards.get(id); |
| 363 | if (existing) { |
| 364 | if (!existing.flows.includes(flow.id)) existing.flows.push(flow.id); |
| 365 | } else { |
| 366 | cards.set(id, { hop, flows: [flow.id], order: order++ }); |
| 367 | indegree.set(id, 0); |
| 368 | successors.set(id, new Set()); |
| 369 | } |
| 370 | |
| 371 | const previous = flow.hops[i - 1]; |
| 372 | if (!previous || hop.edge === null) continue; |
| 373 | // An upward hop is the same edge read backwards; the ARROW still points |
| 374 | // the way the reader travelled, which is what the strip is describing. |
| 375 | const from = previous.node.id; |
| 376 | const key = `${from} ${id}`; |
| 377 | const link = links.get(key); |
| 378 | if (link) { |
| 379 | if (!link.flows.includes(flow.id)) link.flows.push(flow.id); |
| 380 | continue; |
| 381 | } |
| 382 | links.set(key, { source: from, target: id, edge: hop.edge, flows: [flow.id] }); |
| 383 | const outs = successors.get(from); |
| 384 | if (outs && !outs.has(id)) { |
| 385 | outs.add(id); |
| 386 | indegree.set(id, (indegree.get(id) ?? 0) + 1); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (cards.size === 0) { |
| 392 | return { cards: [], endCaps: [], links: [], width: 0, height: 0, columns: 0, gaps: [] }; |
| 393 | } |
| 394 | |
| 395 | // ---- column = longest distance from a start ----------------------------- |
| 396 | const column = new Map<string, number>(); |
| 397 | for (const id of cards.keys()) column.set(id, 0); |
| 398 | // Kahn order, so a node is placed only after everything that reaches it. |
| 399 | const pending = new Map(indegree); |
| 400 | const queue = [...cards.keys()].filter((id) => (pending.get(id) ?? 0) === 0); |
no test coverage detected