MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / flow

Function flow

ui/src/lib/program-model.ts:140–194  ·  view source on GitHub ↗
(block: WireBlock, incoming: readonly Tail[], runs: readonly string[])

Source from the content-addressed store, hash-verified

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.
172 [{ id: fork.id, when: [arm.when], runs: [...runs], arm: armWord(item, arm) }]
173 : tails.map((t) => ({ id: t.id, when: [...t.when, arm.when], runs: [...t.runs, ...runs] }));
174 // An arm that answers, returns or throws does not rejoin — nothing
175 // leaves the last box in it, which is what says so on a canvas.
176 const armTails = flow(arm.body, entry, []);
177 if (arm.ends === null) for (const t of armTails) out.push(t);
178 }
179 // An `if` with no `else` runs on either way; a fork with both sides
180 // covered runs on only through the arms that did not end.
181 if (item.arms.length < 2) for (const t of tails) out.push({ ...t, runs: [...t.runs, ...runs] });
182 tails = mergeTails(out);
183 } else if (item.kind === 'block') {
184 const label = runWords(item);
185 const inner = flow(item.body, tails, [...runs, label]);
186 // A helper that answers on one path still returns on another: the code
187 // after the call follows the call, not nothing. And what comes after
188 // the call is not inside it — a tail that fell through the block drops
189 // the block's own words on the way out.
190 tails = (inner.length > 0 ? inner : tails).map((t) => (t.runs.includes(label) ? { ...t, runs: t.runs.filter((r) => r !== label) } : t));
191 }
192 }
193 return tails;
194 };
195
196 flow(program.root, [{ id: anchor, when: [], runs: [] }], []);
197

Callers 1

orderGraphFunction · 0.70

Calls 8

hasStepFunction · 0.85
armWordFunction · 0.85
mergeTailsFunction · 0.85
runWordsFunction · 0.85
pushMethod · 0.80
mapMethod · 0.80
joinFunction · 0.70
addMethod · 0.45

Tested by

no test coverage detected