| 112 | * starts, so the first thing in the body follows it. |
| 113 | */ |
| 114 | export function orderGraph(program: NonNullable<WireStepsPayload['program']>, anchor: string): OrderGraph { |
| 115 | const edges: OrderEdge[] = []; |
| 116 | const seen = new Set<string>([anchor]); |
| 117 | const at = new Map<string, OrderEdge>(); |
| 118 | const forks: OrderFork[] = []; |
| 119 | |
| 120 | const join = (from: string, to: string, tail: Tail): void => { |
| 121 | if (from === to) return; |
| 122 | const key = `${from} ${to}`; |
| 123 | const when = tail.when.filter((w, i) => w && tail.when.indexOf(w) === i).join(' && '); |
| 124 | const found = at.get(key); |
| 125 | if (found) { |
| 126 | // Two ways to the same step: the picture keeps both conditions, the way |
| 127 | // a link with several sites does. |
| 128 | if (when !== found.when) found.when = !when || !found.when ? '' : `${found.when} || ${when}`; |
| 129 | for (const r of tail.runs) if (!found.runs.includes(r)) found.runs.push(r); |
| 130 | // Reached as two different arms of one decision: it happens either way, |
| 131 | // and the line stops claiming a side. |
| 132 | if (found.arm !== tail.arm) delete found.arm; |
| 133 | return; |
| 134 | } |
| 135 | const edge: OrderEdge = { from, to, when, runs: [...tail.runs], ...(tail.arm !== undefined ? { arm: tail.arm } : {}) }; |
| 136 | at.set(key, edge); |
| 137 | edges.push(edge); |
| 138 | }; |
| 139 | |
| 140 | const flow = (block: WireBlock, incoming: readonly Tail[], runs: readonly string[]): Tail[] => { |
| 141 | let tails: Tail[] = [...incoming]; |
| 142 | for (const item of block) { |
| 143 | if (item.kind === 'step') { |
| 144 | seen.add(item.step); |
| 145 | for (const t of tails) join(t.id, item.step, { ...t, runs: [...t.runs, ...runs] }); |
| 146 | // What the step itself sets in motion happens inside it, so the next |
| 147 | // thing in the block follows THAT, not the box. |
| 148 | let inner: Tail[] = [{ id: item.step, when: [], runs: [] }]; |
| 149 | if (item.body && item.body.length > 0) inner = flow(item.body, inner, []); |
| 150 | tails = inner; |
| 151 | } else if (item.kind === 'fork') { |
| 152 | // A decision two or more of whose arms lead somewhere diverges from a |
| 153 | // POINT: the condition said once, each line out an arm — instead of |
| 154 | // two lines that each carry the whole predicate, one of them negated, |
| 155 | // with nothing saying they are the same choice. A fork with one drawn |
| 156 | // arm keeps the plain line: an early exit reads as a guard clause. |
| 157 | const drawn = item.arms.filter((a) => hasStep(a.body)).length; |
| 158 | const fork: OrderFork | null = |
| 159 | drawn >= 2 && tails.length > 0 ? { id: `fork:${forks.length}`, on: item.on, form: item.form } : null; |
| 160 | if (fork !== null) { |
| 161 | forks.push(fork); |
| 162 | for (const t of tails) join(t.id, fork.id, { ...t, runs: [...t.runs, ...runs] }); |
| 163 | } |
| 164 | const out: Tail[] = []; |
| 165 | for (const arm of item.arms) { |
| 166 | // The arm's condition as the SOURCE has it: the words are made once, |
| 167 | // at the end, or two ways of arriving would each carry their own WHEN. |
| 168 | const entry: Tail[] = |
| 169 | fork !== null && hasStep(arm.body) |
| 170 | ? // From the point: the line says the arm; the arm's own |
| 171 | // condition rides along for the hover. |