A single node now lists any number of variables. Every entry is an output that reads the current value whenever it is pulled; an entry switched to Set is also an input and is written when the flow arrives. Exec pins only exist while at least one entry writes, and all inputs are read before any write, so one node can swap two variables. Get/Set variable, Get/Set variables and Variable are gone. Old project files are converted on load: each old node becomes a Variables node with the same variables, edges and typed values. A written port without a typed value now shows the zero of its kind on the node body, matching what the inspector shows and what the run writes. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
35 lines
1.3 KiB
TypeScript
35 lines
1.3 KiB
TypeScript
import { portDefOf, VARIABLES } from '../../nested';
|
|
import { type NodeDef, withInlineDefault } from '../../NodeRegistry';
|
|
import { writeTyped } from '../../values';
|
|
import { exec } from '../shared';
|
|
|
|
/**
|
|
* Every variable picked is an output, read at the moment it is asked for. One switched to Set is an
|
|
* input too; the flow pins exist only while at least one is.
|
|
*/
|
|
export const Variables: NodeDef = {
|
|
type: VARIABLES,
|
|
label: 'Variables',
|
|
category: 'variable',
|
|
description:
|
|
"Reads this behavior's variables at any time. Those switched to Set are written together when " +
|
|
'the flow arrives, and a read after that sees the new value.',
|
|
inputs: [],
|
|
outputs: [],
|
|
params: [],
|
|
declaresPorts: 'both',
|
|
declaredPort: (port) => withInlineDefault(portDefOf(port)),
|
|
derivePorts: ({ node }) => {
|
|
const ports = node.ports ?? [];
|
|
const only = ports.length === 1 ? ports[0]?.key : undefined;
|
|
const shape = ports.some((p) => p.write) ? { inputs: [exec()], outputs: [exec()] } : {};
|
|
return only ? { ...shape, label: only } : shape;
|
|
},
|
|
read: (ctx) => ctx.variables,
|
|
run: (ctx, inputs, _params, def) => {
|
|
for (const port of def.inputs)
|
|
if (port.type !== 'exec') writeTyped(ctx.variables, port.key, inputs[port.key]);
|
|
return { next: 'exec' };
|
|
},
|
|
};
|