Files
feuerwerk/src/core/graph/nodes/flow/StateTransition.ts
T

30 lines
1.3 KiB
TypeScript

import { textOf } from '../../../model/Properties';
import { flatDef } from '../../../properties/PropertySchema';
import type { NodeDef } from '../../NodeRegistry';
import { currentState, setState } from '../../sharedState';
import { machinePort, STATE_TRANSITION, TRANSITION_PARAM, transitionTo } from '../../stateMachine';
import { asString } from '../../values';
import { exec } from '../shared';
/** What the transitions drawn in the machine are for at runtime: one step along a named arrow. */
export const StateTransition: NodeDef = {
type: STATE_TRANSITION,
label: 'State transition',
category: 'action',
description:
"Takes the transition of this name out of the machine's current state, then continues. " +
'Nothing happens when that state has no transition of the name.',
inputs: [exec(), machinePort()],
outputs: [exec()],
params: [flatDef(TRANSITION_PARAM, 'text', { label: 'Transition', value: '' })],
run: (ctx, inputs, params, _def, _entry, lookup) => {
const id = asString(inputs.machine);
const current = currentState(ctx, id);
const machine = current ? lookup.machine(id) : null;
const name = textOf(params[TRANSITION_PARAM] ?? '');
const target = machine ? transitionTo(machine, current, name) : '';
if (target) setState(ctx, id, target, true);
return { next: 'exec' };
},
};