Reading one field of a struct needed its own Expand node, even where the graph already knows the struct type. A split output shows the fields directly on the node, so a Find … Match test becomes a single Equals and the wires keep running left to right. - Right-clicking an output pin offers "Split into <Type> fields" when the type is known and "Split as …" for every other struct asset. The output is replaced by one pin per field with the field's kind. - The type is known for typed struct variables, typed Pack/Expand, fields of another split and item of For each, Find, Match and Item at over a typed list (portSplits.ts). - Right-clicking a field pin offers "Recombine". Splitting drops the edges of the whole output, recombining drops the field edges. One undo step brings them back. - The split is stored as GraphNode.splits and saved with the graph. Copy and paste carry it along. - Field pins follow the struct asset like a typed Expand. A new field gets a pin, a removed field loses pin and edge, a renamed field keeps its edge. Deleting the asset keeps the last fields. The delete dialog counts splits, so it now says "node(s)" instead of "Pack/Expand node(s)". - Renaming a variable carries its split and the field edges along. - At run time the node still produces the whole value. A field pin reads its field like Expand, a missing field reads as empty. - Limits: outputs only, one level, behavior graphs only. Tests: - ./check.sh: 185 files, 2706 tests, CHECK EXIT 0 - e2e GM_STEPS=1-6,54,55: ALL PASSED, 303 checks - New port-splits.test.ts with 12 tests. e2e 6z-vi with 3 checks using a real mouse: split Find.item, wire item.Name into the test and Match.item.Amount into the log (logs 7), recombine. - Counter-checks: without field reads in the interpreter, without rename in the split, without moving edges on rename and without cleanup on field removal one unit test fails each. In the browser, without field reads nothing is logged and without type inference the split entry is missing. - Check total in README: 1520. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
63 lines
2.8 KiB
TypeScript
63 lines
2.8 KiB
TypeScript
/**
|
||
* Split outputs: a struct output shown as one output per field, so reading a field needs no `Expand`
|
||
* of its own. Only the drawing and the wiring see the field ports – `resolveDef` puts them in place of
|
||
* the whole. The node itself still produces the whole value, and the interpreter picks the field out
|
||
* of it when a field port is read (`fieldOfSplit`).
|
||
*/
|
||
import type { GraphNode, PortSplit } from './Graph';
|
||
import type { NodeDef, PortDef } from './NodeRegistry';
|
||
import { asStruct, unplaced, type Value } from './values';
|
||
|
||
/** Declared keys are identifiers, so the dot never clashes with a port of the node's own. */
|
||
export function splitPortKey(port: string, field: string): string {
|
||
return `${port}.${field}`;
|
||
}
|
||
|
||
export function splitOf(node: GraphNode, port: string): PortSplit | undefined {
|
||
return node.splits?.find((s) => s.port === port);
|
||
}
|
||
|
||
/** Which split output a key names, and which field of it – undefined for any other key. */
|
||
export function splitField(node: GraphNode, key: string): { split: PortSplit; field: string } | undefined {
|
||
const dot = key.indexOf('.');
|
||
if (dot < 0) return undefined;
|
||
const split = splitOf(node, key.slice(0, dot));
|
||
const field = key.slice(dot + 1);
|
||
return split?.fields.some((f) => f.key === field) ? { split, field } : undefined;
|
||
}
|
||
|
||
/** Whether an output can be taken apart: a value that may hold a struct, and not already split. */
|
||
export function canSplit(node: GraphNode, port: PortDef): boolean {
|
||
return (port.type === 'struct' || port.type === 'any') && !port.ghost && !splitOf(node, port.key);
|
||
}
|
||
|
||
/** The outputs as drawn and wired: each split one replaced, where it stood, by its field ports. */
|
||
export function withSplits(
|
||
def: NodeDef,
|
||
node: GraphNode,
|
||
portOf: (key: string, kind: PortSplit['fields'][number]) => PortDef,
|
||
): NodeDef {
|
||
if (!node.splits?.length) return def;
|
||
const outputs = def.outputs.flatMap((port) => {
|
||
const split = port.type === 'exec' ? undefined : splitOf(node, port.key);
|
||
return split ? split.fields.map((f) => portOf(splitPortKey(port.key, f.key), f)) : [port];
|
||
});
|
||
return { ...def, outputs };
|
||
}
|
||
|
||
/** One field of a whole output, read the way `Expand` reads it: a field the value lacks is empty. */
|
||
export function fieldValue(whole: Value | undefined, field: string): Value {
|
||
return unplaced(asStruct(whole)[field]) ?? null;
|
||
}
|
||
|
||
/** What a node produced, with the value of every field port of its splits beside the wholes. */
|
||
export function withSplitValues(node: GraphNode, values: Record<string, Value>): Record<string, Value> {
|
||
if (!node.splits?.length) return values;
|
||
const out = { ...values };
|
||
for (const split of node.splits)
|
||
if (split.port in values)
|
||
for (const f of split.fields)
|
||
out[splitPortKey(split.port, f.key)] = fieldValue(values[split.port], f.key);
|
||
return out;
|
||
}
|