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' }; }, };